This is my code, I compiled it with:
gcc thread.c -lpthread
It didn't print any error or warning. But when I run it, the program doesn't print anything.
Platform: Ubuntu 11.10 64 bit gcc 4.6.1
Exit status :0
When I debug it, I found it prints hello as I expected.
This is my code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *loopPrint(void *ptr)
{
char *p = (char *)ptr;
while (1)
{
printf("%s\n", p);
}
}
void *pClock(void *ptr)
{
sleep(3);
exit(0);
}
int main()
{
pthread_t showMsg, clock;
int main_pth, wait_pth;
char *msg = "Hello";
main_pth = pthread_create(&showMsg, NULL, loopPrint, (void *)msg);
wait_pth = pthread_create(&clock, NULL, pClock, NULL);
pthread_join(main_pth, NULL);
pthread_join(wait_pth, NULL);
return 0;
}
pthread_join(main_pth, NULL);
This is wrong, pthread_join takes a pthread_t as an argument. Replace with:
pthread_join(showMsg, NULL);
(Same for the other one.)
And do check the return values of these calls, they can and do fail.
And BTW, you're missing #include <unistd.h> for the sleep call.
main_pth etc is the error return of pthread_create and not the thread id. Wait ("join") for showMsg and clock.
Related
I am recently learning about threads in C, and I have noticed something I consider weird.
Let's take the next code:
#include <stdio.h>
#include <pthread.h>
void *sub_routine(void *p)
{
p = NULL;
printf("This is a sub_routine\n");
return (NULL);
}
int main(int argc, char **argv)
{
void *p;
pthread_t thread;
if (argc < 1)
return (0);
p = argv[1];
pthread_create(&thread, NULL, sub_routine, NULL);
sub_routine(p);
return (0);
}
I use this command line to compile my program:
gcc -Wall -Wextra -Werror -pthread pthreads.c
The expected result is to print This is a sub_routine two times. Well that happens but not 100% of the times. Is there any particular reason for that?
When main returns, it exits the program even if other threads are running. Therefore, it’s possible for the thread you spawned to not get to the printf before the main thread returns, in which case the program ends before you’ll see both messages.
One way to address this is to add a call to pthread_join at the end of main to tell the program to wait for the thread you created to finish running before main returns. That will ensure you always see two printouts.
Add pthread_join(thread, NULL) at the end (just before return) of the main thread to join the thread "sub_routine"
The modified version of your code works properly:
#include <stdio.h>
#include <pthread.h>
void *sub_routine(void *p)
{
p = NULL;
printf("This is a sub_routine\n");
return (NULL);
}
int main(int argc, char **argv)
{
void *p;
pthread_t thread;
if (argc < 1)
return (0);
p = argv[1];
pthread_create(&thread, NULL, sub_routine, NULL);
sub_routine(p);
pthread_join(thread, NULL);
return (0);
}
I have this assignment for Operating Systems, the code won't run in MAC terminal, I keep getting an issue that says, "Image". Is there any help I can get on this issue?
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
// These two functions will run concurrently
void* print_i(void *ptr)
{
printf("I am in i\n");
}
void* print_j(void *ptr)
{
printf("I am in j\n");
}
int main()
{
pthread_t t1,t2;
int rc1 = pthread_create(&t1, NULL, print_i, NULL);
int rc2 = pthread_create(&t2, NULL, print_j, NULL);
exit(0);
}
The keyword void (not a pointer) means "nothing" in C. This is consistent.
As you noted, void* means "pointer to anything" in languages that support raw pointers (C and C++). This means you need to return something.
This is the reason why you get this type of warning.
If you return someting, like return 0; warnings disapear.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
// These two functions will run concurrently
void* print_i(void *ptr)
{
printf("I am in i\n");
return 0;
}
void* print_j(void *ptr)
{
printf("I am in j\n");
return 0;
}
int main()
{
pthread_t t1,t2;
int rc1 = pthread_create(&t1, NULL, print_i, NULL);
int rc2 = pthread_create(&t2, NULL, print_j, NULL);
exit(0);
}
You can learn more about it here:
https://linux.die.net/man/3/pthread_create
I've been trying to create a chat program in C with threads but it hasn't been working, so I decided to play with threads for a bit first. I'm trying to run a thread that prints "hello world", but it gives me a segmentation fault. I haven't been able to find the root of the problem so I came here. heres the code:
#include <stdio.h>
#include <pthread.h>
void* test(void * arg) {
printf("hello world\n");
return NULL;
}
int main() {
pthread_t test;
pthread_create(&test, NULL, (void *) test, NULL);
pthread_exit(NULL);
return 0;
}
There's probably a stupid reason why it's not working so I hope you guys wont have too much trouble finding it!
Your start routine name and thread id name are the same so I think compiler gets confused when you pass &test. Your code works by changing the thread id name.
#include <stdio.h>
#include <pthread.h>
void* test(void * arg) {
printf("hello world\n");
return NULL;
}
int main() {
pthread_t t;
pthread_create(&t, NULL, (void *) test, NULL);
pthread_exit(NULL);
return 0;
}
void* test(void * arg) { ... }
// vvvv
pthread_t test;
pthread_create(&test, NULL, (void *) test, NULL);
// ^^^^
That pthread_t variable is "hiding" the function name. In other words, you're calling some arbitrary uninitialised value as your function. That's unlikely to end well :-)
What you're doing is really no different to expecting the following program to output 7 (it won't):
#include <stdio.h>
int i = 7;
int main(void) {
int i = 42;
printf("%d\n", i);
return 0;
}
You could solve it simply by renaming your function to testFn, for example.
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.
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.
This is for an assignment, but right now I'm just playing around with a code I found in a tutorial, but I can't seem to get it to compile, and I don't understand the error I receive. My code is below:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *myfunc (void *myvar);
int main( int argc, char *argv[])
{
pthread_t thread1;
pthread_t thread2;
char *msg1 = "First thread";
char *msg2 = "Second thread";
int ret1;
int ret2;
//create threads
ret1 = pthread_create(&thread1, NULL, myfunc, (void*) msg1);
ret2 = pthread_create(&thread2, NULL, myfunc, (void*) msg2);
printf("Main function after pthread_create\n");
//join threads back to main process once completed
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("Return thread ret1 = %d\n", ret1);
printf("Return thread ret2 = %d\n", ret2);
return 0;
}
void *myfunc(void *myvar)
{
char *msg;
msg = (char *) myvar;
int i;
for (i = 0; i < 10; i++) {
printf("%s%d\n", msg, i);
sleep(1);
}
return NULL;
}
I compiled using gcc -o c_thread c_thread.c
and received the error:
/usr/lib/gcc/x86_64-linux-gnu/5/.../.../.../x84_64-linux-gnu/ctr1.o: In function '_start':
/build/buildd/glibc-2.21/csu/.../sysdeps/x86_64/start.S:114: undefined reference to 'main'
collect2: error: ld returned 1 exit status
I know other questions have been asked about similar errors, but every answer I found related to a code which used a 'nontraditional' main function, whereas mine follows the standard int main(int argc char *argv[]) format
Any suggestions would be greatly appreciated
Your missing the -lpthread when compiling
Try compiling as following via terminal or adjusting the settings from the linker if you're working in an IDE:
gcc pro.c -o pro -lpthread
Suppose pro.c was your program file.