360 likes | 505 Views
Scheduling & Signals. Process Scheduling. Deciding which process/thread should occupy the resource (CPU, disk, etc). CPU. I want to play. Whose turn is it?. Process 2. Process 3. Process 1. Process Scheduling.
E N D
Process Scheduling • Deciding which process/thread should occupy the resource (CPU, disk, etc) CPU I want to play Whose turn is it? Process 2 Process 3 Process 1
Process Scheduling • Objective of multiprogramming – maximal CPU utilization, i.e., have always a process running • Objective of time-sharing – switch CPU among processes frequently enough so that users can interact with a program which is running • Need: • Scheduling Mechanisms: • Context Switching between Processes • Queueing Capabilities • Scheduling Policies: • Which process to select for running at CPU ?
Context Switch • Switch CPU from one process to another (Performed by scheduler) • It includes: • save PCB state of the old process; • load PCB state of the new process; • Flush memory cache; • Change memory mapping (TLB); • Context switch is expensive (1-1000 microseconds) • No useful work is done (pure overhead) • Can become a bottleneck • Need hardware support
When to schedule? • A new process starts • The running process exits • The running process is blocked • I/O interrupt (some processes will be ready) • Clock interrupt (every 10 milliseconds)
Queuing Diagram for Processes Create Job CPU Ready Queue I/O request in Device Queue I/O Time Slice Expired Update Accounting Fork a Child Create Child Interrupt Occurs Wait for an Interrupt
Simple Processor Scheduling Algorithms • Batch systems • First Come First Serve (FCFS) • Shortest Job First • Interactive Systems • Round Robin • Priority Scheduling • …
FCFS Example The final schedule: P1 (24) P2 (3) P3 (4) 24 27 0 P1 waiting time: 0 P2 waiting time: 24 P3 waiting time: 27 The average waiting time: (0+24+27)/3 = 17
Round-robin • One of the oldest, simplest, most commonly used scheduling algorithm • Select process/thread from ready queue in a round-robin fashion (take turns) • Problem: • Do not consider priority • Context switch overhead
Round-robin: Example Suppose time quantum is: 1 unit, P1, P2 & P3 never block P1 P2 P3 P1 P2 P3 P1 P2 P3 P2 0 10 P1 waiting time: 4 P2 waiting time: 6 P3 waiting time: 6 The average waiting time (AWT): (4+6+6)/3 = 5.33
Time Quantum • Time slice too large • FIFO behavior • Poor response time • Time slice too small • Too many context switches (overheads) • Inefficient CPU utilization • Typical time-slice • 10 to 100 ms
Shortest Job First (SJF) • Schedule the job with the shortest elapse time first • Scheduling in Batch Systems • Two types: • Non-preemptive • Preemptive (more advanced) • Requirement: the elapse time needs to be known in advance • Optimal if all jobs are available simultaneously (provable) • Gives the best possible AWT (average waiting time)
Non-preemptive SJF: Example Do it yourself P4 (3) P1 (6) P3 (7) P2 (8) 3 9 16 24 0 P4 waiting time: 0 P1 waiting time: 3 P3 waiting time: 9 P2 waiting time: 16 The total time is: 24 The average waiting time (AWT): (0+3+9+16)/4 = 7
Comparing to FCFS Do it yourself P1 (6) P2 (8) P3 (7) P4 (3) 14 21 24 0 6 The total time is the same. The average waiting time (AWT): (0+6+14+21)/4 = 10.25 (comparing to 7) P1 waiting time: 0 P2 waiting time: 6 P3 waiting time: 14 P4 waiting time: 21
A Problem with SJF • Starvation • In some condition, a job is waiting for ever • Example: SJF • Process A with elapse time of 1 hour arrives at time 0 • But every 1 minute, a short process with elapse time of 2 minutes arrive • Result of SJF: A never gets to run • What’s the difference between starvation and a dead lock?
Priority Scheduling • Each job is assigned a priority. • FCFS within each priority level. • Select highest priority job over lower ones. • Rational: higher priority jobs are more mission-critical • Example: DVD movie player vs. send email • Problems: • May not give the best AWT • indefinite blocking or starvation a process
Priority Scheduling: Example Do it yourself P2 (8) P4 (3) P3 (7) P1 (6) 24 0 8 11 18 P2 waiting time: 0 P4 waiting time: 8 P3 waiting time: 11 P1 waiting time: 18 The average waiting time (AWT): (0+8+11+18)/4 = 9.25 (worse than SJF)
Set Priority • Every process has a default priority • User can also change a process priority • int nice (int incr); • int getpriority (int which, id_t who); • The which argument may be one of the following values: PRIO_PROCESS, PRIO_PGRP, or PRIO_USER. • indicating that the who argument is to be interpreted as a process ID, a process group ID, or an effective user ID, respectively. • int setpriority (int which, id_t who, int value); • Only a process with appropriate privileges can lower its nice value
Introduction to Signals • What is Signal? • A signal is a software notification to a process of an event. • Why do we need Signals? • In systems we need to enable asynchronous events • Examples of asynchronous events: • Email message arrives on my machine – mailing agent (user) process should retrieve • Invalid memory access happens – OS should inform scheduler to remove process from the processor • Alarm clock goes on – process which sets the alarm should catch it
Basic Signal Concept (Definitions) • Signal is generated when the event that causes the signal occurs. • Signal is delivered when the process takes action based on the signal. • The lifetime of a signal is the interval between its generation and delivery. • Signal that has been generated but not yet delivered is pending. • Process catches signal if it executes signal handler when the signal is delivered. • Alternatively, a process can ignorea signal when it is delivered, that is to take no action. • Process can temporarily prevent signal from being delivered by blocking it. • Signal Mask contains a set of signals currently blocked.
Process Signal Generated Signal Mask Signal delivered Signal Caught by handler Signal not blocked Signal Handler Signal Mask Signal Mask Return from Signal Handler Process Resumed How Signals Work
Generating Signals • Signal has a symbolic name starting with SIG • Signal names are defined in signal.h • Users can generate signals (e.g., SIGUSR1) • OS generates signals when certain errors occur (e.g., SIGSEGV – invalid memory reference) • Specific calls generate signals such as alarm (e.g., SIGALARM)
Command Line Generates Signals • You can send a signal to a process from the command line using kill • kill -l will list the signals the system understands • kill [-signal] pid will send a signal to a process. • The optional argument may be a name or a number. • The default is SIGTERM. • To unconditionally kill a process, use: • kill -9 pid which is • kill -SIGKILL pid.
Command Line Generates Signals • stty –a • CTRL-C is SIGINT (interactive attention signal • CTRL-Z is SIGSTOP (execution stopped – cannot be ignored) • CTRL-Y is SIGCONT (execution continued if stopped) • CTRL-D is SIGQUIT (interactive termination: core dump)
Timers Generate SIGALRM Signals • #include <unistd.h> • unsigned alarm (unsigned seconds); • alarm(20) creates SIGALRM to calling process after 20 real time seconds. • Calls are not stacked • alarm(0) cancels alarm
Examples: Programming Signals From a program you can use the kill system call: #include <sys/types.h> #include <signal.h> intkill(pid_t pid, int sig); Example 8.4: send SIGUSR1 to process 3423: if (kill(3423, SIGUSR1) == -1) perror("Failed to send the SIGUSR1 signal"); Example 8.5: a child kills its parent: if (kill(getppid(), SIGTERM) == -1) perror ("Failed to kill parent");
Programming Signals Example 8.5: a process sends a signal to itself: if (raise(SIGUSR1) != 0) perror("Failed to raise SIGUSR1"); //It is same as kill(getpid(), SIGUSR1); Example 8.8: kill an infinite loop after 10 seconds: int main(void) { alarm(10); for ( ; ; ) ; }
Signal Masks • Process can temporarily prevent signal from being delivered by blocking it. • Signal Mask contains a set of signals currently blocked. • When a process blocks a signal, the OS does not deliver signal until the process unblocks the signal • When a process ignores signal, signal is delivered and the process handles it by throwing it away.
Signal Sets • Signal set is of type sigset_t • Signal sets are manipulated by five functions: • #include <signal.h> • int sigemptyset(sigset_t *set); • int sigfillset(sigset_t *set); • int sigaddset(sigset_t *set, int signo); • int sigdelset(sigset_t *set, int signo); • int sigismember(const sigset_t *set, int signo);
Signal Sets • sigemptyset initializes the set to contain no signals • sigfillset puts all signals in the set • sigaddset adds one signal to the set • sigdelset removes one signal from the set • sigismember tests to see if a signal is in the set
SigInt SigQuit SigKill … SigCont SigAbrt Signal Masks SigMask 0 0 1 … 1 0 Signal SigInt Bit 2, Signal Sigkill Bit 9, Signal SigChld Bit 20 A SIGSET is a collection of signals: #000003 is SIGHUP + SIGINT
SIGPROCMASK • The functionsigprocmaskis used to modify the signal mask. • #include <signal.h> • int sigprocmask(int how, • const sigset_t *restrict set, • sigset_t *restrict oset) • If oset is non-null, the previous value of the signal mask is stored in oset. • ‘how’specifies the manner in which the signal mask is to be modified • SIG_BLOCK • The set of blocked signals is the union of the current set and the set argument. • SIG_UNBLOCKED • The signals in set are removed from the current set of blocked signals. • SIG_SETMASK • The set of blocked signals is set to the argument set.
Example: Initialize Signal Set: if ((sigemptyset(&twosigs) == -1) || (sigaddset(&twosigs, SIGINT) == -1) || (sigaddset(&twosigs, SIGQUIT) == -1)) perror("Failed to set up signal mask");
Example: Add SIGINT to Set of Blocked Signals sigset_t newsigset; if ((sigemptyset(&newsigset) == -1) || (sigaddset(&newsigset, SIGINT) == -1)) perror("Failed to initialize the signal set"); else if (sigprocmask(SIG_BLOCK, &newsigset, NULL) == -1) perror("Failed to blockSIGINT"); If SIGINT is already blocked, the call to sigprocmask has no effect.