no more thread scheduling when waitpid in one thread
nadine peyrouze <[email protected]>
| Newsgroups | gmane.linux.ngpt.devel |
|---|---|
| Organization | Evidian |
| Message-ID | <[email protected]> |
Hello,
Running ngpt 2.0.4 on redhat linux kernel 2.4.19.
A simple program with 2 threads :
- one thread print "="
- one thread, forks and runs waitpid on the child
the child forked print "."
- the main thread wait for joining with the above 2 threads
Running this program, I have the ouput :
...............
and then
======
Should have mixed . and =
Do you know this problem and how to fix it ?
Thanks.
Below is code used to test.
#ifdef GLOBAL
#include <pthread.h>
#else
#define _PTHREAD_PRIVATE
#include "pthread.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <linux/unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define die(str) \
do { \
fprintf(stderr, "**die: %s: errno=%d\n", str, errno); \
exit(1); \
} while (0)
static void *child_print(void *_arg)
{
char *name = (char *)_arg;
int i=0;
fprintf(stderr, "child_print: startup %s \n", name);
for (i = 0; i < 50; i++) {
if (i % 5 == 0)
sleep(1);
fprintf(stderr, "=");
}
fprintf(stderr,"\n");
fprintf(stderr, "child_print: shutdown %s \n", name);
// _exit(0); /* the process will exit */
return _arg;
}
static void *child_fork(void *_arg)
{
char *name = (char *)_arg;
int pid;
fprintf(stderr, "child_fork: startup %s \n", name);
pid = fork();
if (pid == 0) {
/* we are in child */
int i=0;
for (i = 0; i < 50; i++) {
if (i % 5 == 0)
sleep(1);
fprintf(stderr, ".");
}
fprintf(stderr,"\n");
}
else {
int res=0;
int status=0;
fprintf(stderr,"child_fork: waitpid\n");
res = waitpid(pid, &status, 0);
fprintf(stderr, "child_fork: shutdown %s \n", name);
}
// _exit(0); /* the process will exit */
return _arg;
}
int main(int argc, char *argv[])
{
pthread_attr_t thread_attr;
pthread_t thread[4];
char *rc;
fprintf(stderr, "main: init\n");
fprintf(stderr, "main: initializing attribute object\n");
if (pthread_attr_init(&thread_attr) != 0)
die("pthread_attr_init");
if (pthread_attr_setdetachstate(&thread_attr,
PTHREAD_CREATE_JOINABLE) != 0)
die("pthread_attr_setdetachstate");
if (argc > 1) {
/* create as bound-thread (as 1:1 mode) */
if (pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM) != 0)
die("pthread_attr_setscope");
}
fprintf(stderr, "main: create child_print\n");
if (pthread_create(&thread[0], &thread_attr, child_print, (void
*)"child_print") != 0)
die("pthread_create");
fprintf(stderr, "main: create child_fork\n");
if (pthread_create(&thread[1], &thread_attr, child_fork, (void
*)"child_fork") != 0)
die("pthread_create");
sleep(5);
fprintf(stderr, "main: joining...\n");
if (pthread_join(thread[0], (void **)&rc) != 0)
die("pthread_join");
fprintf(stderr, "main: joined thread: %s\n", rc);
if (pthread_join(thread[1], (void **)&rc) != 0)
die("pthread_join");
fprintf(stderr, "main: joined thread: %s\n", rc);
fprintf(stderr, "main: exit\n");
exit(0);
}