330 likes | 359 Views
Processes and threads. Lecture 4 ~ Fall, 2007 ~. Contents. Processes Threads Scheduling. Process definition. OS abstraction to represent what is needed to run a single program A program in execution , including the current values of PC, registers, and variables
E N D
Processes and threads Lecture 4 ~ Fall, 2007 ~
Contents • Processes • Threads • Scheduling TUCN. Operating Systems. Lecture 4
Process definition • OS abstraction to represent what is needed to run a single program • A program in execution, including the current values of PC, registers, and variables • Formally, a process is a sequential stream of execution in its own address space TUCN. Operating Systems. Lecture 4
The process model Process and program • The program is a static (inactive) entity, whereas the process is an active one • A process is an activity of some kind • A process has a program, input, output, and a state • There are two parts of a process • sequential execution: no concurrency inside a process; everything happens sequentially • process state: everything that process interacts with (registers, memory, files, etc) TUCN. Operating Systems. Lecture 4
The process modelThe context of this presentation • Single-processor systems • Pseudo-parallelism • Multiprogramming • Switching among processes • A single processor may be shared among several processes • Scheduling algorithm TUCN. Operating Systems. Lecture 4
Process creation • Automatically by the system • At system initialization • Foreground and background processes • By another process • A system call for process creation • Situations • A process needs help doing some computation • A user action – interaction with the shell TUCN. Operating Systems. Lecture 4
Process hierarchies • A process creates a new process • The two processes are distinct • Process hierarchy • Parent–Child Relationship • The child can itself create new processes • Tree of processes • No hierarchy • All processes are equal TUCN. Operating Systems. Lecture 4
Process termination • Voluntary – using a special system call • Normal exit • Error detection exit • Ex.: inexistent files, insufficient or incorrect input • Involuntary – receiving an interruption • Initiated by the system due to a fatal error • Ex.: illegal instructions, division by zero etc. • Initiated (killed) by another process TUCN. Operating Systems. Lecture 4
Process states • Running • using the CPU at that moment (executed by the CPU) • Ready • ready to be executed, but no CPU available • Blocked • wait for same event to occur • Just created (optional) • waiting for same resources to be allocated • Terminated (optional) • keeping same information about the exit state TUCN. Operating Systems. Lecture 4
Process states transitions • Just created to Ready or Running • Running to Ready • suspended by the scheduler • time slice expired • Ready to Running • its turn comes again • selected by the scheduler • Running to Blocked • wait for some event to occur • Blocked to Ready • the awaited event occurs • Running to Terminated Just created (optional) Terminated (optional) Running Blocked Ready TUCN. Operating Systems. Lecture 4
Implementation of processes • OS maintains a process table • each process is allocate an entry – PCB (Process Control Block) • used as a repository when the process is suspended or blocked • Information in a PCB • process state (new, ready, running, waiting, or halted) • the value of the program counter (PC) • the value of the CPU registers • CPU scheduling information (process priority etc.) • memory management information (pointers etc.) • accounting information (the amount of CPU and real time used, time limits etc.) • I/O status information (outstanding I/O req., I/O devices allocated, list of open files etc.) TUCN. Operating Systems. Lecture 4
Relation between processes • Independent processes • the set of object accessed by them are disjoint • a process cannot affect or be affected by other processes executing in the system • Competing (concurrent) processes • processes share resources but there is no information exchanged between them • a process can affect or be affected by the other processes executing in the system • Cooperating processes • exchange information either by using shared data objects or through message passing TUCN. Operating Systems. Lecture 4
The thread model (1) • Process model • resource grouping and execution • Processes • group related resources together • Threads of one process • describe an sequential execution within a process • share the same address space and resources of the process • each thread has its own PC, registers and stack of execution • there is no protection between threads in one process • lightweight processes, multithreading TUCN. Operating Systems. Lecture 4
The thread model (2) Three processes each with one thread Multiprogramming One process with three threads Multithreading TUCN. Operating Systems. Lecture 4
The thread model (3) TUCN. Operating Systems. Lecture 4
Thread usage (1)A multithreaded document editor TUCN. Operating Systems. Lecture 4
Thread usage (2)A multithreading Web server TUCN. Operating Systems. Lecture 4
Implementing threads in user space (1) TUCN. Operating Systems. Lecture 4
Implementing threads in user space (2) • The kernel knows nothing about threads • The approach is suitable for OS that does not support threads • The threads run on top of a runtime system • Each process has its own thread table • Advantages • Thread switching and scheduling is faster than to trapping the kernel • Each process can have its own customized scheduling algorithm • Scale better • Disadvantages • The implementation of blocking system calls • The need that a thread voluntarily gives up the CPU TUCN. Operating Systems. Lecture 4
Implementing threads in the kernel (1) TUCN. Operating Systems. Lecture 4
Implementing threads in the kernel (2) • The kernel knows about the threads • The kernel schedules all the threads • The kernel has a thread table • Advantages • The kernel can switch between threads belonging to different processes • No problem with blocking system calls • Disadvantages • Greater cost (time and resources) – Solution: recycling TUCN. Operating Systems. Lecture 4
Hybrid implementation TUCN. Operating Systems. Lecture 4
Introduction to scheduling (1) • Scheduler • OS component that decides what process will be run and for how long • uses a scheduling algorithm • Processes • Compute-bounded (CPU-bounded) • I/O-bounded TUCN. Operating Systems. Lecture 4
Introduction to scheduling (2) TUCN. Operating Systems. Lecture 4
Introduction to scheduling (3) The moment of scheduling • Process creation • Process termination • Process blocking • Interrupt occurrence • Clock interrupt occurrence • non-preemptive scheduling algorithms • preemptive scheduling algorithms TUCN. Operating Systems. Lecture 4
Introduction to scheduling (4) Categories of scheduling algorithms • Batch • Non-preemptive algorithms • Preemptive algorithms with long time quanta • Reduces processes switches and increase performance • Interactive • Preemptive algorithms are needed • Real-Time • Preemption normally used, but sometimes not needed TUCN. Operating Systems. Lecture 4
Scheduling algorithm goals (1) • All systems • Fairness • giving each process a fair share of CPU • equivalent processes get equivalent CPU times • Balance – keeping all parts of the system busy • Batch systems • Throughput – maximize jobs per hour • Turnaround time – minimize time between submission and termination TUCN. Operating Systems. Lecture 4
Scheduling algorithm goals (2) • Interactive systems • Response time – respond (react) to request quickly • Proportionality – meet, if possible, user’s expectations • Real-time systems • Meeting deadlines – avoid losing data TUCN. Operating Systems. Lecture 4
Scheduling in batch systems (1)First-Come First-Served • Is non-preemptive • Simple to understand and implement • The average waiting time (a.w.t.) depends on the process order and execution time • Example 1 • P1:24, P2:3, P3:3 (process : execution time) • waiting time for P1 is 0 ms, for P2 is 24, and for P3 is 27 • a.w.t. = 17 ms • Example 2 • P2:3, P3:3, P1:24 (process:execution time) • waiting time for P1 is 6 ms, for P2 is 0, and for P3 is 3 • a.w.t. = 3 ms TUCN. Operating Systems. Lecture 4
Scheduling in batch systems (2)Shortest Job First • Runtime is known in advance • Is the optimal algorithm (only) when all the jobs are available simultaneously • Non-preemptive • Example • 0:P1:8,1:P2:4, 2:P3:9, 3:P4:5(submit time:process:run time ) • a.w.t. = (0 + (8-1) + (17-3) + (12-2))/4 = 7.75 • Preemptive Shortest remaining time next • Example – the same as previously • a.w.t. = ((10-1)+(1-1)+(17-2)+(5-3))/4 = 6,5 TUCN. Operating Systems. Lecture 4
Scheduling in interactive sys. (1)Round-Robin Scheduling • Each process is assigned a time interval • time quantum or slice • Is a preemptive algorithm • Maintain a FIFO list for ready processes • The length of the quantum • Too short lower the CPU efficiency • Too large poor response to interactive requests • 20-50 msec is a reasonable compromise TUCN. Operating Systems. Lecture 4
Scheduling in interactive sys. (2)Priority scheduling • Each process has a priority assigned • The greatest priority runnable process is always run • Give a chance to other processes change the priority of the running process or assign it a quantum • Priorities assignment • statically • dynamically • Priority classes • Use priority scheduling between classes • Use another scheduling algorithm with each class TUCN. Operating Systems. Lecture 4
Bibliography [Tann01] Andrew Tannenbaum, “Modern Operating Systems”, second edition, Prentice Hall, 2001, pg. 71 – 100, pg. 132 – 151. TUCN. Operating Systems. Lecture 4