Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems Process Managment.

Similar presentations


Presentation on theme: "Operating Systems Process Managment."— Presentation transcript:

1 Operating Systems Process Managment

2 Processes

3 Process Definition A process is a program in execution.

4 Multi-Programming Modern Operating Systems, 3rd ed., Andrew Tanenbuam 

5 Process Creation Principal events that cause process creation:
1. System initialization 2. Execution of a process creation system by a running processes 3. User request to create a new process 4. Initiation of a batch job

6 Creating a new process in Unix
Fork: Unix system call to create an copy of the invoking process. The calling process is called the parent, the newly created process is the parent. Fork is standardized by POSIX (Portable Operating System Interface). Implemented by most UNIX (including Mac OS as of Leopard) and is closely adhered to in Linux.

7 Question Who & where is fork implemented?

8 Fork Details Everything gets copied, PCB is an exact copy, address space, registers, file descriptors, etc. (Process id would be different). The child process starts off and the first thing it does is return from the very system call its parent made! How can the child’s and parent’s execution path separate? Hint: Interface: int fork ()

9 Fork Skeleton A multi-perspective episodic approach, Jae C. Oh

10 Possible Fork implementation
A multi-perspective episodic approach, Jae C. Oh

11 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 are created equal

12 Foreground, Background & Daemons
Foreground processes: interacting with user Background processes: running without input from user What is the significance in windowed systems? Daemons: Wait in background to handle event or activity.

13 Process Life Cycle

14 Process States Running: Process is currently running in the CPU.
Ready: Process is ready to run (waiting for scheduler to choose it to run). Blocked: Process is waiting for some external event to happen (example process is waiting for an file read).

15 Process State Diagram Modern Operating Systems, 3rd ed., Andrew Tanenbuam 

16 Process Termination Conditions which terminate processes
Normal exit (voluntary) Error exit (voluntary) Fatal error (involuntary) Killed by another process (involuntary) Kill command A Daemon process finds Zombie processes Within a program

17 Process System Calls Modern Operating Systems, 3rd ed., Andrew Tanenbuam 

18 Using Exec & Waitpid A multi-perspective episodic approach, Jae C. Oh

19 Question What happens if you don’t put exit system call at the end of your program?

20 Implementation of Processes
Fields of a process data structure

21 Scheduling Introduction to Scheduling (1)‏
Bursts of CPU usage alternate with periods of I/O wait a CPU-bound process an I/O bound process

22 When to schedule? When a process is created Parent or child?
When a process exits When a process requests an I/O (I.e., blocked)‏ When a process blocks due to C.R. mutual exclusion When an I/O interrupt occurs When a clock interrupt occurs (e.g., quantum expires)‏

23 Introduction to Scheduling (2)‏
Scheduling Algorithm Goals

24 Preemtpion Pre-emptive Scheduler – removes process P from CPU without P's consent, after some fixed amount of time. Non-Pre-emptive Scheduler – Process P must actively release the the CPU (Yield, Exit, Sleep, or blocking system call).

25 Metrics Throughput: The number of processes a system can complete in a set amount of time. i.e. # per minute, # per hour, etc. Turn Around time: The time a process takes from being created to being finished. Response Time: The time an interactive process takes to respond to user input.

26 Scheduling algorithms for batch systems
FCFS Shortest Job First All jobs must be available simultaneously (is provably optimal). Shortest Remaining Time Next Preemptive verson of SJF

27 Scheduling in Batch Systems (2)‏
Three level scheduling

28 Scheduling in Interactive Systems
Round Robin Scheduling list of ready processes list of ready processes after B uses up its quantum

29 Time Quota If time quota is too long, what can happen?
If time quota is too short what happens?

30 Priority Scheduling We can assign an importance factor to our processes. Priority can be static or dynamic Why would we ever change priority? Higher priority equals longer time quota? Or All good priorities should finish first?

31 Scheduling in Interactive Systems
A scheduling algorithm with four priority classes

32 Shortest Job First Scheduling
Always produces the minimum average response time. We need to know how long each process/thread takes!

33 Lottery Scheduling Each runable entity is given a certain number of tickets. The more tickets you have, higher your odds of winning. Trade tickets? Problems?

34 Fair Share Scheduler Schedule not only based on individual process, but process's owner. N users, each user may have different # of processes. Does this make sense on a PC?

35 Policy versus Mechanism
Separate what is allowed to be done with how it is done a process knows which of its children threads are important and need priority Scheduling algorithm parameterized mechanism in the kernel Parameters filled in by user processes policy set by user process

36 User-level Thread Scheduling
Possible scheduling of user-level threads 50-msec process quantum threads run 5 msec/CPU burst

37 Kernel-Level Thread Scheduling
Possible scheduling of kernel-level threads 50-msec process quantum threads run 5 msec/CPU burst

38 1. User-Level Threads: Thread data structure maintained in user-space.
2. Kernel-Level Threads: Thread data structure maintained in kernel. 3. Hybrid Threads: Mixture of User & Kernel. 4. Process data structure: Address space, open files, list of threads. 5. Thread data structure: registers, stack, state. 6. Context Switch: Assembly needed to save/load regs. MMU updated. 7. Critical region: Code where data is shared amongst processes. 8. Mutual Exclusion: Only one process has access to a CR. 9. Busy waiting: Keep checking (loop) some condition. 10. Deadlock: Process(es) are waiting (sleeping) for an event that can never occur. No work is done. 11. Starvation: Process(es) wait for an event that can happen, but doesn’t. 12. Semaphore: A counter and queue, implements P (down) & V (up). 13. Mutex: IS A TYPE OF SEMAPHORE! Used for mutual exclusion. 14. Monitor: Part of a language, provides mutual exclusion. 14. Throughput, turn around time, 15. Round-Robin, Priority, Multi-queue, MLFQ, Lottery, SJF

