Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2: Processes Topics –Processes –Threads –Process Scheduling –Inter Process Communication (IPC) Reference: Operating Systems Design and Implementation.

Similar presentations


Presentation on theme: "Chapter 2: Processes Topics –Processes –Threads –Process Scheduling –Inter Process Communication (IPC) Reference: Operating Systems Design and Implementation."— Presentation transcript:

1 Chapter 2: Processes Topics –Processes –Threads –Process Scheduling –Inter Process Communication (IPC) Reference: Operating Systems Design and Implementation (Second Edition) by Andrew S. Tanenbaum, Albert S. Woodhull

2 Processes: The Process Model a) Multiprogramming of four programs b) Conceptual model of 4 independent, sequential processes c) Only one program active at any instant

3 Process Hierarchies Parent creates a child process, child processes can create its own process Forms a hierarchy –UNIX calls this a "process group" Windows has no concept of process hierarchy –all processes created are equal

4 Process States (1) Possible process states –running –blocked –ready Transition among states

5 Process States (2) Lowest layer of process-structured OS –handles interrupts, scheduling Above that layer are sequential processes

6 The Thread Model (1) (a) Three processes each with one thread (b) One process with three threads

7 The Thread Model (2) Items shared by all threads Items private to each thread

8 The Thread Model (3) Each thread has its own stack

9 Thread Usage (1) A word processor with three threads

10 Implementing Threads in User Space A user-level threads package Advantage Fast switching among threads Disadvantage when one thread blocks, the kernel blocks the entire process

11 Implementing Threads in the Kernel A threads package managed by the kernel

12 Process Scheduling When more than one process is runnable, the OS must decide which one to run first. Scheduler makes this decision using some scheduling algorithm Property of good scheduling algorithm –Fairness: Each process gets fair share of CPU –Efficiency: 100% utilization of CPU –Response time: minimize response time for users –Turnaround: minimize the time batch users must wait for output –Throughput: maximize the number of jobs processed per hour

13 Process Scheduling Algorithm Round Robin Scheduling Priority Scheduling Shortest Job First Guaranteed Scheduling Lottery Scheduling

14 Round Robin Scheduling(1) list of runnable processes list of runnable processes after B uses up its quantum Simplest, fairest and most widely used Each process is assigned a quantum (time interval) which it is allowed to run If the process is still running at the end of quantum, the CPU is preempted and given to another process Also the CPU switches when a process blocks All Processes are equally important

15 Round Robin Scheduling(2) Suppose quantum is 20 ms and context switch takes 5 ms (total 25 ms) Cause 20% CPU time wastage If quantum is 500 ms Cause less than 1% CPU time wastage but poor response time

16 Priority Scheduling A scheduling algorithm with four priority classes  Each process is assigned a priority and the runnable process with the highest priority is allowed to run  To prevent high-priority processes from running indefinitely, the scheduler may decrease the priority of currently running process at each clock tick.  I/O bound processes are given highest priority Round Robin

17 Shortest Job First Shortest job first scheduling Appropriate for batch jobs Mean Turnaround time is optimal Consider 4 jobs with run time of a, b, c and d respectively. The mean turnaround time is (4a +3b +2c + d)/4

18 Interprocess Communication (IPC) Processes frequently need to communicate with other processes. Example –In a shell pipeline, the output of the first process must be passed to the second process Generally shared memory is used to establish the connection among processes. This memory must be shared carefully to avoid inconsistency

19 IPC: Race Condition Two processes want to access shared memory at same time Race Condition The situation where 2 or more processes are reading or writing some shared data is called race condition

20 IPC: Critical Regions (1) Four conditions to provide mutual exclusion  No two processes simultaneously in critical region  No assumptions made about speeds or numbers of CPUs  No process running outside its critical region may block another process  No process must wait forever to enter its critical region Critical Region/ Critical Section The part of the program where the shared memory is accessed is called the critical region

21 IPC: Critical Regions (2) Mutual exclusion using critical regions

22 How to Manage Race Condition Mutual Exclusion with Busy Waiting –Strict Alternation –Peterson’s Solution Mutual Exclusion without Busy Waiting –Sleep and wakeup –Semaphore –Message Passing –Monitor

23 Strict Alternation Process 1Process 2 Not a good idea when one process is much slower than the other. Why? Wastage of CPU time by busy waiting Drawback

24 Peterson’s Solution Drawback Busy Waiting

25 Sleep and Wakeup: Consumer Producer Problem Drawback Both may sleep forever Solution Use wakeup waiting bit What happens if more than one consumer? More waiting bit ? Problem is still there.

26 Semaphores: Consumer Producer Problem(1) Semaphore An integer to keep count of wakeups saved for future use Down (semaphore) If value > 0, Decrement it and continue (one stored wakeup is used) If value = 0, Process is put to sleep without completing Down Up (semaphore) if one or more processes were sleeping on that semaphore (unable to complete down operation), one of them is chosen by the system at random and allowed to complete its Down Atomic Action Checking value, changing it and possibly going to sleep

27 Semaphores: Consumer Producer Problem(2) NO busy wait

28 Monitors (1) One have to be very careful while using semaphore Monitor is like class where only one procedure is active at one time It is sufficient to put only the critical regions into monitor procedures as no two processes will ever execute their critical regions at the same time

29 Monitors (2) Only one monitor procedure active at one time Block when buffer full Send signal when buffer has one more slot

30 Monitors (3) Solution to producer-consumer problem in Java (part 1)

31 Monitors (4) Solution to producer-consumer problem in Java (part 2)

32 Message Passing No shared memory N message is analogous to N slots in a shared buffer


Download ppt "Chapter 2: Processes Topics –Processes –Threads –Process Scheduling –Inter Process Communication (IPC) Reference: Operating Systems Design and Implementation."

Similar presentations


Ads by Google