Tuesday, November 24, 2015

OS : Multithread


Recently, I have one doubt over process.  I tried to understand it, but as it mostly happens to understand it I had to check something else.


https://computing.llnl.gov/tutorials/pthreads/


When we run a program, a PROCESS is created. Now each PROCESS is actually a default THREAD, which requires ONE CORE of CPU.

All this doubts came since now CPU are multiprocessor and multicore.

So when we have octa-core CPU, that means it has 8 cores. Now on this CPU we can run 8 threads at same time.

Now if no PROCESS is creating any threads, then we have 8 PROCESS running at same time on this CPU.

Now each PROCESS has its own ADDRESS SPACE.  But THREADS of same PROCESS share the ADDRESS SPACE of default THREAD of the PROCESS.


But with SINGLE CORE CPU, even when we have multiple THREADED program written, THREADS will be running one after another.

Now if we have put our THREAD into sleep or if it is waiting for some resource, then CPU will process some other THREAD based on PRIORITY.


Also please note, when you are making a multithreaded program, then you divide your FUNCTIONALITY into SUB-FUNCTIONALITY and assigns each SUB_F  to threads.

Each THREAD entry point is call to that FUNCTION.


Use Case :

1) You have to guess a word which is 4 letter and starts with W and ends with R.  Now I will give you one such word with all four letters. You have to see how fast you can reach it.

Now with multi-core CPU, we can use both CORE to work parallely to guess words.

This is when you have MULTICORE processor

2) In case of SINGLE PROCESS which has two parts, one BACKGROUND/DATA PROCESSING and ONE FOREGROUND. In this case we don't want our FOREGROUND process to be slow because of BACKGROUND.

This is use case when you have SINGLE CORE processor.

Make a program in which one THREAD writes data to text file and one THREAD is printing loop count, and one thread is calculating fibonacci series.  See when you perform READ/WRITE operation does fibonacci program runs.



-----------------------------------------------------------------------------------------------------------------------

How does THREADS get scheduled ?

Can we call some THREAD from other task ? We have thread id, but is there any API to call THREAD from the thread id.

Does Modem uses THREAD ? if yes where and why

No comments:

Post a Comment