Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
IN2011 Nets & Ops Session 3 3 IN2011 Nets & Ops Session 3 4
IN2011 Nets & Ops Session 3 5 IN2011 Nets & Ops Session 3 6
1
Process State Process in Memory
IN2011 Nets & Ops Session 3 7 IN2011 Nets & Ops Session 3 8
IN2011 Nets & Ops Session 3 9 IN2011 Nets & Ops Session 3 10
n Address space
l Child duplicate of parent
n UNIX examples
l fork system call creates new process
l exec system call used after a fork to replace the
process’ memory space with a new program
IN2011 Nets & Ops Session 3 11 IN2011 Nets & Ops Session 3 12
2
C Program Forking new Process Process Termination
int main()
{
pid_t pid;
n Process executes its final statement
/* fork another process */
and asks to be deleted by the operating
pid = fork();
if (pid < 0) { /* error occurred */
system (exit)
fprintf(stderr, "Fork Failed");
exit(-1);
l Output data from child to parent (via wait)
}
else if (pid == 0) { /* child process */
l Process’ resources are deallocated by the OS
execl("/bin/ls", "ls", NULL);
} n Some operating systems do not allow a
else { /* parent process */
/* parent will wait for the child to complete */ child to continue if its parent terminates
wait (NULL);
printf ("Child Complete"); l All children terminated - cascading termination
exit(0);
} l Note that this is not the default behaviour in Linux
}
IN2011 Nets & Ops Session 3 13 IN2011 Nets & Ops Session 3 14
IN2011 Nets & Ops Session 3 15 IN2011 Nets & Ops Session 3 16
Process Scheduling Queues Ready Queue And Various I/O Device Queues
3
Context Switch CPU Context Switch
IN2011 Nets & Ops Session 3 19 IN2011 Nets & Ops Session 3 20
IN2011 Nets & Ops Session 3 21 IN2011 Nets & Ops Session 3 22
4
Scheduler Aims Some Scheduling Schemes
IN2011 Nets & Ops Session 3 25 IN2011 Nets & Ops Session 3 26
n Simple scheme which services jobs from n Designed for time-sharing (multitasking)
the ready queue in First-Come-First- n Each process gets a small unit of CPU time
Served order (time quantum).
l New processes join the back of the queue l When quantum elapses, process is preempted and added
to the back of the ready queue
l Once a process is scheduled on the CPU it stays there
until it is interrupted: l implemented using timers to generate periodic interrupts
4 either by itself (system call, eg I/O request) n For n processes in the ready queue and time
4 or by a hardware interrupt (eg I/O device) quantum q, each process gets 1/n of the CPU
l No good for interactive systems, because CPU-bound time in chunks of at most q time units
processes hog the CPU l q must be large with respect to context switch time,
otherwise overhead is too high
IN2011 Nets & Ops Session 3 27 IN2011 Nets & Ops Session 3 28
1 1 2 1,2 1 3 3
0- 5- 10- 15- 20- 25- 30- 35- 40- 45- 50- 55- 60- 65- 70- 75- 80- 85- 90- 95- 100- 105- 110- 115-
process arrival resource demands summary 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120
id time (ms) (not known in advance) 1 1 * 2 3 2 1 3 2 1 3 3 2 1 2 3 1 2 3 1 2 3 1 2
Assume context switching takes zero time Turnaround_time = process end_time – process start_time
Assume each process uses a different I/O device Length = sum of all the process' resource demand times
Assume time quantum = 5ms Waiting_time = process Turnaround_time – process Length
IN2011 Nets & Ops Session 3 29 IN2011 Nets & Ops Session 3 30
5
RR Calculations: Exercise Shortest-Job-First (SJF)
n Calculate turnaround, length, and waiting n Associate with each process the length
times for each process of its next CPU burst. Use these
l P1
lengths to schedule the process with the
l P2
l P3
shortest time
n Calculate: n SJF is optimal – gives minimum
l average turnaround time average waiting time for a given set of
l average waiting time processes
n Calculate CPU utilisation (percentage of CPU l The difficulty is knowing the length of the next
time spent running processes) CPU burst!
IN2011 Nets & Ops Session 3 31 IN2011 Nets & Ops Session 3 32
Length of Next CPU Burst Prediction of the Length of the Next CPU Burst
IN2011 Nets & Ops Session 3 33 IN2011 Nets & Ops Session 3 34
IN2011 Nets & Ops Session 3 35 IN2011 Nets & Ops Session 3 36
6
Multiple-Processor Scheduling Linux Scheduling (before 2.6.23)
n CPU scheduling is more complex when n Constant order O(1) scheduling time
multiple CPUs are available n Two priority ranges: time-sharing and
n Read [OSC] Section 5.5 real-time
n Real-time range from 0 to 99 and nice
value from 100 to 140
l essentially different schedulers used for real-time
and non real-time scheduling
IN2011 Nets & Ops Session 3 37 IN2011 Nets & Ops Session 3 38
IN2011 Nets & Ops Session 3 39 IN2011 Nets & Ops Session 3 40