Program, Process and Thread

In this article, we are going to discuss Program, Process, and Thread. Many of us read these topics in OS (Operating System) courses. Here we are going to get an overview of all of these. Also, by the end of this article, we will surely grasp a good hold on Program, Process, and Thread concept.

Let's first start with the program, A set of instruction that are written in higer level languages like Java, c, python and stored in a file (.c, .java, .py ... etc) is called a program. Example : Here below we can see a simple java program that add two numbers and prints its sum, this java program has its file extension as .java.




As we can see in our java program, some instructions/tasks mentioned to add two numbers and print the sum. Now, we as humans write these instructions but to understand these by computers it should be in binary code (that is in 1 0). so, here Compilers (it convert the whole program into binary at once) and Interpreters (It convert into binary line by line ) come into the picture.

Now to execute/run this program, this binary code loads into the main memory (RAM) and is represented as a process (a program in execution), and these processes are picked by CPU (Central processing unit) in order to execute the steps mentioned in the program (that comes as binary code to CPU).

The task of managing the process (like which process to load first and which process after in main memory) is done by OS (Operating System like Windows, UNIX, Mac, etc). also, OS helps the process by allocating other resources to process, during the execution of the process. In order to execute the process by CPU, the process requires CPU resources and storage.

Let's understand the memory layout of a process, It is having 4 different sections like stack, heap, text, and data.

  • Text section: It includes compiled program code.

  • Data section: includes global and static variables.

  • Stack section : use for local variables and function/method return values.

  • Heap section: used for dynamic memory allocation.

Also, Stack (grows downward) and Heap (grows upward) grow towards each other, if both ever collide then there will be an error of stack overflow. The below code snippet will help us to understand which part of the code stores in which section.

Now the task of a Process is divided into various or at least one sequential flow of execution, each of these single flows is called as Thread . "A thread is a basic unit of CPU utilization" or we can also say it as lightweight process.

Each thread has its own stack, its own program counter, and its own local variables.

Unlike different processes, different threads within a process share code segment, data segment, and open files. It means the changes in data by one thread can be seen by other threads also.

Example: A word processor may have a thread for displaying graphics, another thread for responding to keystrokes from the user, another thread that counts the number of lines and words, and a thread for performing spelling and grammar checking in the background.

Another example is interactive GUI. Programmers use event-listener to build GUIs that listen and respond to keyboard or mouse keystrokes. These event listeners are simply threads within the process.

Now let's discuss how can we see process and threads in our OS(Operating System). Below we are taking example of linux. In linux we have command like top, using which we can see the current running processes in our linux OS.

top

here in below screenshot, we can see cpu usage , memory usage , PID and more things that are related to processes and threads. Also, if we want to terminate or stop execution of any process we can use kill command. kill [pid] , here pid is the id of the process or thread that you want to teminate.

Now, let see how we can see the threads associated with a process. using top command again we can see all the threads associated with process. here is the command top -H -p [pid]. For example If we want to see threads related to PID 3029. then we can use command as top -H -p 3029.

There is another way also to see threads using command : ps -e -T | grep [name of application or pid]. here below we can see threads related to "chrome".





Note: Please comment below for any improvement, suggestions, regarding any wrong information present in this article. We will improve it so that only the right information is available in this article.





Comments

  1. Very impressive, the way you have explained it so easily in laymen terms .. keep posting. If there is a way we can connect on linkedin it would be great. I have a feeling your timeline would be full of great content.

    ReplyDelete

Post a Comment