Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Thread problems

119 views
Skip to first unread message

ivanis...@gmail.com

unread,
Jan 24, 2013, 5:12:38 AM1/24/13
to
Hi,
I have a problem when a thread is created because the function associated is not executed. My Ansi C code is:

>>
#include <stdio.h>
#include <spthread.h>

pthread_t threadid;
pthread_attr_t pta;

void *donothing() {
fprintf(stdout,"KKKKKK\n");fflush(0);
}

int main(int argc, char *argv[])
{
int i,rc;
int threadNumber=0;

pthread_attr_init(&pta);
pthread_attr_setdetachstate(&pta,PTHREAD_CREATE_JOINABLE);
fprintf(stdout,"Creating..\n");fflush(0);
rc = pthread_create(&threadid, &pta, donothing, NULL);

while(1){
}

}
<<

I am compilating it with:
"cc sample.c -o sample -L/G/system/zmxodbc -lZSPTDLL -D_PTH_TIMESPEC_DECLARED -D_SPT_MODEL_"

I have tried it using the "pthread.h" library, but the result is the same.

Thanks.

Keith Dick

unread,
Jan 24, 2013, 5:36:11 AM1/24/13
to
I know nothing about programming with these thread libraries, so I probably can't help much, but let me ask: How do you know your thread function is not being executed? Is it just that you do not see the output you expected it to write, or did you use a debugger to put a breakpoint in the code of the thread function, and see that execution never reaches the breakpoint?

I believe these threading libraries implement threads at the user code level, not in the OS, so I wonder whether that spin loop after the pthread_create() call is preventing switching to the new thread. Try putting a spt_sleep(1) call in that loop and see whether that makes any difference.

Anupam Das

unread,
Jan 24, 2013, 8:15:04 AM1/24/13
to
>> rc = pthread_create(&threadid, &pta, donothing, NULL); <<

Two things immediately comes to mind -

1. Why don't you run the program under the Inspect/eInspect debugger and set a breakpoint at donothing function and check whether it really does or doesn't trigger the initial function?

2. Let's remove pta pointer for a moment and make it NULL. I would like to see how to does this behave without parameters.

Anupam Das

unread,
Jan 24, 2013, 8:17:38 AM1/24/13
to
By the way, if you don't know how to prepare a binary for debugging meaningfully, here are the tips for OSS:

1. Compile using -g option along with all other options you mentioned above in the CC command line.
2. If the name of executable is something.exe then to start that under the control of the GDB debugger is -

OSS> run -debug /../../something.exe

Keith Dick

unread,
Jan 24, 2013, 9:55:02 AM1/24/13
to
You don't have to fully qualify the name from the root on down.

run -debug ./something.exe

will work.

JShepherd

unread,
Jan 24, 2013, 10:04:14 AM1/24/13
to
In article <d986be35-3633-4443...@googlegroups.com>,
ivanis...@gmail.com says...
----------------------

Threads on NSK are user level threads, not OS level threads.

If a thread does not wait on something or give up control
the pthread lib cannot do a context switch.

If possible use the T1280 threads package, ZPUTDLL


/*

# T1248 threads
defines=" -D_PTH_TIMESPEC_DECLARED -D_SPT_MODEL_"
options="-Wextensions "
libs="-lZSPTDLL"
cc sample.c -o sample $options $libs $defines


# T1280 threads
defines="-D_PTH_TIMESPEC_DECLARED -D_PUT_MODEL_"
options="-Wextensions"
libs="-lZPUTDLL"
cc sample.c -o sample $options $libs $defines

*/


#include <stdio.h>

#ifdef _SPT_MODEL_
#include <spthread.h>
#else
#include <pthread.h>
#endif

pthread_t threadid;
pthread_attr_t pta;

void *donothing(void)
{
fprintf(stdout,"KKKKKK\n");
fflush(stdout);
}

int main(int argc, char *argv[])
{
int i,rc;
int threadNumber=0;

pthread_attr_init(&pta);
pthread_attr_setdetachstate(&pta,PTHREAD_CREATE_JOINABLE);
fprintf(stdout,"Creating..\n");
fflush(stdout);
rc = pthread_create(&threadid, &pta, donothing, NULL);

while(1)
{
sched_yield();
}
}

