I am designing a C program for multi-threaded web server. I want to accept all the request on one thread and process them using threadpool on 2nd thread.
I am having hard time while making both the threads run parallel. I am attaching schema of my code.
pthread_t entry, scheduler, temp;
pthread_create(&entry, NULL, (void *)&listen, (void *)server_sockfd);
pthread_create(&scheduler, NULL, (void *)&scheduler, (void *)server_sockfd);
pthread_join (entry, NULL);
pthread_join (scheduler, NULL);
My 1st thread listen will accept all the request from client and put it in a queue and 2nd thread will schedule them.
My problem is: My first thread is running all the time and my 2nd thread never runs. 1st thread has socket function of accept() system call which is used in While loop so that it will listening all the time.
Please let me know how can I run both the threads in parallel.
Thank You.
Related
I am writing a simple multi-client server communication program using POSIX threads in C. I am creating a thread every time a new client is connected, i.e. after the accept(...) routine in main().
I have put the accept(...) and the pthread_create(...) inside a while(1) loop, so that server continues to accept clients forever. Now, where should I write the pthread_join(...) routine after a thread exits.
More Info: Inside the thread's "start routine", I have used poll() & then recv() functions, again inside a while(1) loop to continuously poll for availability of client and receive the data from client, respectively. The thread exits in following cases:
1) Either poll() returns some error event or client hangs up.
2) recv() returns a value <= 0.
Language: C
Platform: Suse Linux Enterprise Server 10.3 (x86_64)
First up starting a new thread for each client is probably wasteful and surely won't scale very far. You should try a design where a thread handles more than one client (i.e. calls poll on more than one socket). Indeed, that's what poll(2), epoll etc were designed for.
That being said, in this design you likely needn't join the threads at all. You're not mentioning any reason why the main thread would need information from a thread that finished. Put another way, there's no need for joining.
Just set them as "detached" (pthread_detach or pthread_attr_setdetachstate) and they will be cleaned up automatically when their function returns.
The problem is that pthread_join blocks the calling thread until the thread exits. This means you can't really call it and hope the thread have exited as then the main thread will not be able to do anything else until the thread have exited.
One solution is that each child thread have a flag that is polled by the main thread, and the child thread set that flag just before exiting. When the main thread notices the flag being set, it can join the child thread.
Another possible solution, is if you have e.g. pthread_tryjoin_np (which you should have since you're on a Linux system). Then the main thread in its loop can simply try to join all the child threads in a non-blocking way.
Yet another solution may be to detach the child threads. Then they will run by themselves and do not need to be joined.
Ah, the ol' clean shutdown problem.
Assuming that you may want to cleanly disconnect the server from all clients under some circumstance or other, your main thread will have to tell the client threads that they're to disconnect. So how could this be done?
One way would be to have a pipe (one per client thread) between the main thread and client thread. The client thread includes the file descriptor for that pipe in its call to poll(). That way the main thread can easily send a command to the client thread, telling it to terminate. The client thread reads the command when poll() tells it that the pipe has become ready for reading.
So your main thread can then send some sort of command through the pipe to the client thread and then call pthread_join() waiting for the client thread to tidy itself up and terminate.
Similarly another pipe (again one per client thread) can be used by the client thread to send information to the main thread. Instead of being stuck in a call to accept(), the main thread can be using poll() to wait for a new client connection and for messages from the existing client threads. A timeout on poll() also allows the main thread to do something periodically.
Welcome to the world of the actor model of concurrent programming.
Of course, if you don't need a clean shut down then you can just let threads terminate as and when they want to, and just ctrl c the program to close it...
As other people have said getting the balance of work per thread is important for efficient scaling.
I have a socket programming problem. I am running the server and then it waits for the client. Once I run the client though, nothing happens, it just terminates and brings back the prompt. Basically it compiles alright but it doesn't run at all. It terminates as soon as I run it. This only happens when I use threads in the client code.
This is the code I'm using:
if(pthread_create(&threadID[i++], NULL, (void *)dostuff, (void *)(intptr_t)sock) != 0)
{
perror("Thread create error");
}
On the other hand, if I type in simply
dostuff(sock);
The client program does execute. I need threading because I need to implement I/O multiplexing. Could you tell me how to stop the client from terminating when I use threads?
You'll need to wait for the thread to finish before exiting the program, for example using pthread_join
// do this before returning from main
pthread_join(threadID[i], NULL);
I have a linux kernel module, I had to implement a TCP server and multiple clients to send some traffic. I had a question,
Now at the server end, I have a main-kthread which spawns another kthread once it sees a TCP connection waiting to be Accepted. Now after the main kthread spawns say a couple of kthreads how can I put the main-kthread in wait state to make sure the other kthreads exit before the main-kthread can exit.
I am looking something similar to phtread-join semantics, but i need this in the Linux kernel space.
I could not find kthread_join as such, I am looking at alternatives. I did read about waitqueues. Hence was looking at interruptible_sleep_on() and wake_up().
But am unable to figure out how to use these API's.
For example,
In the main-thread, I do a interruptible_sleep_on, then who should wake him up? All the other threads, or the last thread? how do i figure who is the last thread exiting? Also how to make sure, the other threads have exited when this main-kthread is woken up.
Wait queues are the right tools. There is a good description in Linux Device Drivers (LDD) chapter 6. Here's a sketch of how your code might look like:
Maintain a counter of server threads.
Increment the counter just before spawning a server thread.
When the server thread exists, decrease the counter.
In the main thread, when exiting, wait on a wait queue until the counter value is 0.
In the server threads, just after decreasing the counter, notify the main thread by signaling on the wait queue.
Warning: untested code, this is just a skeleton.
struct global_data {
wait_queue_head_t wq;
atomic_t thread_count;
};
main_thread () {
global_data gd = {0};
init_wait_queue_head(&gd.wq);
while (serving) {
…
atomic_inc(&server_threads);
if (IS_ERR_OR_NULL(kthread_create(server_thread, &gd)) {
atomic_dec(&server_threads);
}
}
wait_event_interruptible(&gd.wq, atomic_read(gd.thread_count) == 0);
/* final cleanup */
}
server_thread (global_data *gd) {
…
atomic_dec(&gd->server_threads);
wake_up(&gd->wq);
}
I have a parallel C program running on a shared memory architecture using pthreads. How do I kill a thread without killing the whole process? I.e. if one thread finishes first, I want to wait for all the others before exiting. exit(status_code) kills the whole process which is not what I want. I could have a barrier, wait for all threads to reach the barrier and then exit but there might be an easier way?
Thank you.
Use pthread_join in the "main" (driver) thread to wait for all the other ones to terminate.
To clarify what "a thread terminates" above means, look at the pthread_exit manpage says:
Performing a return from the start function of any thread other than the main thread results in an implicit call to pthread_exit(), using the function's return value as the thread's exit status.
So pthread_join waits for the joined thread to either return from it start function or call pthread_exit.
(You do need to keep track of all the threads you created to use this. And none of these work if you detached the processing threads - you should probably not be doing that in this case anyway.)
You can use pthread_join() function to join all of your threads.
As an example, lets say you created 5 threads and you want to contunie your operation from main function when all of the threads are finished.
pthread_t threadId[5]; // holder for 5 thread
// Create 5 threads
for(index = 0; index < 5; index++)
pthread_create(&(threadId[index]), NULL, producer, (void *) &(argsForProducers[index]));
// Join them
for (index = 0; index < 5; index++)
pthread_join(threadId[index], NULL);
You can call pthread_exit() to end the thread yo are in. If you want to kill a thread from within another thread, you can call pthread_cancel(threadToKill). To synchronize alll threads you can call pthread_join().
You want to use a join operation for your application to wait on the threads. Take a look at the pthread man page for a good overview.
I've been using the pthread library for creating & joining threads in C.
When should I create a thread as detached, right from the outset? Does it offer any performance advantage vs. a joinable thread?
Is it legal to not do a pthread_join() on a joinable (by default) thread? Or should such a thread always use the detach() function before pthread_exit()ing?
Create a detached thread when you know you won't want to wait for it with pthread_join(). The only performance benefit is that when a detached thread terminates, its resources can be released immediately instead of having to wait for the thread to be joined before the resources can be released.
It is 'legal' not to join a joinable thread; but it is not usually advisable because (as previously noted) the resources won't be released until the thread is joined, so they'll remain tied up indefinitely (until the program exits) if you don't join it.
When should I create a thread as detached, right from the outset?
Whenever the application doesn't care when that thread completes and doesn't care about its return value of a thread, either (a thread may communicate a value back to other thread/application via pthread_exit).
For example, in a client-server application model, a server may create a new thread to process each request. But the server itself doesn't care about thread's return value of the thread. In that case, it makes sense to created detached threads.
The only thing the server needs to ensure is that the currently processed requests are completed. Which it can do so, just by exiting the main thread without exiting the whole program/application. When the last thread in the process exits, the application/program will naturally exit.
The pseudocode might look like:
/* A server application */
void process(void *arg)
{
/* Detach self. */
pthread_detach(pthread_self());
/* process a client request. */
pthread_exit(NULL);
}
int main(void)
{
while (not_done) {
pthread_t t_id;
errno = pthread_create(&t_id, NULL, process, NULL);
if (errno) perror("pthread_create:");
}
/* There may be pending requests at this point. */
/* Just exit the main thread - not the whole program - so that remaining
requests that may still be processed can continue. */
pthread_exit(NULL);
}
Another example could be a daemon or logger thread that logs some information at regular intervals for as long as the application runs.
Does it offer any performance advantage vs. a joinable thread?
Performance-wise, there's no difference between joinable threads vs detached threads. The only difference is that with detached threads, its resources (such as thread stack and any associated heap memory, and so on - exactly what constitutes those "resources" are implementation-specific).
Is it legal to not do a pthread_join() on a joinable (by default) thread?
Yes, it's legal to not join with a thread. pthread_join is a just convenience function that's by no means needs to be used unless you need. But note that the threads created are joinable threads by default.
An example when you might want to join is when threads do a "piece" of work that's split between them. In that case, you'd want to check all threads complete before proceeding. Task farm parallelism is a good example.
Or should such a thread always use the detach() function before pthread_exit()ing?
Not necessary. But you'd often want to decide whether you want a joinable or detached thread at the time of creation.
Note that while a detachable thread can be created in by setting the attribute PTHREAD_CREATE_DETACHED with a call to pthread_attr_setdetachstate, a thread decide can decide to detach itself at any point in time e.g. with pthread_detach(pthread_self()). Also, a thread that has the thread id (pthread_t) of another thread can detach with pthread_detach(thread_id);.