1 / 45

Scheduling & Signals

Scheduling & Signals. CS241 Discussion Section Fall 2007 Week 5. Today we’ll…. Review the basic scheduling algorithms Play with signals (as time permits) This section will focus on examples. CPU Scheduling. The CPU Scheduler decides which thread should be in the running state.

duyen
Download Presentation

Scheduling & Signals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scheduling & Signals • CS241 Discussion Section • Fall 2007 • Week 5

  2. Today we’ll… • Review the basic scheduling algorithms • Play with signals (as time permits) • This section will focus on examples.

  3. CPU Scheduling • The CPU Scheduler decides which thread should be in the running state. • It is called when: • A new thread is created • A thread finishes • A clock interrupt occurs • An I/O interrupt occurs • A thread yields

  4. Algorithms • First-Come First-Serve (FCFS) • Shortest Job First (SJF) • Priority • Round Robin (RR)

  5. CPU Scheduling • Scheduling algorithms can be preemptive or non-preemptive • Non-Preemptive: Each thread chooses when to yield the processor • system call • thread finishes • Preemptive: The scheduler can force the thread to yield • time quantum expires • other thread preempts current one

  6. Metrics • Response Time • The time from the submission of a thread until the thread begins to execute • Waiting Time • Total time that a thread spends waiting • Turnaround Time • Thread finish time – thread entry time

  7. Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 First Come First Serve (FCFS) • At t=0, P1 starts

  8. Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 FCFS Example • At t=6, P1 finishes, and P2 starts P1 0 6

  9. Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 FCFS Example • At t=14, P2 finishes, and P3 starts P1 P2 0 14 6

  10. Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 FCFS Example • At t=21, P3 finishes, and P4 starts P1 P2 P3 0 14 21 6

  11. Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 FCFS Example • At t=24, P4 finishes P1 P2 P3 P4 0 14 21 24 6

  12. Metrics for FCFS Example P1 P2 P3 P4 0 14 21 24 6 Average response time (ART): (0+6+14+21)/4 = 10.25 Average waiting time (AWT): (0+6+14+21)/4 = 10.25 Average turnaround time (ATT): (6+14+21+24)/4 = 16.25 P1 waiting time: 0 P2 waiting time: 6 P3 waiting time: 14 P4 waiting time: 21 P1 turnaround time: 6 P2 turnaround time: 14 P3 turnaround time: 21 P4 turnaround time: 24

  13. Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 Shortest Job First (SJF) • At t=0, P4 starts

  14. Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 SJF Example • At t=3, P4 finishes, and P1 starts P4 0 3

  15. Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 SJF Example • At t=9, P1 finishes, and P3 starts P4 P1 0 3 9

  16. Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 SJF Example • At t=16, P3 finishes, and P2 starts P4 P1 P3 0 3 9 16

  17. Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 SJF Example • At t=24, P2 finishes P4 P1 P3 P2 0 3 9 16 24

  18. Metrics for SJF Example P4 P1 P3 P2 0 3 9 16 24 Average response time (ART): (0+3+9+16)/4 = 7 Average waiting time (AWT): (0+3+9+16)/4 = 7 Average turnaround time (ATT): (3+9+16+24)/4 = 13 P4 waiting time: 0 P1 waiting time: 3 P3 waiting time: 9 P2 waiting time: 16 P4 turnaround time: 3 P1 turnaround time: 9 P3 turnaround time: 16 P2 turnaround time: 24

  19. Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 Priority Scheduling Example • At t=0, P2 starts

  20. Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 Priority Scheduling Example • At t=8, P2 finishes, and P4 starts P2 0 8

  21. Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 Priority Scheduling Example • At t=11, P4 finishes, and P3 starts P2 P4 0 8 11

  22. Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 Priority Scheduling Example • At t=18, P3 finishes, and P1 starts P2 P4 P3 0 8 11 18

  23. Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 Priority Scheduling Example • At t=24, P1 finishes P2 P4 P3 P1 24 0 8 11 18

  24. Metrics for Priority Example P2 P4 P3 P1 24 0 8 11 18 Average response time (ART): (0+8+11+18)/4 = 9.25 Average waiting time (AWT): (0+8+11+18)/4 = 9.25 Average turnaround time (ATT): (8+11+18+24)/4 = 15.25 P2 waiting time: 0 P4 waiting time: 8 P3 waiting time: 11 P1 waiting time: 18 P2 turnaround time: 8 P4 turnaround time: 11 P3 turnaround time: 18 P1 turnaround time: 24

  25. Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 Round Robin Example • Each thread runs for 1 second, and then yields to the next thread. P1 P2 P3 P4 0

  26. Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 Round Robin Example • At t=12, P4 finishes (P1, P2, and P3 left in queue) P1 P2 P3 P4 0 12

  27. Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 Round Robin Example • At t=19, P1 finishes (P2 and P3 left in queue) P1 P2 P3 P4 0 12 19

  28. Round Robin Example • At t=23, P3 finishes (P2 left in queue) Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 P1 P2 P3 P4 0 12 19 23

  29. Round Robin Example • At t=24, P2 finishes Process Duration Priority # Arrival Time P1 6 4 0 P2 8 1 0 P3 7 3 0 P4 3 2 0 P1 P2 P3 P4 0 12 19 24

  30. Metrics for Round Robin Example P1 P2 P3 P4 0 12 19 24 P1 waiting time: 13 P2 waiting time: 16 P3 waiting time: 16 P4 waiting time: 9 Average response time (ART): (0 + 1 + 2 + 3) / 4 = 1.5 Average waiting time (AWT): (13+16+16+9)/4 = 13.5 Average turnaround time (ATT): (12+24+23+12)/4 = 17.75 P1 turnaround time: 19 P2 turnaround time: 24 P3 turnaround time: 23 P4 turnaround time: 12

  31. Comparison of Metrics Method ART AWT ATT FCFS 10.25 10.25 16.25 SJF 7 7 13 Priority 9.25 9.25 15.25 RR 1.5 13.5 17.75

  32. Problem 1: Schedules (in pairs) • Say that these three jobs are going to arrive at our scheduler 1 • time unit apart from each other (i.e. one job at time 0, one at time • 1, and one at time 2). Assume that the jobs do not block. • Which arrival order(s) guarantee that the jobs finish • in the order J1 then J2 then J3 if the scheduler uses: • FCFS? • non-preemptive SJF? • preemptive SJF? (use remaining time; break ties with earliest arriving) • RR-1? (assume jobs that arrive during a round are placed on the ready queue immediately) • non-preemptive Priority? (assume higher priority # is more important) • preemptive Priority?

  33. Abrupt change of topic…

  34. Signals • A signal is a software notification to a process of an event. • Signals are asynchronous, so the process can handle them no matter where it is in the program.

  35. User-triggered signals • Ctrl+C triggers SIGINT • Ctrl+Z triggers SIGSTOP • Ctrl+D triggers SIGQUIT • etc. • Run stty -a to see others.

  36. Sending signals from the command line • The Unix kill command sends signals. • The usage is • kill -<signal> <pid> • Run kill -l to see available signals. • The command is called kill because in many cases the default behavior is for the process receiving the signal to die.

  37. Sending signals from inside the program • Use the kill() function to send signals from one process to another. • #include <sys/types.h> • #include <signal.h> • int kill(pid_t pid, int sig); • Examples: • kill(getpid(),SIGTERM); • kill(getppid(),SIGKILL);

  38. Example: d5-example1.c • This example takes in input one character at a time, and shifts the letters forward by one. • Run this program, and send it signals using: • Ctrl+C, Ctrl+Z, etc. • The kill command, from another window

  39. Signal blocking • We can block some signals, so they don’t reach the process right away. • d5-example2.c contains code for blocking SIGINT. • The calls used are: • sigemptyset(&newsigset); • sigaddset(&newsigset,SIGINT); • sigprocmask(SIG_BLOCK,&newsigset,NULL);

  40. Example: d5-example2.c • Run this program. Note that unlike d5-example1.c , this program does not respond to Ctrl+C. You will need to send it a SIGKILL signal to kill it. • This is because the SIGINT signal has been blocked. • (Note: some signals, such as SIGKILL, cannot be blocked.)

  41. Signal sets • A sigset_t is a set of signals, represented as a bit array. • The functions sigemptyset, sigfillset, sigaddset, and sigdelset set and change the signal set. • Call sigemptyset or sigfillset first (else operations are undefined).

  42. Signal Mask • The signal mask is the set of signals that are currently blocked. sigprocmask changes the signal mask: • sigprocmask(SIG_BLOCK,&newset,&oldset) blocks all signals in newset. • sigprocmask(SIG_UNBLOCK,&newset,&oldset) unblocks all signals not in newset. • sigprocmask(SIG_SETMASK,&newset,&oldset) blocks all signals in newset, and unblocks all signals not in newset.

  43. Signal handlers • We can also change what happens when a process receives a signal. • In d5-example3.c, every time the process receives the signal SIGUSR1, it calls the change_shift function, which changes the shift value. • act.sa_handler = change_shift; • act.sa_flags = 0; • sigemptyset(&act.sa_mask); • sigaction(SIGUSR1, &act, NULL);

  44. Example: d5-example3.c • Run this program. • From another window, use kill to send this process a SIGUSR1 signal a few times. • Notice how the output changes each time.

  45. Signal Actions • In the struct sigactionact: • sa_handler is the function called upon receiving the signal • sa_flags are special flags • sa_mask are the additional signals to block while handling this signal • sigaction(SIGUSR1, &act1, NULL) associates this handler with SIGUSR1.

More Related