Reputation: 21
I have written quite a lot of threaded code on HP-UX and even SUSE and that works perfectly. But it does not work on Red Hat. This is my machine:
Linux version 3.10.0-1062.18.1.el7.x86_64 (Red Hat 4.8.5-39)
Red_Hat_Enterprise_Linux-Release_Notes-7-en-US-7-2.el7.noarch
redhat-release-server-7.7-10.el7.x86_64
I wrote a simple test program, thr_ex.c:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void *funny(void *);
void *funny(s)
void *s;
{
int fd;
fd = creat("/tmp/funny_func", 0600);
write(fd, s, strlen((char *) s));
close(fd);
}
int main()
{
int return_value;
pthread_t thread_id;
pthread_attr_t thread_attr;
pthread_attr_init(&thread_attr);
pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM);
pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
return_value = pthread_create(&thread_id, &thread_attr, funny, (void *) "Here I am\n");
printf("Return value == %d\n", return_value);
printf("Thread id == %hu\n", thread_id);
exit(0);
} /* End main. */
Compiling, building:
gcc -pthread -s -o thr_ex thr_ex.c
Running:
./thr_ex
Return value == 0
Thread id == 5888
But no file gets created under /tmp. strace -f shows no creat() or write() ( except from the printf's in main () ).
However, strace -f do show, for example: strace: Exit of unknown pid 64574 ignored
I have tried even simpler code where the thread only runs a printf() and a fflush(), with no thread attributes and no argument to the function. Still nothing happens.
Upvotes: 2
Views: 167
Reputation: 311088
Insert before the return statement or exit( 0 ) statement in main
pthread_exit( NULL );
Otherwise the created thread can have no time to be executed because the process will end.
Upvotes: 2