operating systems cse 411
DESCRIPTION
Operating Systems CSE 411. CPU Management Sept. 25 2006 - Lecture 9 Instructor: Bhuvan Urgaonkar. Last time Pre-emptive scheduling Lottery, Reservation, … Today Signals Introduction to accounting CPU accounting Threads. Interrupts/Traps and Signals Compared. Recap interrupt/trap - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/1.jpg)
Operating SystemsOperating SystemsCSE 411CSE 411
CPU ManagementCPU Management
Sept. 25 2006 - Lecture 9Sept. 25 2006 - Lecture 9
Instructor: Bhuvan UrgaonkarInstructor: Bhuvan Urgaonkar
![Page 2: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/2.jpg)
• Last time– Pre-emptive scheduling– Lottery, Reservation, …
• Today– Signals– Introduction to accounting
• CPU accounting
– Threads
![Page 3: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/3.jpg)
Interrupts/Traps and Signals Compared
• Recap interrupt/trap– Used to notify the OS that something has happened that it
needs to attend to• E.g. 1: Network packet arrived at the Ethernet card• E.g. 2: Process made a system call• E.g. 3: Process performed division by zero• E.g. 4: CPU about to melt!
– Interrupt/trap handlers implemented by the OS; their addresses stored at fixed locations indexed by their numbers
– Usually handled right away• Linux: Top-half right away, bottom-half at leisure• Interrupts: Asynchronous generation, synchronous handling• Traps: Synchronous generation and handling
![Page 4: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/4.jpg)
Interrupts/Traps versus Signals
• Used to notify the OS that something has happened that it needs to attend to
• Interrupt/trap handlers are implemented by the OS
• Usually handled right away
• Interrupts asynch. generated, traps synch. generated
• Used to notify a process that something has happened that it needs to attend to
• Signal handlers may be implemented by the process
• Handled when the process is scheduled next
• Generated and handled synchronously– May be due to an asynch.
interrupt, but the signal will be generated synch. WHY?
![Page 5: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/5.jpg)
Signals
• Fixed number of signals defined by the OS and made known to the processes– UNIX: signal.h
• A process may implement its own handler for one or more signals– Allowed for most signals, not allowed for SIGKILL– Each PCB has indicators for which signals were received
and are due– Upon getting scheduled, the handler for signals received are
executed in some order• Okay from the process point of view since it is unaware
of when it is being scheduled or taken off the CPU
![Page 6: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/6.jpg)
More on signals• Each signal has a default action which is one of the following:
– The signal is discarded after being received– The process is terminated after the signal is received– A core file is written, then the process is terminated– Stop the process after the signal is received
• Each signal defined by the system falls into one of five classes:– Hardware conditions– Software conditions– Input/output notification– Process control
– Resource control
![Page 7: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/7.jpg)
Examples of signals
• SIGHUP 1 /* hangup */ • SIGINT 2 /* interrupt */• SIGQUIT 3 /* quit */ • SIGILL 4 /* illegal instruction */• SIGABRT 6 /* used by abort */ • SIGKILL 9 /* hard kill */• SIGALRM 14 /* alarm clock */ • SIGCONT 19 /* continue a stopped process */ • SIGCHLD 20 /* to parent on child stop or exit */
![Page 8: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/8.jpg)
System calls related to signals
• kill(signal_num, pid) - to send a signal
• signal(signal_num, handler) - to handle it
![Page 9: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/9.jpg)
Signal Handling (Visual)
time
Signal due indicators
Signal is not due
Signal is due
Timer interruptSystem call (trap) Calls kill() to
send a signal to P (trap)
Accesses illegalmemory location (trap)
Runs SIGSEGV handler; dumps core and exits
PCB of P
ISR run; assumeP scheduled again
Sys call done; Par scheduled
Timer interrupt
OSParent of P
These are the events that P sees (its view of what is going on)
![Page 10: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/10.jpg)
Some example programs to show signal handling in
UNIX
![Page 11: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/11.jpg)
CPU Accounting
![Page 12: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/12.jpg)
CPU Accounting• OS keeps track of each process’s CPU usage
– Scheduler needs this information– Billing in commercial settings
• E.g., Sun’s Grid: $1 per CPU-hour– Prevent resource exhaustion due to malicious or erroneous
processes• E.g., fork bomb!
– Solution: Limit the number of processes a process can fork
• Lets look at the top utility– OS provides syscalls for getting certain usage information
• Look at getrusage()
• Issue: Who should be charged for CPU usage of the OS?– Consider the example of an I/O-intensive process– Suggested reading: “resource containers” paper (not part of the
syllabus)
![Page 13: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/13.jpg)
Threads
![Page 14: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/14.jpg)
What is a Thread?
• A basic unit of CPU utilization like a process (not necessarily known to the OS though)
• “Smaller” than a process– Part of a process– Shares code + data + some other OS resources with other
threads that belong to the same process• Files and signal handlers
![Page 15: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/15.jpg)
User Threads• Thread management done by user-level threads library• OS doesn’t know about the existence of these threads• Three primary thread libraries:
– POSIX Pthreads– Win32 threads– Java threads
![Page 16: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/16.jpg)
Kernel Threads• OS sees and manages these threads• OS provides system calls to create, terminate,
etc. (just like the system calls it provides for processes)
• Examples– Windows XP/2000– Solaris– Linux– Tru64 UNIX– Mac OS X
![Page 17: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/17.jpg)
Benefits• Responsiveness• Resource Sharing• Economy• Utilization of MP Architectures
![Page 18: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/18.jpg)
Multithreading Models
• Many-to-One
• One-to-One
• Many-to-Many
![Page 19: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/19.jpg)
Many-to-One• Many user-level threads mapped
to single kernel thread• Examples:
– Solaris Green Threads– GNU Portable Threads
![Page 20: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/20.jpg)
Many-to-One Model
![Page 21: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/21.jpg)
One-to-One• Each user-level thread maps to
kernel thread• Examples
– Windows NT/XP/2000– Linux– Solaris 9 and later
![Page 22: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/22.jpg)
One-to-one Model
![Page 23: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/23.jpg)
Many-to-Many Model
• Allows many user level threads to be mapped to many kernel threads
• Allows the operating system to create a sufficient number of kernel threads
• Solaris prior to version 9• Windows NT/2000 with the ThreadFiber
package
![Page 24: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/24.jpg)
Many-to-Many Model
![Page 25: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/25.jpg)
Two-level Model
• Similar to M:M, except that it allows a user thread to be bound to kernel thread
• Examples– IRIX– HP-UX– Tru64 UNIX– Solaris 8 and earlier
![Page 26: Operating Systems CSE 411](https://reader036.vdocument.in/reader036/viewer/2022070411/5681472f550346895db46c06/html5/thumbnails/26.jpg)
Two-level Model