400 likes | 429 Views
This lecture covers monolithic kernel vs. microkernel, program vs. process vs. thread, interrupts, process states and scheduling, focusing on UNIX and Windows OS. It delves into examples of processes, threads, interruptions, context switching, and scheduling objectives and criteria.
E N D
Process Management Operating Systems Lecture 3, 27 March 2003 Mr. Greg Vogl Uganda Martyrs University
Overview • Monolithic kernel vs. microkernel (lecture 1) • Program, Process, Thread • Interrupts • Process States • Process Scheduling • Processes in UNIX • Processes in Windows Operating Systems: Process Management
1. Monolithic Kernel Operating Systems: Process Management
Microkernel Operating Systems: Process Management
2. Program • Series of commands • Instructions only (usually not data) • Stored on disk, copied into memory • Can be in use by many users and processes at the same time • Also called software • Includes shell scripts, batch files Operating Systems: Process Management
Process • Variables point to instructions + data • Loaded in memory • context contains all process state info. • stored in Process Control Block • One user and one program per process • Also called task • (or job for batch processes) Operating Systems: Process Management
Process Context • program counter • register indicating next program instruction to run • other registers • accumulator, index/address, status, general • stack of subroutine return addresses • subroutines call other subroutines • values of local and global variables • pointers to open data files • user and terminal number (in multi-user OS) Operating Systems: Process Management
Examples of Processes • User Processes • Shells • Text editors, databases • Background jobs (end with & in UNIX) • System Processes • Memory management, process scheduling • daemons (system background processes) • Mail and print servers Operating Systems: Process Management
Thread • Also called sub- or lightweight process • Little private memory; memory is shared • Subdivides work of the process • Threads are managed by the process • Reduces high overhead for creating processes and context switching • Java was designed to write thread-based programs Operating Systems: Process Management
Thread Components • At minimum, every thread has its own • program counter • stack • Program text shared with other threads • Each procedure has a frame to hold its local variables • Heap of objects is shared by all threads Operating Systems: Process Management
Uses of Threads • Servers (database, mail, print, etc.) • one thread per client request • Network server • one thread per connection • Time-sharing • one thread per user • Real-time factory control • one thread per device Operating Systems: Process Management
3. Interrupt • Signal hardwareCPU requesting services • CPU postpones its work to handle interrupt • Interrupt handler routines are part of OS code • OS switches modes (usersupervisor) • Interrupts are prioritised • Stack used to store multiple interrupt levels • Programs can mask (ignore) some interrupts • Others unmaskable (to avoid losing data) Operating Systems: Process Management
Examples of Interrupts • Key pressed • Disk or other I/O task finished • System clock Operating Systems: Process Management
Software Interrupts • Also called traps or exceptions • Processor interrupted by programs • Examples • Division by 0 error • Bus error • Array out of bounds • Buffer overflow Operating Systems: Process Management
Context Switching • Preserve state of current process • Start or re-start another process • Performed by dispatcher (OS component) • When to change context? • Overhead cost (takes CPU time) • Processes prioritised by properties Operating Systems: Process Management
4. Process States • Runnable • Running (only one per processor) • Ready (waiting for its turn to run) • Blocked • Explicit e.g. wait() until a child terminates • Implicit e.g. read() • Blocked by another process • New (not yet allowed to wait its turn) • Suspended (e.g. swapped to virtual memory) • Terminated (finished but still using resources) Operating Systems: Process Management
Process State DiagramRitchie p. 62 entry (HLS) I/O completion ready blocked (LLS)dispatch timeout I/O wait (MLS)suspend running resume suspend resume suspend termination blocked suspended ready suspended I/O completion Operating Systems: Process Management
5. Scheduling • Assign each process time to use CPU • Determine sequence (order), timing (when) • Conflicting objectives need compromise • Scheduling levels • High: whether to admit a new process • Medium: suspend/resume a process • Low: dispatch (run) a ready process Operating Systems: Process Management
Scheduling Objectives • Maximise throughput • Give all users a “fair” (not equal) chance • Provide tolerable performance • Response time for on-line user • Turnaround time for batch users • Degrade performance gracefully • OK to be slow but avoid complete collapse • Be consistent, predictable over time Operating Systems: Process Management
Scheduling Criteria • Priority which may be either/both: • Assigned to job by user • Determined by properties of the job • Class of job (real-time > on-line > batch) • Resources needed (CPU time, memory) • I/O or CPU bound (the aim is a balance) • Resources already used • Time already waited Operating Systems: Process Management
Types of Scheduling Policies • Preemptive (Used by most OS today) • OS stops one process to run another • Non-preemptive • process runs until termination or I/O wait • Cooperative (Used by Windows 3.11) • Programs must voluntarily give up CPU • Not managed by OS; trusts programmers • If a process hangs the whole PC hangs Operating Systems: Process Management
Scheduling Policies • First come first served (FCFS/FIFO) • Shortest job first (SJF) • Shortest remaining time (SRT) • Highest response ratio next • Round robin (RR) • Multi-level feedback queues (MFQ) Operating Systems: Process Management
First come first served • Also called first in first out (FIFO) • Process waiting longest is first in queue • Favours long jobs • high run-time/wait-time ratio • Favours CPU-bound jobs • I/O devices underused Operating Systems: Process Management
Shortest job first (next) • Run job with shortest estimated run time • Long jobs may be delayed indefinitely • JCL commands can specify run time Operating Systems: Process Management
Shortest remaining time • Run job w/ shortest est. remaining time • Highly favours short jobs • Long jobs will be delayed indefinitely • Requires estimating total run time • Requires measuring elapsed run time Operating Systems: Process Management
Highest response ratio next • P = (time waiting + run time) / run time • Priority based on two factors • Favours shorter jobs • Guarantees a job cannot be starved Operating Systems: Process Management
Round Robin • Each process given a set time slice • Pre-emption at end of time quantum • Hardware timer generates interrupts • After running, go to back of queue • Used in most interactive operating systems • How large should each time slice be? • Small high context switch overhead • Large user response time is reduced • In practice, about 10-20 ms Operating Systems: Process Management
Multi-level feedback queues • Separate queues for different priorities • New process level 1 FIFO (highest) • After timeout level 2 FIFO, etc. • Lowest level is Round Robin • Adapts to past process behaviours • high CPU usage reduced access level • Long processes may starve • if wait time is long, promote/increase slice Operating Systems: Process Management
6. UNIX process creation • PID 0: sched (process scheduler) • PID 1: init (ancestor of other processes) • daemons (automatic, for sys. admin.) • getty (one per terminal) • login (lets users log in) • shell (lets users run programs) • user-initiated processes (run from shell) Operating Systems: Process Management
UNIX fork() • fork() produces new “child” process • Child is exact copy of parent • Starts in same program, at same place • Exact copy of memory space • (globals, stack, heap object) • Both calls to fork() return a value • Parent fork() returns process ID of child • Child fork() returns 0 Operating Systems: Process Management
UNIX exec() • Execute (load, run) another program • Overwrite calling process in memory • Uses same PID; not a new process • Used after fork() to start new process • Parent waits for child process to finish • Run in background to not hold up shell • myprogram & Operating Systems: Process Management
UNIX scheduling • Varies with the flavour of UNIX • Often dynamic priority, round robin, MFQ • Processes have number priority values • 0=highest, 60=lowest, 20=initial/default • Users can reduce priority using nice • nice -10 myprog # makes priority 30 • Only superuser can increase priority • nice --10 myprog # makes priority 10 Operating Systems: Process Management
UNIX ps • Command to display process status • ps # list PIDs, TTYs, CPU time, name • ps -a # all terminal-created processes • ps -e # everything (incl. daemons) • ps -f # full list (more details) • ps -l # long list (ppid, priority, size, nice) Operating Systems: Process Management
Other UNIX commands • top – process list, continuously updated • jobs – list jobs running in current shell • bg, fg – send jobs to back/foreground • at – run batch job at specified time(s) • kill – send signal to terminate process • sleep – pause for some seconds Operating Systems: Process Management
7. Processes in Windows • Every process treated as thread • Process can create additional threads • Scheduler operates over all threads • Each process has virtual address space • No parent-child process relationship • Environment copied to new processes Operating Systems: Process Management
Dynamic Link Libraries (DLLs) • Executable code routines • Linked into application only at run time • Can be shared by several programs Operating Systems: Process Management
Starting Programs • Type program name (maybe full path) • MS-DOS prompt, run dialog box, Explorer • Double-click icon • Desktop, Explorer, My Computer • Click shortcut • Start or Programs menu, QuickLaunch • Start when the computer starts • autoexec.bat, config.sys, Startup menu Operating Systems: Process Management
Managing Tasks • Ways to close programs • Ctrl-C; File, Exit; Close button (X); Alt-F4 • Press Ctrl-Alt-Del to view task manager • End Task: stop non-responding program • Ctrl-Alt-Del again to shut down/restart Operating Systems: Process Management
WinMe System Information • Loaded modules (exe, dll, etc.) • Name, version, size, file date, mfg., path • Running tasks • Name, path, PID, priority, version, size • Startup programs • Program, command, user name, location • Print jobs Operating Systems: Process Management