Docsity

PROCESS & MULTITHREADING, Study notes of Advanced Operating Systems

Toggle sidebar
bg1
Process in Operating System
A process is a program in execution which then forms the
basis of all computation. The process is not as same as
program code but a lot more than it. A process is an
'active' entity as opposed to the program which is
considered to be a 'passive' entity. Attributes held by the
process include hardware state, memory, CPU, etc.
Process memory is divided into four sections for efficient
working :
The Text section is made up of the compiled
program code, read in from non-volatile storage when
the program is launched.
The Data section is made up of the global and static
variables, allocated and initialized prior to executing
the main.
The Heap is used for the dynamic memory allocation
and is managed via calls to new, delete, malloc, free,
etc.
The Stack is used for local variables. Space on the
stack is reserved for local variables when they are
declared.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download PROCESS & MULTITHREADING and more Study notes Advanced Operating Systems in PDF only on Docsity!

Process in Operating System

A process is a program in execution which then forms the basis of all computation. The process is not as same as program code but a lot more than it. A process is an 'active' entity as opposed to the program which is considered to be a 'passive' entity. Attributes held by the process include hardware state, memory, CPU, etc. Process memory is divided into four sections for efficient working :  The Text section is made up of the compiled program code, read in from non-volatile storage when the program is launched.  The Data section is made up of the global and static variables, allocated and initialized prior to executing the main.  The Heap is used for the dynamic memory allocation and is managed via calls to new, delete, malloc, free, etc.  The Stack is used for local variables. Space on the stack is reserved for local variables when they are declared.

The different Process States

Processes in the operating system can be in any of the following states:  NEW- The process is being created.  READY- The process is waiting to be assigned to a processor.  RUNNING- Instructions are being executed.  WAITING- The process is waiting for some event to occur(such as an I/O completion or reception of a signal).  TERMINATED- The process has finished execution.

I/O Status information : Devices allocated, open file tables, etc.

Process vs Program

Let us take a look at the differences between Process and Program: Process Program The process is basically an instance of the computer program that is being executed. A Program is basically a collection of instructions that mainly performs a specific task when executed by the computer. A process has a shorter A Program has a longer lifetime.

Process Program lifetime. A Process requires resources such as memory, CPU, Input-Output devices. A Program is stored by hard-disk and does not require any resources. A process has a dynamic instance of code and data A Program has static code and static data. Basically, a process is the running instance of the code. On the other hand, the program is the executable code. Process Scheduling When there are two or more runnable processes then it is decided by the Operating system which one to run first then it is referred to as Process Scheduling. A scheduler is used to make decisions by using some scheduling algorithm. Given below are the properties of a Good Scheduling Algorithm :  Response time should be minimum for the users.

Pre-emptive Scheduling: When the operating system decides to favour another process, pre- empting the currently executing process.

What are Scheduling Queues?

 All processes, upon entering into the system, are stored in the Job Queue.  Processes in the Ready state are placed in the Ready Queue.  Processes waiting for a device to become available are placed in Device Queues. There are unique device queues available for each I/O device. A new process is initially put in the Ready queue. It waits in the ready queue until it is selected for execution(or dispatched). Once the process is assigned to the CPU and is executing, one of the following several events can occur:  The process could issue an I/O request, and then be placed in the I/O queue.  The process could create a new subprocess and wait for its termination.  The process could be removed forcibly from the CPU, as a result of an interrupt, and be put back in the ready queue.

In the first two cases, the process eventually switches from the waiting state to the ready state, and is then put back in the ready queue. A process continues this cycle until it terminates, at which time it is removed from all queues and has its PCB and resources deallocated.

Types of Schedulers

There are three types of schedulers available:

  1. Long Term Scheduler
  2. Short Term Scheduler
  3. Medium Term Scheduler Let's discuss about all the different types of Schedulers in detail: Long Term Scheduler

Addition of Medium-term scheduling to the queueing diagram.

What is Context Switch?

  1. Switching the CPU to another process requires saving the state of the old process and loading the saved state for the new process. This task is known as a Context Switch.
  2. The context of a process is represented in the Process Control Block(PCB) of a process; it includes the value of the CPU registers, the process state and memory-management information. When a context switch occurs, the Kernel saves the context of the old process in its PCB and loads the saved context of the new process scheduled to run.
  3. Context switch time is pure overhead , because the system does no useful work while switching. Its speed varies from machine to machine, depending on the memory speed, the number of registers that must be copied, and the existence of special instructions(such as a single instruction to load or

store all registers). Typical speeds range from 1 to 1000 microseconds.

  1. Context Switching has become such a performance bottleneck that programmers are using new structures(threads) to avoid it whenever and wherever possible.

