Process vs Threads, thread example - wiki4techi

Process vs Threads, thread example


process vs threads

Create a Thread


Compile and link this program using the following code:

Anand:$> cc -o thread-create thread-create.c -lpthread


Now run the code, you will see unpredictable pattern of x’s and o’s, as Linux alternately schedules the two threads.


Create Two Threads


The example has a serious bug.The main thread creates the thread parameter structures (thread1_args and thread2_args) as local variables, and then passes pointers to these structures to the threads it creates.What if main thread finishes executing before either of the other two threads are done? … this could lead to problems….! The memory containing the thread parameter structures will be deallocated while the other two threads are still accessing it.


Joining Threads

One solution for above problem is to force main to wait until the other two threads are done.A function similar to wait that waits for a thread to finish instead of a process will solve this issue. That function is pthread_join, which takes two arguments: the thread ID of the thread to wait for, and a pointer to a void* variable that will receive the finished thread’s return value. If you don’t care about the thread return value, pass NULL as the second argument.Below code shows the corrected main function, here main does not exit until both of the threads printing x’s and o’s have completed, so they are no longer using the argument structures.


Revised Main Function for thread-create2.c

Create Two Threads


Compute Prime Numbers in a Thread


Processes Vs Threads


For some programs that demands or need concurrency, the decision whether to use processes or threads can be difficult. Here are some guidelines to help you decide


Which concurrency model best suits your program:


  • All threads in a program must run the same executable (they are all part of same process). A child process, on the other hand, may run a different executable by calling an exec function.
  • An errant thread can harm other threads in the same process because threads share the same virtual memory space and other resources. For instance, a wild memory write through an uninitialized pointer in one thread can corrupt memory visible to another thread.

An errant process, on the other hand, cannot do so because each process has a copy of the program’s memory space. (So it’s safer)


  • There is an additional performance overhead of copying memory for a new process relative to creating a new thread. However, the copy is performed only when the memory is changed, so the penalty is minimal if the child process only reads memory.
  • Threads should be used for programs that need fine-grained parallelism. For example, if a problem can be broken into multiple, nearly identical tasks, threads may be a good choice. Processes should be used for programs that need coarser parallelism, and may be not too much data commonality.
  • Sharing data among threads is trivial because threads share the same memory. (However, great care must be taken to avoid race conditions) Sharing data among processes requires the use of IPC mechanisms, like pipe, shared memory, named pipes and so on.This can be more cumbersome but makes multiple processes less likely to suffer from concurrency bugs.




You may also like...

Leave a Reply

Optimization WordPress Plugins & Solutions by W3 EDGE