Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.
-
date post
20-Dec-2015 -
Category
Documents
-
view
219 -
download
1
Transcript of Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.
![Page 1: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/1.jpg)
Styresystemer og Multiprogrammering
Block 3, 2005
Processes, Threads, and Scheduling
Robert Glück
![Page 2: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/2.jpg)
2
Today’s Plan• Processes:
– Process Concept– Context Switch– Process Family– Interprocess Communication
• Threads:– A light form of parallelism
• Scheduling:– How to utilize the system resources best?– How to ensure short waiting times?
![Page 3: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/3.jpg)
3
What is a Process?• A process is a program in execution:
– Program code– State of execution (registers, open files, ...)
• Process execution in sequential fashion• Operating system executes a variety
of user processes and system process• Programs functionality is specified by
a series of instructions:– Executable code– High-level programming language
![Page 4: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/4.jpg)
4
What’s in a Process?
• Information associated with each process:– Program code (same for all instances)– Program data– CPU registers– System resources:
• Memory• Open files
– Accounting information:• Process ID• Resource usage
![Page 5: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/5.jpg)
5
Process Control Block
![Page 6: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/6.jpg)
6
Life Cycle of a Process
As a process P executes, it changes state:new: P is being createdrunning: P’s instructions are executedwaiting: P is waiting for some eventready: P is ready to be executed,
but waits for CPU timeterminated: P has finished execution
![Page 7: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/7.jpg)
7
Process Life Cycle
Schedulers: manage queues
![Page 8: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/8.jpg)
8
Switch from Process to Process
![Page 9: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/9.jpg)
9
Context Switch
• Context switch is expensive (typical: ≤10msec)– Direct costs:
• Save and load execution state (CPU registers, counters,...)• Memory-management information (e.g., virtual memory)
– Indirect costs:• CPU cache filled with data of old process• Other caches have similar problems
• Hardware supported context switch:– Single instruction to load and store registers– CPU has several register sets (example: 10 sets)– Intel hyperthreading (2 active process per CPU)
![Page 10: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/10.jpg)
10
Process Scheduling Queues
Processes that are not active are located:
- Ready queue: ready, waiting for CPU
- Waiting queues: process waits for event:
I/O device: waiting for an I/O device
Synchronization: limited access to a system resource requires wait
![Page 11: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/11.jpg)
11
Scheduling Queues
I/O
CPU
![Page 12: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/12.jpg)
12
Representation of Process Scheduling
![Page 13: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/13.jpg)
13
Schedulers
• Long-term: select which processes should be brought into the ready queue
• Short-term: selects which process should be executed next and allocates CPU
• Medium-term: swap-out, swap-in, reduce degree of multiprogramming, improve process mix or overcommitted
![Page 14: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/14.jpg)
14
Scheduling Time Horizons
short-term ≤ 10msec
long-term≤ minutes
medium-term
![Page 15: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/15.jpg)
15
Family Life of Processes• Process (parent) can start new processes (children)• A child can inherit (parts of) the system resources of
the parent– Open files– Address space
• A child can be an instance of another program (typically: inherit nothing of the parent)
• Execution of child:– Parent waits until child terminates– Parent and child execute concurrently
• When parent terminates:– Kill all children– Rights to children given to ancestor
![Page 16: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/16.jpg)
16
UNIX Example• fork() duplicates a process:
– Copy address space of the parent– Return value:
• 0 for the child• Process_id of the child is returned to parent
• wait() wait for termination of child
• execv(prog) execute a new program ”prog”
![Page 17: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/17.jpg)
17
Forking Separate Process in C#include <stdio.h>#include <unistd.h>int 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);printf("Child Complete");exit(0);
}}
![Page 18: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/18.jpg)
18
UNIX Example: pstree
process tree
![Page 19: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/19.jpg)
19
UNIX Example: ps
Four children
One child
![Page 20: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/20.jpg)
20
Cooperating Processes
• Independent process if it cannot affect or be affected by other processes.
• Cooperating process if it can affect or be affected by other processes.
• Reasons for providing process cooperation:– Share data (example: a shared file)– Computation speedup (only if multiple processing
elements such as CPU or I/O channels)– Modularity (divide system functions into modules) – Convenience: several processes are natural for a
task (example: edit, print, compile in parallel)
![Page 21: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/21.jpg)
21
Interprocess Communication (IPC)
• Linking input and output streams:– UNIX pipes, example: gunzip –v text.txt.gz | less
• Message Passing:– Direkt:
• send(453, message): send msg to process with ID 453• receive(id, message): receive msg from some process
– Indirekt (via mailboxes):• Easier to establish many-to-many relations• Named mailboxes abstract from process ID
• Shared memory
![Page 22: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/22.jpg)
22
Producer-Consumer Problem
• Paradigm for cooperating processes: producer process produces that is consumed by a consumer process
• Buffer:– Unbounded = no practical limit on the size– Bounded = synchronize P’s and C’s speed
• Shared memory:– buffer: a table– in: pointer to the next free entry– out: pointer to the next available element
![Page 23: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/23.jpg)
23
Shared-Memory Solutionitem nextProduced;
while (1) {while (((in + 1) % BUFFER_SIZE) == out)
yield(); /* do nothing */buffer[in] = nextProduced;in = (in + 1) % BUFFER_SIZE;
}
item nextConsumed;
while (1) {while (in == out)
yield(); /* do nothing */nextConsumed = buffer[out];out = (out + 1) % BUFFER_SIZE;
}
buffer FULLcondition
buffer EMPTYcondition
![Page 24: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/24.jpg)
24
Threads
• Process can have several parallel tasks:– Web browser: display images, layout text,
retrieve data from the network– Word processor: display graphics, check spelling,
read keystrokes from the user
• Threads give a process the possibility to have several active program sequences that share the resources of the process:– threads are ”light” overhead– processes are ”heavy” overhead
![Page 25: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/25.jpg)
25
Single- and Multiple-Threaded
three threads
![Page 26: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/26.jpg)
26
User and Kernel Threads
• User Threads:– Implemented ”inside” a single process– Administration of threads inside user process– Low overhead when creating, switching, and
terminating threads
• Kernel Threads:– Calls kernel for administration (expensive)– Kernel can switch threads when a system call
blocks– Can exploit multiple processors (parallelism)
![Page 27: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/27.jpg)
27
CPU Scheduling
• Goal of CPU scheduling: ”Best utilization of system resources”
• Different scheduling strategies:– First come, first served– Shortest job first– Round robin– Priority based– Multilevel queue
![Page 28: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/28.jpg)
28
Goals of Scheduling
• Maximize utilization of system resources:– CPU– I/O devices
• Process execution consists of:– CPU execution– I/O requests
• Ensure low waiting times for user!
![Page 29: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/29.jpg)
29
Usage of CPU versus I/O
• CPU-bound: uses CPU more that I/O devices
• I/O-bound: uses I/O devices more than CPU
![Page 30: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/30.jpg)
30
Histogram of CPU-burst Times
1
![Page 31: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/31.jpg)
31
CPU Scheduler
• CPU scheduler selects a process for execution from a set of ready processes
• Scheduling decisions may take place when:1. running process switches to waiting state (I/O req)
2. running process switches to ready state (interrupt)
3. waiting process switches to ready state (I/O done)
4. process terminates
• Nonpreemptive (voluntary switch): 1 & 4• Preemptive (forced switch): 2 & 4
![Page 32: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/32.jpg)
32
Criteria for CPU Scheduling• CPU utilization:
– How much % of time CPU is busy (typical 40-90%)
• Throughput:– Number of processes that are completed per minute
• Turnaround time:– Time it takes to complete a process
• Waiting time:– Time spent waiting in the ready queue
• Response time:– Time it takes from a request to a response
MAXIMIZE
MAXIMIZE
MINIMIZE
MINIMIZE
MINIMIZE
![Page 33: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/33.jpg)
33
First Come, First Served (FCFS) Process CPU burst time (msec)
P1 24
P2 3
P3 3
• Processes arrive in the order: P1 , P2 , P3
• Gantt chart for the schedule is:
• Waiting times: P1 = 0; P2 = 24; P3 = 27
• Average waiting time: (0 + 24 + 27)/3 = 17
P1 P2 P3
24 27 300
![Page 34: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/34.jpg)
34
First Come, First Served (cont’d)
• Suppose that the processes arrive in the order
P2 , P3 , P1
• Gantt chart for the schedule is:
• Waiting times: P1 = 6; P2 = 0; P3 = 3
• Average waiting time: (6 + 0 + 3)/3 = 3
• Much better than previous case!• Convoy effect: short process behind long process
P1P3P2
63 300
![Page 35: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/35.jpg)
35
Shortest Job First• CPU assigned to the process that has the
smallest next CPU burst:– Preemptive: when a new process with a shorter
CPU burst arrives at the ready queue– Nonpreemptive: running process is not interrupted
• Shortest waiting time for a set of processes• Problem: need to know the execution time• Heuristics predict length of next CPU burst:
– Exponential average: ( ) nnn t ταατ −+=+ 1 1
![Page 36: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/36.jpg)
36
Round Robin (RR)• Each process gets a small unit of CPU time
(time quantum), usually q=10-100 msec.• After this time has elapsed, the process is
preemptied and added to the end of the ready queue.
• If there are n processes in the ready queue, then each process gets 1/n of the CPU time, and no process waits longer that (n-1)q times.
• Performance:– q large long waiting times (same as FCFS)– q small q must be large with respect to context-
switch time, otherwise the overhead is too high
![Page 37: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/37.jpg)
37
Priority Scheduling• A priority number is associated with each
process. Example: 0 (high) - 7 (low)• The CPU is allocated to the process with the
highest priority (equal priority in FCFS)• SJF is a priority scheduling where the priority
is the predicted next CPU burst time• Problem: Starvation – low priority processes
may never execute• Solution: Aging – as time progresses increase
the priority of the process
![Page 38: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/38.jpg)
38
Multilevel Ready Queue• Queue is partitioned into separate queues:
– foreground (interactive) processes– background (batch) processes
• Each queue has its own scheduling algorithm:– foreground: RR– background: FCFS
• Scheduling must be done between the queues: – Fixed priority scheduling: serve all from foreground,
then from background. Possibility of starvation– Time slice: example: 80% to foreground in RR,
20% to background in FCFS.
![Page 39: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/39.jpg)
39
Multilevel Feedback Queue
• A process can move between the various queues; e.g., implement aging
• Observe processes and place them into queue according to their behavior:– A foreground process that often (or once)
exceeds its time quantum is placed into the background queue
![Page 40: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/40.jpg)
40
Example of Multilevel Feedback Queue
![Page 41: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/41.jpg)
41
Summary
• Process: program in execution, process switch, process queues
• Threads: parallelism within a process
• Scheduling: maximize CPU utilization, minimize waiting times, forced vs voluntary process switch, several scheduling strategies
![Page 42: Styresystemer og Multiprogrammering Block 3, 2005 Processes, Threads, and Scheduling Robert Glück.](https://reader036.fdocuments.in/reader036/viewer/2022062407/56649d545503460f94a30293/html5/thumbnails/42.jpg)
42
Source
• These slides are based on SGG04 and the slides provided by the authors.