Process Threads: Doing Multiple things at once !! - wiki4techi

Process Threads: Doing Multiple things at once !!

[AdSense-A]

Process Threads: Doing Multiple things at once !!

A program or process normally does one thing at a time. What if we want the program to do multiple things at a time?

 

Lets take an example that you are writing a program to show a bouncing ball graphically on screen. And there are functions to draw a ball on screen. And with time, its weight, size etc, we have a re-draw() function, which keeps on drawing new position on the screen, and removes the previous image, giving us a feeling that it’s a continues movie of a bouncing ball.

 

Now the flow in any procedural language is almost fixed.

  1. Draw the background, define boundary limits.
  2. Use some default position of ball to start with.
  3. Understand the ball size, color, weight, and get the new position.
  4. Erase the old ball on the screen
  5. Draw the ball at new position
  6. Goto step 3

 

Process Threads:

What if we have to show 2 balls bouncing of different size, color, and frequency of bouncing? What is 10 balls… 50 balls?

 

You can easily imagine, that we need something, which will allow the process to do multiple things at a time…

 

 

Now, this is where we need something called as THREAD !!

[AdSense-A]

Process Threads:

Thread is something which allows a process to do more than one thing in parallel. So there is something called as main thread, or the process itself, and any other things you want the process to handle in parallel, you start hem in a new thread. All the threads will continue to execute at the same time.

 

Now, what difference does it make if I have multiple processes using fork(), or totally independent processes communicating through (IPC- Inter process communications), or have single process having multiple threads??

 

So a thread exists within a process.Threads are a finer-grained unit of independent execution than processes.When you invoke a program, Linux creates a new process and in that process creates a single thread, which runs the program sequentially within that thread.This thread can create any number of threads; all these threads run the same program (process) but each thread may be executing a different part of the program at any given time.

Process Threads:

We know, a child process running uses its parent’s virtual memory, file descriptors, and so on copied.The child process can modify its memory, close file descriptors, and the like without affecting its parent, and vice versa.When a program creates another thread, though, nothing is copied.The creating and the created thread share the same memory space, file descriptors, and other system resources as the original. Which means one thread changing some value of some variable, reflects for other. Similarly, if one thread closes a file descriptor, other threads may not read from or write to that file descriptor, as the descriptor is already closed.

 

Because a process and all its threads can be executing only one program at a time (as all of them are a part of same process), if any thread inside a process calls one of the exec functions, all the other threads are ended (the new program may, of course, create new threads). GNU/Linux implements the POSIX standard thread API (known as pthreads). All thread functions and data types are declared in the header file <pthread.h>.The pthread functions are not included in the standard C library. Instead, they are in libpthread, so you should add -lpthread to the command line when you link your program.

The practical examples of creating threads, we will see in my next post.

[AdSense-A]

Process Threads

You may also like...

Leave a Reply

Optimization WordPress Plugins & Solutions by W3 EDGE