why isnt the code after pthread_join() executed? - c

I am writing a simple tcp client for an IRC server that I have. So Im implementing this by having a writer thread and a receiver thread, however after the user quits the session, the program stops all execution after the thread has been stopped and just presents a scanf()-like output in the console.
void senderThread(void* context){
for(;;){
//setup here
//gets user input and stores into char* msg
if(strcmp(msg, "quit") == 0){
printf("quitting...\n");
free(msg);
shutdown(clientdata->sockfd, SHUT_RDWR);
return NULL;
}else {
write(clientdata->sockfd, msg, strlen(msg));
write(clientdata->sockfd, "\n", strlen("\n"));
free(msg);
}
}
return NULL;
}
int main(int argc, char* argv[]){
//setup stuff here
pthread_t thread_id[1];
pthread_create(&thread_id[1], NULL, senderThread, (void *) &clientdata);
printf("creating threads...\n");
pthread_join(thread_id, NULL); //for(;;) loop here
shutdown(sockfd, SHUT_RDWR);
free(username);
return 0;
}
this is a part of the code for the program, however when I type 'quit' into the stdin, nothing happens. the printf and the shutdown both fire, but then I am left just left with the terminal expecting input and the program not actually closing. none of the code after the pthread_join() is executed. Why is this?

&thread_id[1] is a pointer to the second element in the one-element array. That will be out of bounds and lead to undefined behavior.
You then call pthread_join passing a pointer to the first and uninitialized element in the array. Again leading to undefined behavior.
I suggest you use a single pthread_t value (instead of an array), and use the address-of operator in both the pthread_create and the pthread_join calls:
pthread_t thread_id;
pthread_create(&thread_id, ...);
pthread_join(&thread_id, NULL);

Related

Simple pthread prog: segmentation fault

Trying to see how pthread works by running a simple program but I am getting segmentation fault (core dumped) at pthread_create
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void* testfunc(void* arg) {
while (1) {
printf("testfunc");
}
}
int main(void) {
printf("helo\n");
if (pthread_create(NULL, NULL, &testfunc, NULL) != 0) {
perror("pthread failed to create\n");
}
while (1) {
printf("main function\n");
sleep(1000);
}
return 0;
}
What seems to be causing the problem? I am on Ubuntu 20.04 if that matters.
You can't pass NULL for pthread_create's first argument.
Before returning, a successful call to pthread_create() stores the ID of the new thread in the buffer pointed to by thread
Also, pthread_create doesn't set errno, so using perror makes no sense, at least not without some prep.
on error, it returns an error number, and the contents of *thread are undefined.
Fixed:
pthread_t thread;
if ( ( errno = pthread_create(&thread, NULL, &testfunc, NULL) ) != 0 ) {
perror("pthread failed to create\n");
}
...
pthread_join(thread, ...); // Normally.
Threads in c are very unforgiving. There are a few problems with your code that I can see.
First you might want to refer to the developer docs for p_thread. They are very well documented. What you currently have is a thread call but you are not pointing anything to that thread. This is why you are receiving the segmentation error. Meaning your program lost the pointer to that thread somewhere when it tried calling it. I suggest something like.
pthread_t thread;
int * argument = 5;
if(pthread_create(&thread,NULL, &testfunc, &argument) !=0){
// ^This is a pointer to your argument
// that you want to pass in
perror("pthread failed to create\n");
exit(1);
}
and your thread function will also need to be typecast from a void pointer into whatever you want it to return to work with. Then it needs to be cast back to a void pointer before is returned from the thread routine.
void* testfunc(void* arg){
int* testVar = (int *)arg;
// do some logic here
return (void *) testVar;
}
lastly you are responsible for your memory in C so you must kill the thread you created before exiting.
pthread_join(thread, NULL);
My number one suggestion is you watch some videos relating to it.

How to use C popen in a pthread

