0% found this document useful (0 votes)
4 views

Week 2 Process

The document provides an overview of processes in operating systems, detailing their states, memory layout, and control structures like the Process Control Block (PCB). It discusses types of processes, scheduling methods, and the differences between forking and threading. Additionally, it covers CPU scheduling criteria and examples of scheduling algorithms.

Uploaded by

Sparsh Kashyap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Week 2 Process

The document provides an overview of processes in operating systems, detailing their states, memory layout, and control structures like the Process Control Block (PCB). It discusses types of processes, scheduling methods, and the differences between forking and threading. Additionally, it covers CPU scheduling criteria and examples of scheduling algorithms.

Uploaded by

Sparsh Kashyap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

12-1

Process and its States


12-2
Process

A program in execution is called process.

A process is more than the set of instructions, besides it contains


memory area, local, global variable, CPU registers, Program
Counters.
12-3
Process Concept

Program is passive entity stored on disk (executable file); process is


active
◆Program becomes process when an executable
file is loaded into memory
Execution of program started via GUI mouse clicks, command line entry
of its name, etc.
One program can be several processes
◆Consider multiple users executing the same
program
◆A user running multiple copies of web browser
12-4
Process Concept

Multiple parts
◆The program code, also called text section
◆Current activity including program counter,
processor registers
◆Stack containing temporary data
Function parameters, return addresses, local variables
◆Data section containing static and global
variables
◆Heap containing memory dynamically allocated
during run time
12-5
Process in Memory

When a program is loaded into the memory and it becomes a


process, it can be divided into four sections ─ stack, heap, text
and data.
12-6
Memory Layout of a C Program
12-7
P-1

which section of a program does not contain in the memory?

Heap
Data
Stack
Program counter
12-8
Types of Processes

❑ CPU Bound Process: If the process is intensive in terms


of CPU operations then it is called CPU bound process.
❑ For. e.g. Multiply two matrices

❑ I/O Bound Process: If the process is intensive in terms of


I/O operations then it is called IO bound process.
❑ For e.g. Word processor, counting number of lines in a
file
12-9
Concept of Multiprogramming

❑ Pre-emption – Process is forcefully removed from CPU.


Pre-emption is also called as time sharing or multitasking.

❑ Non pre-emption – Processes are not removed until they


complete the execution. Multiprogramming is an example
of Non-pre-emptive scheduling algorithm.
12-10
Process Control Block (PCB)

When the process is created by the operating system it creates a data


structure to store the information of that process.
Each process is represented in the OS by a PROCESS CONTROL BLOCK
(PCB)- also called Task control Block.

Process Id Program counter

CPU registers Process state

I/O status information

Accounting information

CPU scheduling information

Memory Management information


12-11

PCBs are stored in specially reserved memory for the operating system
known as kernel space.
The Random Access Memory (RAM) can be logically divided into two
distinct regions namely –
◆the kernel space and the user space.
kernel space is the core of the operating system. It normally has full
access to all memory and machine hardware and it can't be accessed by
the user.
12-12
Process State Diagram (5-States)

Awake ( Process-name) : New -> Ready


Dispatch ( Process-name): ready -> running
Timerrunout ( Process-name): running -> ready
Block( Process-name): running -> blocked
wakeup( Process-name): blocked-> ready
12-13
P-3

A Process Control Block(PCB) does not contain which of the


following :

A. Program counter
B. List of open files
C. Process State
D. I/O status information
E. bootstrap program
12-14
P-2

The address of the next instruction to be executed by the


current process is provided by the:

A. CPU registers
B. program counter
C. process stack
D. pipe
12-15
Process Representation in Linux
Represented by the C structure task_struct

pid t_pid; /* process identifier */


long state; /* state of the process */
unsigned int time_slice /* scheduling information */
struct task_struct *parent;/* this process’s parent */
struct list_head children; /* this process’s children */
struct files_struct *files;/* list of open files */
struct mm_struct *mm; /* address space of this process */
12-16
Process Scheduling

The act of determining which process is in the ready state, and


should be moved to the running state is known as Process
Scheduling.
Maintains scheduling queues of processes
◆ Job Queue
In starting, all the processes get stored in the job queue. It
is maintained in the secondary memory.
◆ Ready queue – set of all processes residing in main
memory, ready and waiting to execute
◆ Device Queue(Wait queues) – set of processes waiting for
an event (i.e., I/O)
◆ Processes migrate among the various queues
12-17
Ready and Wait Queues
12-18
CPU Switch From Process to Process
A context switch occurs when the CPU switches from one
process to another.
12-19
Context Switch

When CPU switches to another process, the system must save the
state of the old process and load the saved state for the new process
via a context switch
Context of a process represented in the PCB
Context-switch time is pure overhead; the system does no useful work
while switching
◆The more complex the OS and the PCB ➔ the
longer the context switch
Time dependent on hardware support
◆Some hardware provides multiple sets of
registers per CPU ➔ multiple contexts loaded at
once
12-20
Representation of Process Scheduling
12-21
Schedulers

Schedulers are special system software which handles process


scheduling in various ways. Their main task is to select the jobs to be
submitted into the system and to decide which process to run.
Schedulers are of three types −

Long-Term Scheduler

Short-Term Scheduler

