top of page
Search
understandingdevop

What is the difference between process and thread?

In Linux, if you want to perform any operation you want system call, without system call we cannot perform most of our operations, eg:- create file, run process, open a port, etc.

If you are running any program, the operating system creates a PID for that process. Every running program is nothing but the process.


System Calls are nothing but the way of interacting with the operating system. In other words, system calls allow the user programs to ask the operating system to do some stuff on behalf of the user program, read and write of the file which requires the I/O from/to the storage device, you cannot directly access storage device but kernel can do with the help of system call.


Let's talk about the Process: 


So the process is heavyweight because it includes code, data files, and stack register. Whenever a process is created, the kernel creates a clone of the current process using the fork() system call. So basically kernel creates a child process using fork(), and exec() system call actually executes your binary.


You can see the process tree in your server by executing command "pstree", every process has a parent process and children's associated to it, except the init/systemd process. INIT process is the first process to start with PID 1.


The operating system treats both parent and child processes differently because both processes will have it's unique PID. Here both processes will have its own data files, code, and stack registers.


The process contains stack registers, code, and data files. When kernel creates a child process it will create an exact copy of the stack registers, code, and data files of the parent process.


Context Switching is slower within processes because the operating system has to save all the values in PCB (Process Control Block) and then switch to another process. One process contains a stack register, code, and data files.


If I have 1 parent process with 3 child processes running, and if one of my process requests for disk i/o(input/output) operation, the operating system will only block that specific process and not all the other child processes, because for operating system those are different processes with different PIDs.


Every process in Linux is created by a parent process using a library function called fork() system call.


Nowadays fork() system call is replaced with the clone() system call. The clone system call is similar to the fork system call with some additional features.


Let's talk about thread:


No system calls are required for creating threads (i am talking about user-level thread here). It is the responsibility of an application to create a thread using some library or functions. Thread is created using API.


Context Switching is faster within threads because the operating system has to save all the values of the thread in PCB (Process Control Block) and then switch to another thread. This happens faster because all the threads share the same memory, addresses and there is no system call involved here.


An operating system treats all user-level threads as a single process because it shares memory, addresses, etc.


If I have a Parent process with 3 threads running, and if one of the thread requests for disk io(input/output) operation, here operating system will block the entire process including all threads because for the operating system it is complete 1 process. The operating system won't know about the thread (because it's the user-level thread) which are created by your application.







21 views0 comments

Commenti


Post: Blog2_Post
bottom of page