Operations on Process

Below we have discussed the two major operation Process Creation and Process Termination. Process Creation Through appropriate system calls, such as fork or spawn, processes may create other processes. The process which creates other process, is termed the parent of the other process, while the created sub-process is termed its child. Each process is given an integer identifier, termed as process identifier, or PID. The parent PID (PPID) is also stored for each process. On a typical UNIX systems the process scheduler is termed as sched, and is given PID 0. The first thing done by it at system start-up time is to launch init, which gives that process PID 1. Further Init launches all the system daemons and user logins, and becomes the ultimate parent of all other processes.

  1. The child process has a program loaded into it. To illustrate these different implementations, let us consider the UNIX operating system. In UNIX, each process is identified by its process identifier , which is a unique integer. A new process is created by the fork system call. The new process consists of a copy of the address space of the original process. This mechanism allows the parent process to communicate easily with its child process. Both processes (the parent and the child) continue execution at the instruction after the fork system call, with one difference: The return code for the fork system call is zero for the new(child) process, whereas the(non zero) process identifier of the child is returned to the parent. Typically, the execlp system call is used after the fork system call by one of the two processes to replace the process memory space with a new program. The execlp system call loads a binary file into memory - destroying the memory image of the program containing the execlp system call – and starts its execution. In this manner the two processes are able to communicate, and then to go their separate ways. Below is a C program to illustrate forking a separate process using UNIX(made using Ubuntu): #include<stdio.h> void main(int argc, char *argv[]) { int pid;

/* Fork another process */ pid = fork(); if(pid < 0 ) { //Error occurred fprintf(stderr, "Fork Failed"); exit(- 1 ); } else if (pid == 0 ) { //Child process execlp("/bin/ls","ls",NULL); } else { //Parent process //Parent will wait for the child to complete wait(NULL);

When a process ends, all of its system resources are freed up, open files flushed and closed, etc. The process termination status and execution times are returned to the parent if the parent is waiting for the child to terminate, or eventually returned to init if the process already became an orphan. The processes which are trying to terminate but cannot do so because their parent is not waiting for them are termed zombies. These are eventually inherited by init as orphans and killed off.

First Come First Serve Scheduling

In the "First come first serve" scheduling algorithm, as the name suggests, the process which arrives first, gets executed first, or we can say that the process which requests the CPU first, gets the CPU allocated first.  First Come First Serve, is just like FIFO (First in First out) Queue data structure, where the data element which is added to the queue first, is the one who leaves the queue first.  This is used in Batch Systems.  It's easy to understand and implement programmatically, using a Queue data structure, where a new process enters through the tail of the queue, and the scheduler selects process from the head of the queue.  A perfect real life example of FCFS scheduling is buying tickets at ticket counter.

Calculating Average Waiting Time

For every scheduling algorithm, Average waiting time is a crucial parameter to judge it's performance.

AWT or Average waiting time is the average of the waiting times of the processes in the queue, waiting for the scheduler to pick them for execution. Lower the Average Waiting Time, better the scheduling algorithm. Consider the processes P1, P2, P3, P4 given in the below table, arrives for execution in the same order, with Arrival Time 0 , and given Burst Time , let's find the average waiting time using the FCFS scheduling algorithm. The average waiting time will be 18.75 ms For the above given proccesses, first P1 will be provided with the CPU resources,  Hence, waiting time for P1 will be 0  P1 requires 21 ms for completion, hence waiting time for P2 will be 21 ms

This essentially leads to poort utilization of resources and hence poor performance.

Program for FCFS Scheduling

Here we have a simple C++ program for processes with arrival time as 0. If you are not familiar with C++ language, we would recommend you to first Learn C++ language. In the program, we will be calculating the Average waiting time and Average turn around time for a given array of Burst times for the list of processes. /* Simple C++ program for implementation of FCFS scheduling */ #include using namespace std; // function to find the waiting time for all processes void findWaitingTime(int processes[], int n, int bt[], int wt[]) {

// waiting time for first process will be 0 wt[ 0 ] = 0 ; // calculating waiting time for (int i = 1 ; i < n ; i++) { wt[i] = bt[i- 1 ] + wt[i- 1 ]; } } // function to calculate turn around time void findTurnAroundTime( int processes[], int n, int bt[], int wt[], int tat[]) { // calculating turnaround time by adding // bt[i] + wt[i] for (int i = 0 ; i < n ; i++) { tat[i] = bt[i] + wt[i]; }