Medium-Term Scheduler
12-22
Long-Term Vs Short Term Scheduler
12-23
Medium Term Scheduler
12-24
Process Scheduling Queues
12-25
Process State Diagram ( 7 States)
12-26
12-27
12-28

#include <stdlib.h> #include <stdlib.h>


#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <pthread.h> #include <sys/wait.h>
int x = 2; #include <sys/types.h>
void* routine() {
printf("Hello from thread"); int main(int argc, char* argv[]) {
}
int main(int argc, char* argv[]) { int pid = fork();
pthread_t t1, t2; if (pid == -1) {
if (pthread_create(&t1, NULL, &routine, NULL)) return 1;
{ return 1; }
} printf("Hello from process\n");

if (pthread_join(t1, NULL)) { if (pid != 0) {


return 2; wait(NULL);
} }
return 0; return 0;
} }
12-29

Hello from process


Hello from thread Hello from process
12-30

#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <unistd.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/wait.h>
int x = 2;
#include <sys/types.h>
void* routine() {
printf("process id%d\n",getpid()); int main(int argc, char* argv[]) {
}
int main(int argc, char* argv[]) { int pid = fork();
pthread_t t1, t2; if (pid == -1) {
if (pthread_create(&t1, NULL, &routine, NULL)) return 1;
{ return 1; } }
if (pthread_create(&t2, NULL, &routine, NULL))
{ return 1; printf("process id %d\n",getpid());
} if (pid != 0) {
if (pthread_join(t1, NULL)) { wait(NULL);
return 2; }
if (pthread_join(t2, NULL)) { return 0;
return 2; }
}
return 0; } }
12-31

process id 954
process id 1056 process id 965
process id 1056
12-32

#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <pthread.h> #include <sys/wait.h>
int x = 2;
#include <sys/types.h>
void* routine() {
x += 5;
sleep(2); int main(int argc, char* argv[]) {
printf("Value of x: %d\n", x);
int x = 2;
}
void* routine2() { int pid = fork();
sleep(2); if (pid == -1) {
printf("Value of x: %d\n", x);
return 1;
}
int main(int argc, char* argv[]) { }
pthread_t t1, t2; if (pid == 0) {
if (pthread_create(&t1, NULL, &routine, NULL)) {
x++;
return 1;
} }
if (pthread_create(&t2, NULL, &routine2, NULL)) { sleep(2);
return 2;
printf("Value of x: %d\n", x);
}
if (pthread_join(t1, NULL)) { if (pid != 0) {
return 3; wait(NULL);
}
}
if (pthread_join(t2, NULL)) {
return 4; return 0;
} }
return 0;
}
12-33

Value of x=2
Value of x=7 Value of x=3
Value of x=7
12-34

S.NO Fork Thread

Forking is nothing but


Threading is a light weight process
creating a new process. We
which shares all the section of the
1 create a new process that
process except for the stack. A
copies all the elements of
process can have multiple threads.
old process.

Since all threads of the same process


Changes to the parent share address space and other
3. process do not affect child resources so any changes to the
processes. main thread may affect the behavior
of the other threads of the process.
12-35
Type of CPU Scheduling

Non-Preemptive Scheduling
Under non-preemptive scheduling, once the CPU has been allocated to a
process, the process keeps the CPU until
1. Process is terminated
2. Switches to the waiting state.

Preemptive Scheduling
In this type of Scheduling, the process switches the control to another
process in following conditions
1. High Priority Process Arrives
2. Times Slice Expires
3. Process completes it I/O Operation
4. Generate a child process
12-36
CPU Scheduling Criteria

Throughput: It is the total number of processes completed per unit time or


rather say total amount of work done in a unit of time. This may range from
10/second to 1/hour depending on the specific processes.

Turnaround Time: It is the amount of time taken to execute a particular


process, i.e. the interval from time of submission of the process to the time of
completion of the process (Wall clock time).

Waiting Time: The sum of the periods spent waiting in the ready queue
amount of time a process has been waiting in the ready queue to acquire get
control on the CPU.
12-37
CPU Scheduling Criteria cont…
Load Average: It is the average number of processes residing in the
ready queue waiting for their turn to get into the CPU.
Response Time: Amount of time it takes from when a request was
submitted until the first response is produced. Remember, it is the time
till the first response and not the completion of process execution (final
response).
In general CPU utilization and Throughput are maximized and other
factors are reduced for proper optimization.

CPU Utilization: To make out the best use of CPU and not to waste
any CPU cycle, CPU would be working most of the time (Ideally 100%
of the time).
Considering a real system, CPU usage should range from 40% (lightly
loaded) to 90% (heavily loaded).
12-38
FCFS Example-Q1

Average WT=17
12-39
Q2

Process Burst time Arrival time


P1 6 2
P2 3 5
P3 8 1
P4 3 0
P5 4 4

Average WT=8
12-40
Q3

Average WT=5.75
12-41
Q4.
12-42
SJF
12-43
SJF with non-preemptive
Example 2(Non-preemptive) 12-44

Process Queue Burst time Arrival time


P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4
12-45

Wait time
◆P4= 0-0=0
◆P1= 3-2=1
◆ P2= 9-5=4
◆P5= 11-4=7
◆P3= 15-1=14
Average Waiting Time= (0+1+4+7+14)/5 = 26/5 = 5.2
12-46

You might also like