Process Concept and Threads
Last updated
Last updated
Program : Code which is written using any HLL like c++, java, python etc.
Process : A process can be defined as a program in execution. Or when a program is executed it is called as process
Thread: Unit of execution of a process is called as thread. One process can have one or more then one thread.
Thread is the segment of a process. A thread have 3 states: running, ready, and blocked.
When a process is running it changes its states. Process state can be defined as the current activity of the process or what the process is currently doing.
States of a process are as following:
New (Create) - In this step, the process is about to be created/being created but not yet created, it is the program which is present in secondary memory that will be picked up by OS to create the process.
Ready - Ready to run not yet running. After the creation of a process, the process enters the ready state i.e. the process is loaded into the main memory. The process here is ready to run and is waiting to get the CPU time for its execution. I needs to be assigned to the processor.
Running - The process is being executed
Blocked or Waiting - Waiting for some event to occur like I/O operations or reception of a signal.
Terminated or Completed - Process is killed or Finished its execution
Suspend ready - Process that was initially in the ready state but were swapped out of main memory and placed onto external storage by scheduler are said to be in suspend ready state. The process will transition back to ready state whenever the process is again brought onto the main memory.
Suspend wait or suspend blocked - Similar to suspend ready but uses the process which was performing I/O operation and lack of main memory caused them to move to secondary memory. When work is finished it may go to suspend ready.
Text Section:- A process is more than the program code or a code segment is known as Text Section. This section of memory contains the executable instructions of a program. It also contains constants, macros and it is read-only segment to prevent accidentally modification of an instruction. Stack:- The Stack contains the temporary data, such as function parameters, returns addresses, and local variables. Data Section:- Contains the global variable. Heap Section:- Dynamically allocated memory to process during its run time.
PCB is used to represent a process in the Operating System.
A process control block (PCB) contains information about the process, i.e. registers, quantum, priority, etc. The process table is an array of PCB’s, that means logically contains a PCB for all of the current processes in the system.
Pointer :- It is a stack pointer which is required to be saved when the process is switched from one state to another to retain the current position of the process.
Process state :- It stores the respective state of the process.
Process number/PID:- Every process is assigned with a unique id known as process ID or PID which stores the process identifier.
Program counter :- It stores the counter which contains the address of the next instruction that is to be executed for the process.
Register :- These are the CPU registers which includes: accumulator, base, registers and general purpose registers.
Memory limits:- This field contains the information about memory management system used by operating system. This may include the page tables, segment tables etc.
Open files list :- This information includes the list of files opened for a process.
Miscellaneous accounting and status data :- Resources used by a process for its execution .This field includes information about the amount of CPU used, time constraints, jobs or process number, etc.
I/O status information: For example, devices allocated to the process, open files, etc
CPU scheduling information: For example, Priority (Different processes may have different priorities, for example a short process may be assigned a low priority in the shortest job first scheduling)
There are six popular process scheduling algorithms
First-Come, First-Served (FCFS) Scheduling
Shortest-Job-Next (SJN) Scheduling
Priority Scheduling
Shortest Remaining Time
Round Robin(RR) Scheduling
Multiple-Level Queues Scheduling
These algorithms are either non-preemptive or preemptive. Non-preemptive algorithms are designed so that once a process enters the running state, it cannot be preempted until it completes its allotted time Preemptive scheduling is based on priority where a scheduler may preempt a low priority running process anytime when a high priority process enters into a ready state.
Below are different time with respect to a process.
Arrival Time: Time at which the process arrives in the ready queue. Completion Time: Time at which process completes its execution. Burst Time: Time required by a process for CPU execution. Turn Around Time: Time Difference between completion time and arrival time. Turn Around Time = Completion Time – Arrival Time
Waiting Time(W.T): Time Difference between turn around time and burst time. Waiting Time = Turn Around Time – Burst Time
A program is CPU bound if it would go faster if the CPU were faster, i.e. it spends the majority of its time simply using the CPU (doing calculations).
A program is I/O bound if it would go faster if the I/O subsystem was faster. Which exact I/O system is meant can vary; I typically associate it with disk, but of course networking or communication in general is common too. A program that looks through a huge file for some data might become I/O bound, since the bottleneck is then the reading of the data from disk (actually, this example is perhaps kind of old-fashioned these days with hundreds of MB/s coming in from SSDs).
The process scheduling is the activity of the process manager that handles the removal of the running process from the CPU and the selection of another process on the basis of a particular strategy.
Process scheduling is an essential part of a Multiprogramming operating systems. Such operating systems allow more than one process to be loaded into the executable memory at a time and the loaded process shares the CPU using time multiplexing.
There are three types of process scheduler.
Long Term or job scheduler : It brings the new process to the ‘Ready State’. It controls Degree of Multi-programming, i.e., number of process present in ready state at any point of time. It is important that the long-term scheduler make a careful selection of both IO and CPU bound process. IO bound tasks are which use much of their time in input and output operations while CPU bound processes are which spend their time on CPU. The job scheduler increases efficiency by maintaining a balance between the two.
Short term or CPU scheduler : It is responsible for selecting one process from ready state for scheduling it on the running state. Note: Short-term scheduler only selects the process to schedule it doesn’t load the process on running. Here is when all the scheduling algorithms are used. The CPU scheduler is responsible for ensuring there is no starvation owing to high burst time processes. Dispatcher is responsible for loading the process selected by Short-term scheduler on the CPU (Ready to Running State) Context switching is done by dispatcher only. A dispatcher does the following:
Switching context.
Switching to user mode.
Jumping to the proper location in the newly loaded program.
Medium-term scheduler : It is responsible for suspending and resuming the process. It mainly does swapping (moving processes from main memory to disk and vice versa). It is helpful in maintaining a perfect balance between the I/O bound and the CPU bound. It reduces the degree of multiprogramming.
Context Switching The process of saving the context of one process(Running process) and loading the context of another process(Schedules process) is known as Context Switching. In simple terms, it is like loading and unloading the process from the running state to the ready state.
What is a Thread? A thread is a path of execution within a process. A process can contain multiple threads. It is a basic unit of cpu utilization. A thread is also known as lightweight process
Resources like code, data, and files can be shared among all threads within a process. Note: stack and registers can’t be shared among the threads. Each thread has its own stack and registers.
Why Multithreading? The idea is to achieve parallelism by dividing a process into multiple threads. For example, in a browser, multiple tabs can be different threads. MS Word uses multiple threads: one thread to format the text, another thread to process inputs, etc. Process vs Thread? The primary difference is that threads within the same process run in a shared memory space, while processes run in separate memory spaces. Threads are not independent of one another like processes are, and as a result threads share with other threads their code section, data section, and OS resources (like open files and signals). But, like process, a thread has its own program counter (PC), register set, and stack space. Advantages of Thread over Process 1. Responsiveness: If the process is divided into multiple threads, if one thread completes its execution, then its output can be immediately returned.
2. Faster context switch: Context switch time between threads is lower compared to process context switch. Process context switching requires more overhead from the CPU.
3. Effective utilization of multiprocessor system: If we have multiple threads in a single process, then we can schedule multiple threads on multiple processor. This will make process execution faster.
4. Resource sharing: Resources like code, data, and files can be shared among all threads within a process. Note: stack and registers can’t be shared among the threads. Each thread has its own stack and registers.
5. Communication: Communication between multiple threads is easier, as the threads shares common address space. while in process we have to follow some specific communication technique for communication between two process.
A computer operates in two modes which are user mode and kernel mode. When the computer is running application software, it is in user mode. After the application software request for hardware, the computer enters kernel mode.
The interface between a process and an operating system is provided by system calls. In general, system calls are available as assembly language instructions. System calls are usually made when a process in user mode requires access to a resource. Then it requests the kernel to provide the resource via a system call.
A figure representing the execution of the system call is given as follows −
Then the system call is executed on a priority basis in the kernel mode. After the execution of the system call, the control returns to the user mode and execution of user processes can be resumed.
In general, system calls are required in the following situations −
If a file system requires the creation or deletion of files. Reading and writing from files also require a system call.
Creation and management of new processes.
Network connections also require system calls. This includes sending and receiving packets.
Access to a hardware devices such as a printer, scanner etc. requires a system call.
These system calls handle information and its transfer between the operating system and the user program.
Types of System Calls
Windows
Linux
Process Control
CreateProcess() ExitProcess() WaitForSingleObject()
fork() exit() wait()
File Management
CreateFile() ReadFile() WriteFile() CloseHandle()
open() read() write() close()
Device Management
SetConsoleMode() ReadConsole() WriteConsole()
ioctl() read() write()
Information Maintenance
GetCurrentProcessID() SetTimer() Sleep()
getpid() alarm() sleep()
Communication
CreatePipe() CreateFileMapping() MapViewOfFile()
pipe() shmget() mmap()
An API is a set of commands, functions, protocols, and objects that programmers can use to create software or interact with an external system. It provides developers with standard commands for performing common operations so they do not have to write the code from scratch.
APIs are available for both desktop and mobile operating systems. The Windows API, for example, provides developers with user interface controls and elements, such as windows, scroll bars, and dialog boxes. It also provides commands for accessing the file system and performing file operations, such as creating and deleting files. Additionally, the Windows API includes networking commands that can be used to send and receive data over a local network or the Internet.
Mobile APIs, such as the iOS API, provide commands for detecting touchscreen input, such as tapping, swiping, and rotating. It also includes common user interface elements, such as a pop-up keyboard, a search bar, and a tab bar, which provides navigation buttons the bottom of the screen. The iOS API also includes predefined functions for interacting with an iOS device's hardware, such as the camera, microphone, or speakers.