First, I've been reading some comments in forums about it's recommended to avoid using fork in threads, and popen does fork.
Anyway, I'm doing a program with some threads and it'll be very useful for me to using other library functions which execute popen.
When I do that, program exits.
Let me put a simple example because the code is large:
int main()
{
pthread_t thread1, thread2;
int var=1;
pthread_create(&thread1, NULL, mythread, &var);
pthread_create(&thread2, NULL, anotherthread, &var);
...
}
void *mythread(void *s)
{
...
pthread_detach(pthread_self());
...
printf("this is printed\n");
char *str = externalFunctWithPopen();
printf("this is NEVER printed\n");
...
}
char *externalFunctWithPopen()
{
...
printf("this is also printed\n");
popf = popen(command, "r");
printf("this is not printed at all\n");
while (fgets(buf, sizeof(buf), popf) != 0) {
...
}
As I told before, "this is NEVER printed" is never printed, and furthermore, main exits, including the other thread called anotherthread
Any piece of help is welcome.
main exits, including the other thread
To avoid the behaviour that leaving main() tears down all other threads of the same process. leave it via a call to pthread_exit() instead of doing exit() or just return.

Main() thread id is different after creating another thread

I've been reading and learning about POSIX threads, and tried to write some simple codes to understand it better.
#include <stdio.h> /* standard I/O routines */
#include <pthread.h> /* pthread functions and data structures */
/* function to be executed by the new thread */
void* PrintHello(void* data)
{
pthread_t tid = (pthread_t)data;
printf("Hello from new thread %d - got %d\n", pthread_self(), tid);
pthread_exit(NULL); /* terminate the thread */
}
int main(int argc, char* argv[])
{
int rc; /* return value */
pthread_t thread_id;
int tid;
thread_id = pthread_self();
printf("FLAG = %d ", thread_id);
/* create a new thread that will execute 'PrintHello' */
rc = pthread_create(&thread_id, NULL, PrintHello, (void*)thread_id);
if(rc) /* could not create thread */
{
printf("\n ERROR: return code from pthread_create is %u \n", rc);
exit(1);
}
printf("\n Created new thread (%d) ... \n", thread_id);
pthread_exit(NULL); /* terminate the thread */
}
For this code I get the following output:
FLAG = 363480832
Created new thread (355198720) ...
Hello from new thread 355198720 - got 363480832
What is bothering me is why thread_id which is 363480832, becomes 355198720, same as thread_id of a function that was called from main (PrintHello()). I assumed that thread id doesn't change throughout the program execution. Or is it something inside the function that changes it?
If you're doing anything with a pthread_t other than passing it to a function that takes one, you're doing something wrong. Only the pthreads API knows how to use a pthread_t correctly. They can have any internal structure that's convenient for the implementation.
Being a C language construct, pthread_t behaves more like char *. The necessary language constructs to make it behave like std::string don't exist. So you have to treat it like char *.
A char * contains a string somehow, but you have to understand its implementation to get that value out. Consider:
char *j = "hello";
char *k = strdup (j);
if (j == k)
printf ("This won't happen\n");
printf ("%d\n", j);
printf ("%d\n", k); // these won't be equal
You can't compare char *'s with == to see if they refer to the same string. And if you print out j and k, you'll get different values.
Similarly, a pthread_t refers to one particular thread somehow. But you have to understand how to get the value out. Two pthread_ts can have different apparent values but still refer to the same thread just as two char *s can have different apparent values but still refer to the same string.
Just as you compare two char *'s with strcmp if you want to tell if they refer to the same string value, you compare two pthread_ts with pthread_equal to tell if they refer to the same thread.
So this line of code makes no sense:
printf("FLAG = %d ", thread_id);
A pthread_t is not an integer and you can't print it with a %d format specifier. POSIX has no printable thread IDs. If you want one, you need to code one (perhaps using pthread_getspecific).
In C, arguments are passed by value. In particular, the argument (void *)thread_id is an expression that's evaluated before calling pthread_create, so the fact that pthread_create writes to thread_id as a result of &thread_id being passed as the first argument is irrelevant. If you were instead passing &thread_id rather than (void *)thread_id as the argument to the new thread start function, and dereferencing it there, then you may see the effects you want; however, it's not clear to me that pthread_create's writing of the new thread id via its first argument is required to take place before the new thread starts, so there is probably a data race if you do it that way.
Further, note that David's answer is also correct. It's invalid to print thread ids this way since they are formally an opaque type.
In this line:
rc = pthread_create(&thread_id, NULL, PrintHello, (void*)thread_id);
as the manual says, pthread_create() shall store the ID of the created thread in the location referenced by thread_id. In this example, it would be modified to 355198720, which is the tid of new thread PrintHello().
Besides, it may be better to change the argument for PrintHello to:
rc = pthread_create(&thread_id, NULL, PrintHello, (void*)&thread_id);
and in PrintHello(), it would be:
void* PrintHello(void* data)
{
pthread_t tid = (pthread_t)(*data);
...
}

POSIX parent/child threads not sharing data

One final question for the evening. I have a homework question that I cannot seem to solve, and after spending the better part of the night on it, I'll ask it here.
The problem is a program that is to calculate the Fibonacci sequence given an integer input at the command line (./foo.out 15, for example, and it will calculate the first 15 Fibonacci numbers). The additional parameters are as follows:
1.) It must use two POSIX threads, a parent thread and a child thread.
2.) It must share data (an array) between them.
Currently, the function is breaking when program control passes back to the parent thread. All I am getting is a completely non-descriptive segfault. As can be seen below, I have it outputting at every possible change in control.
Code:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
void *fibonacci(void *param, int numbers[]);
void *runner(void *param);
int main(int argc, char *argv[])
{
pthread_t tid;
pthread_attr_t attr;
if ( argc != 2)
{
fprintf(stderr, "Usage: 426.out <integer value> \n");
return -1;
}
if (atoi(argv[1]) < 0)
{
fprintf(stderr, "Argument must be non-negative\n");
return -2;
}
pthread_attr_init(&attr);
pthread_create(&tid, &attr, runner, argv[1]);
pthread_join(tid,NULL);
return 0;
}
void *fibonacci(void *param, int numbers[])
{
int it, IT_MAX;
printf("Entering Child Thread:\n");
IT_MAX = atoi(param);
numbers[0] = 0;
numbers[1] = 1;
for (it = 2; it < IT_MAX; ++it)
{
numbers[it] = (numbers[it - 1] + numbers[it - 2]);
}
for (it = 0; it < IT_MAX; ++it)
{
printf("%d\n", numbers[it]);
}
printf("Exiting Child Function.\n");
}
void *runner(void *param)
{
int it, IT_MAX;
int numbers[IT_MAX];
pthread_t tid;
pthread_attr_t attr;
printf("Entering Parent Thread:\n");
pthread_attr_init(&attr);
pthread_create(&tid, &attr, fibonacci(param, numbers), NULL);
pthread_join(tid, NULL);
IT_MAX = atoi(param);
for (it = 0; it < IT_MAX; it++)
{
printf("%d\n", numbers[it]); // I suspect the program breaks here
// It produces a segfault rather than this
}
printf("Leaving Parent Thread\n");
}
The question I have, if it is not clear from above, is what am I doing wrong. I am using gcc, and have been using the -Wall -Werror and -lpthread. -Werror keeps throwing "Control Structure going to end of non-void function", but nothing has anything to say about what can be causing the segfault. As before, I looked for the last hour for a question or article that addresses this, so if this is a dupe, please point me to the question or article in which it is addressed. Thanks to anyone in advance for their help.
One of your problems is in this line:
pthread_create(&tid, &attr, fibonacci(param, numbers), NULL);
Instead of starting a thread with the fibonacci() function, this calls the fibbonacci() function in the current thread and passes the result to pthread_create() as the thread start function. fibonacci() doesn't return a function pointer, so this new thread will crash the program.
You need to just pass fibonacci as the third parameter.
Futhermore, fibonacci() must be declared as void *fibonacci(void *) in order to be used as a thread start function. If you want to pass two parameters, you'll need to place them into a struct and pass a pointer to that.
This pair of lines is also problematic:
int it, IT_MAX;
int numbers[IT_MAX];
IT_MAX is an uninitialised variable, and you use it here to define the size of the numbers[] array. That could also be leading to your crash.
This line
pthread_create(&tid, &attr, fibonacci(param, numbers), NULL);
does not pass the address of fibonacci() to pthread_create() but calls fibonacci(param, numbers)and passes the result returned to pthread_create() as 3rd argument.

