Week 2 Process
Week 2 Process
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
Heap
Data
Stack
Program counter
12-8
Types of Processes
Accounting information
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)
A. Program counter
B. List of open files
C. Process State
D. I/O status information
E. bootstrap program
12-14
P-2
A. CPU registers
B. program counter
C. process stack
D. pipe
12-15
Process Representation in Linux
Represented by the C structure task_struct
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
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("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
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
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
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
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