SMD149 - Operating Systems Roland Parviainen November 3, 2005 1 / 45
Outline Overview 2 / 45
Process (tasks) are necessary for concurrency Instance of a program in execution Next invocation of the program creates a new process An active entity A process have its own address space Text region Data region Stack region Process control block 3 / 45
Process states Describes the life of a process Running Executing on the processor Ready Can be executed Blocked Waiting for an event OS maintains a ready list and a blocked list 4 / 45
Process states 5 / 45
Process states When a programs are run: A process is created Added to the ready list When a process is at the head of the list, and the processor is available State transition from ready to running The process is dispatched Interval timer (after a time interval or quantum) is used to avoid monopilization of the processor Alternative: cooperative multitasking 6 / 45
Process control block At process creation: Assign an process identification number (PID) Create a process control block (PCB) In kernel space Stored in a process table The process control block contains: PID state program counter sheduling priority credentials parent process, child processes pointers to data and instructions in memory, allocated resources execution context: processor registers 7 / 45
Context switching Performed by the OS to stop executing a running process and begin executing a previously ready process Save the execution context of the running process to its PCB Load the ready process s execution context from its PCB Must be transparent to processes A process that gains the CPU is said to be dispatched The context switch itself costs CPU time (overhead) 8 / 45
9 / 45
Process operations Operating system (OS) provides fundamental services to processes including: Creating processes Destroying processes Suspending processes Resuming processes Changing a process s priority Blocking processes Waking up processes Dispatching processes Interprocess communication (IPC) 10 / 45
Process creation A process may spawn a new process Leads to a hierarchical structure The parent process creates the child process Exactly one parent process creates a child When a parent process is destroyed, operating systems typically respond in one of two ways: Destroy all child processes of that parent Allow child processes to proceed independently of their parents 11 / 45
Example Unix process creation fork system call: duplicates the running process Two identical copies Return value differs (PID of child to parent, 0 to child) All processes has a parent, except init 12 / 45
13 / 45
Suspend/resume Suspending processes By other users or processes Will not return to blocked/ready state until resumed Introduces more states Allows a user to temporarily prevent processes from running 14 / 45
15 / 45
Interrupts Interrupts enable software to asynchronously respond to signals from hardware May be initiated by a running process (e.g., to indicate error) May be initiated by some event not necessarily related to the running process (e.g., I/O operation) Low overhead Polling is an alternative (synchronous) approach Processor repeatedly requests the status of each device Increases overhead 16 / 45
After receiving an interrupt: The processor completes execution of the current instruction, then pauses the current process The processor then executes one of the kernel s interrupt- handling functions The interrupt handler (stored in an array of pointers called the interrupt vector) determines how the system should respond After the interrupt handler completes, the interrupted process is restored and executed or the next process is executed 17 / 45
18 / 45
Interprocess Communication IPC - Interprocess communication must communicate with one another Signals, message passing, shared memory, remote procedure calls (RPC), sockets 19 / 45
Signals Software interrupts Notifies a process of events No data Handling: Catch the signal Ignore the signal Mask the signal 20 / 45
Signals Example Unix signals SIGKILL - kill process SIGTERM - terminate SIGSTOP - stop a process SIGCONT - restart a process SIGUSR1, SIGUSR2 - user-defined SIGALRM - timer 21 / 45
Example - Linux process states 22 / 45
Multithreading Why threads? Concurrency inside processes Examples GUIs Web server CPU intensive calculations on multiprocessor systems Software design Responsivness Efficiency Cooperation 23 / 45
Thread LWP - Light Weight Process Similar to a process: scheduled on a processor, executes independent of other processes and threads Usually belong to a process (HWP - heavy weight process) A thread share most of process resources - address space, open files, etc within a process can run concurrently and cooperate Data unique to each thread: process registers, stack, thread specific data (TSD), signal masks Only one thread - regular process 24 / 45
25 / 45
Thread states 26 / 45
Thread operations and processes have some common operations Create Exit (terminate) Suspend Resume Sleep Wake Cancel Join 27 / 45
Threading models How to implement threads User-level threads (Many to one, n:1) Kernel threads (One to one, 1:1) Mixed model (Many to many, m:n) 28 / 45
User level threads 29 / 45
Kernel-level threads 30 / 45
Combined model 31 / 45
Thread implementations POSIX (P) Win32 Java threads 32 / 45
alternatives Asynchronous (non blocking) calls As used in some user-level threading implementations Not as easy as threads 33 / 45
Decides which process runs at given time Different schedulers will have different goals Maximize throughput Minimize latency Prevent indefinite postponement Complete process by given deadline Maximize processor utilization Most schedulers try to achieve: Fairness Predictability Scalability 34 / 45
Sheduling levels 35 / 45
Preemptive vs. nonpreemptive scheduling Preemptive processes Can be removed from their current processor Can lead to improved response times Important for interactive environments Preempted processes remain in memory Non-preemptive processes Run until completion or until they yield control of a processor Unimportant processes can block important ones indefinitely 36 / 45
Priorities Static priorities Priority assigned to a process does not change Easy to implement Low overhead Not responsive to changes in environment Dynamic priorities Responsive to change Promote smooth interactivity Incur more overhead than static priorities Justified by increased responsiveness 37 / 45
criteria Processor-bound processes Use all available processor time I/O-bound Generates an I/O request quickly and relinquishes processor Batch processes Contains work to be performed with no user interaction Interactive processes Requires frequent user input 38 / 45
algorithms Decide when and for how long each process runs Make choices about Preemptibility Priority Running time Run-time-to-completion Fairness 39 / 45
First In First Out (FIFO) 40 / 45
Round Robin (RR) 41 / 45
Round Robin (RR) Selfish round-robin scheduling Increases priority as process ages Two queues: Active and Holding Favors older processes to avoid unreasonable delays Quantum size Very large quantum size run for long periods Degenerates to FIFO Very small quantum size System spends more time context switching than running processes Middle-ground Long enough for interactive processes to issue I/O request Batch processes still get majority of processor time 42 / 45
More algorithms Shortest Process First (SPF) scheduling Lower average turn-around time as compared to FIFO Non-preemptive Highest-Response-Ratio-Next (HRRN) scheduling Improves upon SPF scheduling by preventing indefinite postponement Looks at how long the process has been waiting But still non-preemptive Shortest-Remaining-Time (SRT) scheduling Preemptive version of SPF Shorter arriving processes preempt a running process Can result in very large variance of response times: long processes wait even longer than under SPF Not always optimal: context switching overhead and preemption of processes near completion 43 / 45
Example - Linux scheduling Tasks have a priority (-20 to 19) Task: process or thread Sceduler runs every timer interrupt (1ms on IA-32) Task run until the time-slice (quantum) expires, it blocks, or a higher priority task is ready Quantum: 10-200ms One run queue for each priority level Each queue is scheduled using round-robin The task at the front of the highest priority level is dispatched During one epoch, all ready processes will run at least once Epoch: 10*n seconds, n: number of ready tasks Static and effective priority Effective priority is static + modification Tasks that blocks gets a higher priority All operations are constant time (O(1)) 44 / 45
45 / 45
http://www.sm.luth.se/csee/courses/smd/149/ Home work assignment 1 deadline: 14 Nov. Next: Systems programming, lab 1 46 / 45