Randall

unread,
Jan 24, 2013, 10:58:56 AM1/24/13
to
You will need #define _PUT_MODEL_ instead of _SPT_MODEL_ to use the PUT library.

You can test whether the thread is launched as described here. Otherwise while(-1){} is a hard loop that will drop your process to priority 1. You can also do a pthread_join instead, to verify that the thread executed. However, the main thread must give up control at least once via any method that is thread aware. This includes many of the procs in stdio.

Anupam Das

unread,
Jan 24, 2013, 10:50:57 PM1/24/13
to kd...@acm.org
>> run -debug ./something.exe <<

Yes Keith :-). I actually didn't put much of my attention on this path thing.

Anupam Das

unread,
Jan 24, 2013, 11:08:40 PM1/24/13
to
>>
Threads on NSK are user level threads, not OS level threads.

If a thread does not wait on something or give up control the pthread lib cannot do a context switch.

<<

JShepherd popped up an interesting topic which I thought I will elaborate further. Threads on NSK are indeed all user level threads and there is no concept of kernel threads in NSK like other SMP machines. That said, it is more important to note that the NSK OS can only schedule a process and not a thread during execution. Rephrasing what I said just now - in NSK, unlike Linux, the scheduler can't schedule a thread towards the CPU for execution. Context Switch, therefore, means the entire-process-context-switch, which is much costlier task, than what other *nix platforms can do in multi-threaded mode. For them, context switching does not mean the global data area is switched.

Below is the print of a debug session that is done using Unix emulation on Windows where you could easily see and *compare* with our eInspect, that an execution in those SMP machines actually starts with thrad-mode rather than as a process mode.

/home/Anupam Das>gdb zz2.exe
GNU gdb (GDB) 7.5.50.20120815-cvs (cygwin-special)
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-cygwin".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/Anupam Das/zz2.exe...done.
(gdb) b main
Breakpoint 1 at 0x4010ba: file zz2.c, line 9.
(gdb) info thread
No threads.
(gdb) run
Starting program: /home/Anupam Das/zz2.exe
[New Thread 5940.0xe6c]
[New Thread 5940.0x1444]
[New Thread 5940.0x1d8]

Breakpoint 1, main () at zz2.c:9
9 f = fopen ("not_here", "r");
(gdb) l
4
5 int main()
6 {
7 FILE *f;
8
9 f = fopen ("not_here", "r");
10 if (!f)
11 syslog (LOG_ERR|LOG_USER, "Invalid file name");
12 return 0;
13 }
(gdb) info thread
Id Target Id Frame
3 Thread 5940.0x1d8 0x7c90e514 in ntdll!LdrAccessResource ()
from /cygdrive/c/WINDOWS/system32/ntdll.dll
2 Thread 5940.0x1444 0x7c90e514 in ntdll!LdrAccessResource ()
from /cygdrive/c/WINDOWS/system32/ntdll.dll
* 1 Thread 5940.0xe6c main () at zz2.c:9
(gdb) info dll
From To Syms Read Shared Object Library
0x7c901000 0x7c9b1ee0 Yes (*) /cygdrive/c/WINDOWS/system32/ntdll.dll
0x7c801000 0x7c8f5c8c Yes (*) /cygdrive/c/WINDOWS/system32/kernel32.dll
0x10001000 0x1004b726 Yes (*) /cygdrive/c/Documents and Settings/All Users/Application Data/Norton/{0C55C096-0F1D-4F28-AAA2-85EF591126E7}/NAV_20.1.1.2/Definitions/BASHDefs/20130116.013/UMEngx86.dll
0x61001000 0x61480000 Yes (*) /usr/bin/cygwin1.dll
(*): Shared library is missing debugging information.

As you could see, since beginning, there were three threads running distinctly in USER-MODE which can be switched if needed by the scheduler. Now run a similar program in NSK using eInspect debugger, you won't see any threads there. Well, it would be wrong to say no threads, technically speaking there is just one main thread. And what our NSK Pthread library does, in my opinion, are all logical threads who do not have any physical existence like I shown just now.

0 new messages