pthread array of pointers

I have a struck with an array of pthread pointers. Each thread is meant to read a different data stream
typedef struct {
// ...other stuff
pthread_t *threads[MAX_STREAM_COUNT];
} stream_manager;
And when I want to start reading:
void start_reading(stream_manager *sm, int i) {
// do some pre processing stuff
pthread_create( (pthread*) &sm->threads[i],
NULL,
read_stream_cb,
(void*) sm->streams[i] );
}
When I want to stop reading:
void stop_reading(stream_manager *sm, int i) {
iret = pthread_join(*sm->threads[i], NULL);
if(iret != 0) {
perror("pthread_join returned with error:: ");
printf( "pthread_join returned with error:: %s\n", strerror(iret) )
}
}
For now, my read_stream_cb function simply prints the name of the stream its reading from.
void* read_stream_cb(stream *strm) {
stream *s = (stream*) strm;
prinf("%s\n", s->name);
}
In the main program, I initialize 2 streams with different names. I call run start_reading(), sleep(3) and stop_reading()). The program prints the stream names fine for 3 seconds, but the pthread_join does not return successfully. It returns 3 and prints out
pthread join error: Operation timed out
pthread_join returned with errer:: No such process
I think this may be a pointer issue? I may just be confused with order of operations with these pointers in the join pthread_join(*sm->streams[i], NULL); . I'll look into that more.
pthread_create() takes a pthread_t* as its argument, this is passing a pthread_t**:
pthread_create( (pthread*) &sm->threads[i],
as sm->threads is an array of pthread_t*. Change stream_manager to:
typedef struct {
// ...other stuff
pthread_t threads[MAX_STREAM_COUNT]; /* No longer array of pointers. */
} stream_manager;
and remove the unnecessary cast from the call to pthread_create().
pthread_join() takes a pthread_t:
pthread_join(sm->threads[i]); /* Not sm->streams[i]. */

Resources