39 Round Robin Scheduler

40 Round Robin Scheduler RR scheduler is a pre-emptive scheduler.
Each job is given a fixed quanta of time to run in the CPU. All jobs are treated equally (no priority classes). Straight forward and starvation free.

41 Round Robin Example Quanta = 10ms Process Arrival Time Run Time P0 22
22 P1 5 15 P2 13 8

42 Round Robin Example Time Current Process Next Process Ready Queue
Notes EMPTY P0 P0, P0 arrives 5 N/A P1, P1 arrives 10 P1 Quanta Expired 13 P0, P2 P2 arrives 20 P2, P1 30 P2 P1, P0 38 P2 Completes 43 P1 Completes 45 P0 Completes

43 Question Other than quanta expiration what are two other reasons the scheduler might have to choose a different job to run?

44 Completely Fair Scheduler

45 Completely Fair Scheduler
Tries to model an ideal multi-tasking CPU: Run each task at 1/ N equal speed, in parallel. When a process arrives, record the current system time. Keep track of how long each process has been waiting for the CPU (update the wait time based on process ID and the number of processes in the queue). Decrement the wait value when the process gets on the CPU.

46 Implementation Details
Use a Red/Black Tree to keep track of which process has the longest wait. Red/Black Tree: Self balancing binary tree Operations take Log(N)

47 Multi Level Feedback Queues

48 Multi Level Queue Multiple Queues tiered by priority.
All jobs from highest priority queue must be empty to begin next lower queue. Within a given queue, any other scheduling algorithm can be used (FCFS, SJF, RR). Jobs do not change queue. Provides a strict way of grouping together jobs from different users but same priority.

49 Multi Level Feedback queue
Multiple Queues Most jobs are mixture of I/O and CPU. Highest priority queue is for I/O heavy jobs. Lowest priority queue is for CPU heavy jobs. All jobs start at highest priority queue. Jobs that are preempted (use all of time quanta) are booted to lower priority queue. Quanta generally increases indirectly with priority. Jobs that block on I/O go up to next higher priority queue. Job is pre-empted if higher priority jobs arrives.

50 Multi-Level Feedback Queue Example

51 Question Between Multi-Level Queue and Multi-Level Feedback Queue which is more likely to have starvation?

52 Threads

53 The Thread Model Per Process items shared by all threads in a process.
Per thread Items private to each thread.

54 Each thread has its own stack
The Thread Model Each thread has its own stack

55 Thread library calls thread_create thread_exit thread_yield Etc.

56 Thread Motivation Blocking system call needs to wait on some I/O, e.g. disk read. Sometimes called synchronous call. Entire process is put into blocked state. Non-Blocking system call (i.e. asynchronous) kicks off a request (typically to a device) but doesn’t wait for completion. Polling, signals, callbacks or threads would be needed to handle the result.

57 A word processor with three threads
Thread Usage Example 1 A word processor with three threads

58 Threads A thread (or thread-of-execution) is a component of a process that allows intra-process parallelization. A thread has stack and its own set of registers, but shares process address space with other threads within the same process.

59 Thread Example Web-Server processes (e.g. Google) takes thousands of requests per second. Modern Operating Systems, 3rd ed., Andrew Tanenbuam 

60 Process vs Thread Modern Operating Systems, 3rd ed., Andrew Tanenbuam 

61 Implementation of Threads
In user space In kernel space

62 User Threads vs. Kernel Threads
Threads can be implemented in either the kernel or user space. Kernel threads: Kernel manages thread state. Scheduling can be pre-emptive (we can force out a thread). Easy support for blocking system calls. System calls required for scheduling, update or creation. User threads: Kernel is completely unaware of threading. User process/library manages thread state. Pre-emption is not possible, threads must leave on their own. Generally require non-blocking system calls (in OS). No System call for scheduling, update or creation.

63 Implementing Threads in User Space
A user-level threads package

64 User level thread implementation
+ Fast switching between threads (no kernel code needed). + Can do for existing non-multithreaded OS + Each process can have its own customized scheduling algorithm/policy - Blocking system calls can we change system calls to be non-blocking calls? May be the select system call? - No preemption among threads

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

66 Hybrid Implementations
Multiplexing user-level threads onto kernel- level threads

67 Scheduler Activations
Hybrid User & Kernel Space Threads A thread blocked, should not block the process User space decides which thread to run next Upcall notifies User space that thread is blocked

68 Pop-Up Threads Create a new thread on event
Short life span, only handles that event

69 Threads in Java JVM is really a specification, native (kernel threads) may not exist on the host OS. In that case JVM uses user-space threads (called Green Threads). Two ways to implement: Implement Thread sub-class Write class that implements Runnable interface

70 Thread Usage Rough outline of code for previous slide
(a) Dispatcher thread (b) Worker thread

71 Question How can you write a Java program to test if threads are natively supported in the OS?


Download ppt "Operating Systems Process Managment."

Similar presentations


Ads by Google