processes and threads
Download
Skip this Video
Download Presentation
Processes and Threads

Loading in 2 Seconds...

play fullscreen
1 / 113

Processes and Threads - PowerPoint PPT Presentation


  • 72 Views
  • Uploaded on

Processes and Threads. 2.1 Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling. Chapter 2. Processes The Process Model. In the process model, all runnable software is organized as a collection of processes. . Multiprogramming of four programs

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Processes and Threads' - kilenya


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
processes and threads

Processes and Threads

2.1 Processes

2.2 Threads

2.3 Interprocess communication

2.4 Classical IPC problems

2.5 Scheduling

Chapter 2

processes the process model
ProcessesThe Process Model
  • In the process model, all runnable software is organized as a collection of processes.
  • Multiprogramming of four programs
  • Conceptual model of 4 independent, sequential processes
  • Only one program active at any instant
process creation
Process Creation
  • Principal events that cause process creation
    • System initialization
    • Execution of a process creation system
    • User request to create a new process
    • Initiation of a batch job
  • Foreground processes are those that interact with users and perform work for them.
  • Background processes that handle some incoming request are called daemons.
process creation4
Process Creation
  • How to list the running processes?
    • In UNIX, use the ps command.
    • In Windows 95/98/Me, use Ctrl-Alt-Del.
    • In Windows NT/2000/XP, use the task manager.
  • In UNIX, a fork() system call is used to create a new process.
    • Initially, the parent and the child have the same memory image, the same environment strings, and the same open files.
    • The execve() system call can be used to load a new program.
    • But the parent and child have their own distinct address space.
  • In Windows, CreateProcess handles both creation and loading the correct program into the new process.
process termination
Process Termination
  • Conditions which terminate processes
    • Normal exit (voluntary)
      • Exit in UNIX and ExitProcess in Windows.
    • Error exit (voluntary)
      • Example: Compiling errors.
    • Fatal error (involuntary)
      • Example: Core dump
    • Killed by another process (involuntary)
      • Kill in UNIX and TerminateProcess in Windows.
process hierarchies
Process Hierarchies
  • Parent creates a child process, child processes can create its own process
  • Forms a hierarchy
    • UNIX calls this a "process group“
    • In UNIX, init  sshd  sh  ps
  • Windows has no concept of process hierarchy
    • all processes are created equal
process model example
Process Model - Example
  • In UNIX, for example, a fork() system call is used to create child processes in such a hierarchy. A good example is a shell.
  • Consider the menu-driven shell given in programs/c/Ex3.c.
process model example8
Process Model - Example
  • The algorithm (Ex3.c) is:
    • Display the menu and obtain the user's request (1=ls,2=ps,3=exit).
    • If the user wants to exit, then terminate the shell process.
    • Otherwise:
      • Fork off a child process.
      • The child process executes the option selected, while the parent waits for the child to complete.
      • The child exits.
      • The parent goes back to step 1.
process states 1
Process States (1)
  • Possible process states
    • Running - using the CPU.
    • Ready - runnable (in the ready queue).
    • Blocked - unable to run until an external event occurs; e.g., waiting for a key to be pressed.
  • Transitions between states shown
process states 2
Process States (2)
  • Lowest layer of process-structured OS (Scheduler)
    • handles interrupts, scheduling
  • Above that layer are sequential processes
    • User processes, disk processes, terminal processes
implementation of processes
Implementation of Processes
  • The operating system maintains a process table with one entry (called a process control block (PCB)) for each process.
  • When a context switch occurs between processes P1 and P2, the current state of the RUNNING process, say P1, is saved in the PCB for process P1 and the state of a READY process, say P2, is restored from the PCB for process P2 to the CPU registers, etc. Then, process P2 begins RUNNING.
  • Note: This rapid switching between processes gives the illusion of true parallelism and is called pseudo- parallelism.
implementation of processes 1
Implementation of Processes (1)

Fields of a process table entry

implementation of processes 2
Implementation of Processes (2)

Skeleton of what lowest level of OS does when an interrupt occurs

thread vs process
Thread vs. Process
  • A thread – lightweight process (LWP) is a basic unit of CPU utilization.
  • It comprises a thread ID, a program counter, a register set, and a stack.
  • A traditional (heavyweight) process has a single thread of control.
  • If the process has multiple threads of control, it can do more than one task at a time. This situation is called multithreading.
threads the thread model 1
ThreadsThe Thread Model (1)

(a) Three processes each with one thread

(b) One process with three threads

the thread model 2
The Thread Model (2)
  • Items shared by all threads in a process
  • Items private to each thread
the thread model 3
The Thread Model (3)

Each thread has its own stack

thread usage
Thread Usage
  • Why do you use threads?
    • Responsiveness: Multiple activities can be done at same time. They can speed up the application.
    • Resource Sharing: Threads share the memory and the resources of the process to which they belong.
    • Economy: They are easy to create and destroy.
    • Utilization of MP (multiprocessor) Architectures: They are useful on multiple CUP systems.
  • Example - Word Processor, Spreadsheet:
    • One thread interacts with the user.
    • One formats the document (spreadsheet).
    • One writes the file to disk periodically.
thread usage 1
Thread Usage (1)

A word processor with three threads

thread usage21
Thread Usage
  • Example – Web server:
    • One thread, the dispatcher, distributes the requests to a worker thread.
    • A worker thread handles the requests.
  • Example – data processing:
    • An input thread
    • A processing thread
    • An output thread
thread usage 2
Thread Usage (2)

A multithreaded Web server

thread usage 3
Thread Usage (3)
  • Rough outline of code for previous slide

(a) Dispatcher thread

(b) Worker thread

thread usage 4
Thread Usage (4)

Three ways to construct a server

user threads
User Threads
  • Thread management done by user-level threads library
  • User-level threads are fast to create and manage.
  • Problem: If the kernel is single-threaded, then any user-level thread performing a blocking system call will cause the entire process to block.
  • Examples

- POSIX Pthreads

- Mach C-threads

- Solaris UI-threads

implementing threads in user space
Implementing Threads in User Space

A user-level threads package

kernel threads
Kernel Threads
  • Supported by the Kernel: The kernel performs thread creation, scheduling, and management in kernel space.
  • Disadvantage: high cost
  • Examples

- Windows 95/98/NT/2000/XP

- Solaris

- Tru64 UNIX

- BeOS

- OpenBSD

- FreeBSD

- Linux

implementing threads in the kernel
Implementing Threads in the Kernel

A threads package managed by the kernel

hybrid implementations
Hybrid Implementations

Multiplexing user-level threads onto kernel- level threads

scheduler activations
Scheduler Activations
  • Goal – mimic functionality of kernel threads
    • gain performance of user space threads
  • Avoids unnecessary user/kernel transitions
  • Kernel assigns virtual processors to each process
    • lets runtime system allocate threads to processors
    • Makes an upcall to the run-time system to switch threads.
  • Problem: Fundamental reliance on kernel (lower layer)

calling procedures in user space (higher layer)

pop up threads
Pop-Up Threads
  • Creation of a new thread when message arrives

(a) before message arrives

(b) after message arrives

making single threaded code multithreaded
Making Single-Threaded Code Multithreaded

Conflicts between threads over the use of a global variable

making single threaded code multithreaded33
Making Single-Threaded Code Multithreaded

Threads can have private global variables

thread programming
Thread Programming
  • Pthread - a POSIX standard (IEEE 1003.1c) API for thread creation and synchronization. .
  • Solaris 2 is a version of UNIX with support for threads at the kernel and user levels, SMP, and real-time scheduling.
  • Solaris 2 implements the Pthread API and UI threads
thread programming35
Thread Programming
  • Pthread - a POSIX standard (IEEE 1003.1c) API for thread creation and synchronization.
    • Example: thread-sum.c, pthread-ex.c, helloworld.cc
  • Solaris 2 threads:
    • Solaris 2 is a version of UNIX with support for threads at the kernel and user levels, SMP, and real-time scheduling.
    • Solaris 2 implements the Pthread API and UI threads.
    • Example: thread-ex.c, lwp.c
thread programming36
Thread Programming
  • Java threads may be created by:
    • Extending Thread class
    • Implementing the Runnable interface
  • Calling the start method for the new object does two things:
    • It allocates memory and initializes a new thread in the JVM.
    • It calls the run method, making the thread eligible to be run by JVM.
  • Java threads are managed by the JVM.
  • Example: ThreadEx.java, ThreadSum.java
interprocess communication
Interprocess Communication
  • Three issues are involved in interprocess communication (IPC):
    • How one process can pass information to another.
    • How to make sure two or more processes do not get into each other’s way when engaging in critical activities.
    • Proper sequencing when dependencies are present.
  • Race conditions are situations in which several processes access shared data and the final result depends on the order of operations.
interprocess communication race conditions
Interprocess CommunicationRace Conditions

Two processes want to access shared memory at same time

race condition
Race Condition
  • Assume there are two variables , out, which points to the next file to be printed, and in, which points to the next free slot in the directory.
  • Assume in is currently 7. The following situation could happen:

Process A reads in and stores the value 7 in a local variable. A switch to process B happens.

Process B reads in, stores the file name in slot 7 and updates in to be an 8.

Process A stores the file name in slot 7 and updates in to be an 8.

  • The file name in slot 7 was determined by who finished last. A race condition occurs.
critical regions
Critical Regions
  • The key to avoid race conditions is to prohibit more than one process from reading and writing the shared data at the same time.
  • Four conditions to provide mutual exclusion
    • No two processes simultaneously in critical region (mutual exclusion)
    • No assumptions made about speeds or numbers of CPUs
    • No process running outside its critical region may block another process (progress)
    • No process must wait forever to enter its critical region
critical regions 2
Critical Regions (2)

Mutual exclusion using critical regions

mutual exclusion solution disabling interrupts
Mutual Exclusion Solution - Disabling Interrupts
  • By disabling all interrupts just after entering its critical region, no context switching can occur.
  • Thus, it is unwise to allow user processes to disable interrupts.
  • However, it is convenient (and even necessary) for the kernel to disable interrupts while a context switch is being performed.
mutual exclusion solution lock variable
Mutual Exclusion Solution - Lock Variable

shared int lock = 0;

/* entry_code: execute before entering critical section */

while (lock != 0) /* do nothing */ ;

lock = 1;

- critical section -

/* exit_code: execute after leaving critical section */

lock = 0;

  • This solution may violate property 1. If a context switch occurs after one process executes the while statement, but before setting lock = 1, then two (or more) processes may be able to enter their critical sections at the same time.
mutual exclusion solution
Mutual Exclusion Solution

Proposed solution to critical region problem

(a) Process 0. (b) Process 1.

mutual exclusion solution strict alternation
Mutual Exclusion Solution – Strict Alternation
  • This solution may violate progress requirement. Since the processes must strictly alternate entering their critical sections, a process wanting to enter its critical section twice in a row will be blocked until the other process decides to enter (and leave) its critical section as shown in the the table below.
  • The solution of strict alteration is shown in Ex5.c. Be sure to note the way shared memory is allocated using shmget and shmat.
mutual exclusion with busy waiting
Mutual Exclusion with Busy Waiting

Peterson's solution for achieving mutual exclusion

mutual exclusion solution peterson s
Mutual Exclusion Solution – Peterson’s
  • This solution satisfies all 4 properties of a good solution. Unfortunately, this solution involves busy waiting in the while loop. Busy waiting can lead to problems we will discuss below.
  • Challenge: Write the code for Peterson's solution using Ex5.c (the strict alteration code) as a starting point.
hardware solution test and set locks tsl
Hardware solution: Test-and-Set Locks (TSL)
  • The hardware must support a special instruction, tsl, which does 2 things in a single atomic action:

tsl register, flag:

(a) copy a value in memory (flag) to a CPU register and

(b) set flag to 1.

mutual exclusion with busy waiting49
Mutual Exclusion with Busy Waiting

Entering and leaving a critical region using the

TSL instruction

mutual exclusion with busy waiting50
Mutual Exclusion with Busy Waiting
  • The last two solutions, 4 and 5, require BUSY-WAITING; that is, a process executing the entry code will sit in a tight loop using up CPU cycles, testing some condition over and over, until it becomes true. For example, in 5, in the enter_region code, a process keeps checking over and over to see if the flag has been set to 0.
  • Busy-waiting may lead to the PRIORITY-INVERSION PROBLEM if simple priority scheduling is used to schedule the processes.
mutual exclusion with busy waiting51
Mutual Exclusion with Busy Waiting
  • Example: Test-and-set Locks:

P0 (low) - in cs -x

|

context

switch

|

P1 (high) -----tsl-cmp-jnz-tsl... x-tsl-cmp... x-... forever.

  • Note, since priority scheduling is used, P1 will keep getting scheduled and waste time doing busy-waiting. :-(
  • Thus, we have a situation in which a low-priority process is blocking a high-priority process, and this is called PRIORITY-INVERSION.
semaphores e w dijkstra 1965
Semaphores [E.W. Dijkstra, 1965].
  • A SEMAPHORE, S, is a structure consisting of two parts:

(a) an integer counter, COUNT

(b) a queue of pids of blocked processes, Q

  • That is,

struct sem_struct {

int count;

queue Q;

} semaphore;

semaphore S;

semaphores e w dijkstra 196553
Semaphores [E.W. Dijkstra, 1965].
  • There are 2 operations on semaphores, UP and DOWN. These operations must be executed atomically (that is in mutual exclusion). Suppose that P is the process making the system call. The operations are defined as follows:

DOWN(S):

if (S.count > 0)

S.count = S.count - 1;

else

block(P); that is,

(a) enqueue the pid of P in S.Q,

(b) block process P (remove the pid from

the ready queue), and

(c) pass control to the scheduler.

semaphores e w dijkstra 196554
Semaphores [E.W. Dijkstra, 1965].

UP(S):

if (S.Q is nonempty)

wakeup(P) for some process P in S.Q; that is,

(a) remove a pid from S.Q (the pid of P),

(b) put the pid in the ready queue, and

(c) pass control to the scheduler.

else

S.count = S.count + 1;

mutual exclusion problem
Mutual Exclusion Problem

semaphore mutex = 1; /* set mutex.count = 1 */

DOWN(mutex);

- critical section -

UP(mutex);

  • To see how semaphores are used to eliminate race conditions in Ex4.c, see Ex6.c and sem.h. The library sem.h contains a version of UP(semid) and DOWN(semid) that correspond with UP and DOWN given above.
  • Semaphores do not require busy-waiting, instead they involve BLOCKING.
producer consumer problem bounded buffer problem
Producer-Consumer Problem = Bounded Buffer Problem
  • Consider a circular buffer that can hold N items.
  • Producers add items to the buffer and Consumers remove items from the buffer.
  • The Producer-Consumer Problem is to restrict access to the buffer so correct executions result.
sleep and wakeup
Sleep and Wakeup

Producer-consumer problem with fatal race condition

semaphores
Semaphores

The producer-consumer problem using semaphores

mutexes
Mutexes
  • A mutex is a semaphore that can be in one of two states: unlocked or locked.

Implementation of mutex_lock and mutex_unlock

using semaphores
Using Semaphores
  • Process Synchronization: Order process execution:

Suppose we have 4 processes: A, B, C, and D. A must finish executing before B and C start. B and C must finish executing before D starts.

S1 S2

A ----> B ----> D

| ^

| S1 S3 |

+-----> C ------+

Then, the processes may be synchronized using semaphores:

semaphore S1, S2, S3 = 0,0,0;

using semaphores61
Using Semaphores
  • Process Synchronization: Order process execution:

Process A:

----------

- do work of A

UP(S1); /* Let B or C start */

UP(S1); /* Let B or C start */

Process B:

----------

DOWN(S1); /* Block until A is finished */

- do work of B

UP(S2);

Process C:

----------

DOWN(S1);

- do work of C

UP(S3);

using semaphores62
Using Semaphores

Process D:

----------

DOWN(S2);

DOWN(S3);

- do work of D

  • In conclusion, we use semaphores in two different ways: mutual exclusion (mutex) and process synchronization (full, empty).
  • Is it easy to use semaphores?
monitors
Monitors
  • A monitor is a collection of procedures, variables, and data structures that can only be accessed by one process at a time (for the purpose of mutual exclusion).
  • To allow a process to wait within the monitor, a conditionvariable must be declared, as condition x, y;
  • Condition variable can only be used with the operations wait and signal (for the purpose of synchronization).
    • The operation

x.wait();means that the process invoking this operation is suspended until another process invokes

x.signal();

    • The x.signal operation resumes exactly one suspended process. If no process is suspended, then the signal operation has no effect.
monitors64
Monitors

Example of a monitor

monitors65
Monitors
  • Outline of producer-consumer problem with monitors
    • only one monitor procedure active at one time
    • buffer has N slots
monitors66
Monitors
  • Monitors in Java
    • supports user-level threads and methods (procedures) to be grouped together into classes.
    • By adding the keyword synchronized to a method, Java guarantees that once any thread has started executing that method, no other thread can execute that method.
  • Advantages: Ease of programming. (?)
  • Disadvantages:
    • Monitors are a programming language concept, so they are difficult to add to an existing language; e.g., how can a compiler determine which procedures are inside a monitor if they can be nested?
    • Monitors are too expensive to implement and they are overly restrictive (shared memory is required).
monitors67
Monitors

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

monitors68
Monitors

static class our_monitor { //this is a monitor

private int buffer[] = new int[N];

private int count = 0, lo = 0, hi = 0; // counters and indices

public synchronized void insert(int val) {

if (count == N) go_to_sleep(); // if the buffer is full. go to sleep

buffer [hi] = val; // insert an item into the buffer

hi = (hi + 1) % N; // slot to place next Item in

count = count + 1; //one more item in the buffer now

if (count == 1) notify(); //if consumer was sleeping, wake it up

}

public synchronized int remove() {

int val;

if (count == 0) go_to_sleep(); // if the buffer is empty, go to sleep

val = buffer [lo]; // fetch an item from the buffer

lo = (lo + 1) % N; //slot to fetch next item from

count = count - 1; // one few items in the buffer

if (count == N -1) notify(); //if producer was sleeping, wake it up

return val;

}

private void go_to_sleep() {

try {wait();} catch(InterruptedException exc) {};

}

}

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

message passing
Message Passing
  • Possible Approaches:
    • Assign each process a unique address such as addr. Then, send messages directly to the process through that address: blocking receive.

send(addr, msg);

recv(addr, msg);

Example: signals in UNIX.

    • Use mailboxes: blocking receive.

send(mailbox, msg);

recv(mailbox, msg);

Example: pipes in UNIX.

    • Rendezvous: blocking send and receive.

Example: Ada tasks.

  • Message passing is commonly used in parallel programming systems. For example, MPI (Message-Passing Interface).
pipe implementation
Pipe Implementation
  • Pipe description:
    • pipe is a unidirectional data structure.
    • One end is for reading and one end is for writing.
    • Use pipe function to create a pipe.

int mbox[2];

pipe(mbox);

    • In our implementation, mbox[0] is for reading and mbox[1] is for writing.

First End Second End

mbox[0] <-oooooooooooooooooooo<- mbox[1]

Where o stands for token.

    • Each pipe is used like a semaphore.

If the initial value of a semaphore is 0, then no token is required to store in the pipe initially.

pipe implementation71
Pipe Implementation
  • Pipe description:
    • If the initial value of a semaphore is more than 0, for example, the initial value is 3, then it can be initialized in this way:

int msg = 0;

for (i = 1; i <= 3; i++)

write(mbox1[1],&msg,sizeof(msg));

    • DOWN(S) is equivalent to read(mbox[0],&msg,sizeof(msg));
    • UP(S) is equivalent to write(mbox[1],&msg,sizeof(msg));
message passing72
Message Passing

The producer-consumer problem with N messages

barriers
Barriers
  • Use of a barrier
    • processes approaching a barrier
    • all processes but one blocked at barrier
    • last process arrives, all are let through
  • Example: Parallel matrix multiplication
classical ipc problems
Classical IPC Problems
  • These problems are used for testing every newly proposed synchronization scheme:
    • Bounded-Buffer (Producer-Consumer) Problem
    • Dining-Philosophers Problem
    • Readers and Writers Problem
    • Sleeping Barber Problem
dining philosophers
Dining Philosophers
  • Dining Philosophers Problem [Dijkstra, 1965]:

Problem: Five philosophers are seated around a table. There is one fork between each pair of philosophers. Each philosopher needs to grab the two adjacent forks in order to eat. Philosophers alternate between eating and thinking. They only eat for finite periods of time.

dining philosophers76
Dining Philosophers
  • Philosophers eat/think
  • Eating needs 2 forks
  • Pick one fork at a time
  • How to prevent deadlock
dining philosophers77
Dining Philosophers

A nonsolution to the dining philosophers problem

dining philosophers78
Dining Philosophers
  • Problem: Suppose all philosophers take their left forks at the same time. None will be able to take their right forks; that is, they all grab one fork. There will be a deadlock and no philosophers will be able to eat. This is called a CIRCULAR WAIT.
  • Other Solutions:
    • Pick-up the forks only if both are available. See Fig. 2-33 (page 127). Note: this solution may lead to starvation.
    • Only allow up to four philosophers to try grabbing their forks.
    • Asymmetric solution: Odd numbered philosophers grab their left fork first, whereas even numbered philosophers grab their right fork first.
dining philosophers79
Dining Philosophers

Solution to dining philosophers problem (part 1)

dining philosophers80
Dining Philosophers

Solution to dining philosophers problem (part 2)

readers and writers problem
Readers and Writers Problem
  • The readers and writers problem is used to model access to a shared database. Only one writer may write at a time. Any number of readers may read at the same time, but no reader is allow to read when a writer is writing.
  • One solution is shown in Fig. 2-34. The writer might not be able to write if readers keep coming.
  • One variation of the problem, called weak readers reference, is to suspend the incoming readers as long as a writer is waiting.
the readers and writers problem
The Readers and Writers Problem

A solution to the readers and writers problem

the sleeping barber problem
The Sleeping Barber Problem
  • Problem: The barber shop has one barber, one barber chair, and n chairs for waiting customers.
    • If there are no customers present, the barber sits down in the barber chair and falls asleep.
    • When a customer arrives, he has to wake up the sleeping barber.
    • If additional customers arrive while the barber is cutting a customer’s hair, they either sit down or leave the shop.
  • Program the barber and the customers without getting into race conditions.
the sleeping barber problem85
The Sleeping Barber Problem

Solution to sleeping barber problem.

scheduling
Scheduling
  • The SCHEDULER is the part of the operating system that decides (among the runnable processes) which process is to be run next.
  • A SCHEDULING ALGORITHM is a policy used by the scheduler to make that decision.
  • To make sure that no process runs too long, a clock is used to cause a periodic interrupt (usually around 50-60 Hz (times/second)); that is, about every 20 msec. PREEMPTIVE SCHEDULING allows processes that are runnable to be temporarily suspended so that other processes can have a chance to use the CPU.
properties of a good scheduling algorithm
Properties of a GOOD Scheduling Algorithm:
  • Fairness - each process gets its fair share of time with the CPU.
  • Efficiency - keep the CPU busy doing productive work.
  • Response Time - minimize the response time for interactive users.
  • Turnaround Time - minimize the turnaround time on batch jobs.
  • Throughput - maximize the number of jobs processed per hour.
scheduling process behavior
Scheduling(Process Behavior)
  • Bursts of CPU usage alternate with periods of I/O wait
    • a CPU-bound process
    • an I/O bound process
introduction to scheduling
Introduction to Scheduling

Scheduling Algorithm Goals

first come first served fcfs scheduling
First-Come, First-Served (FCFS) Scheduling

P1

P2

P3

0

24

27

30

ProcessBurst Time

P1 24

P2 3

P3 3

  • Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is:
  • Waiting time for P1 = 0; P2 = 24; P3 = 27
  • Average waiting time: (0 + 24 + 27)/3 = 17
fcfs scheduling cont
FCFS Scheduling (Cont.)

P2

P3

P1

0

3

6

30

Suppose that the processes arrive in the order

P2 , P3 , P1 .

  • The Gantt chart for the schedule is:
  • Waiting time for P1 = 6;P2 = 0; P3 = 3
  • Average waiting time: (6 + 0 + 3)/3 = 3
  • Much better than previous case.
  • Convoy effect: short process wait behind long process
shortest job first sjr scheduling
Shortest-Job-First (SJR) Scheduling
  • Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time.
  • The real difficulty with the SJF algorithm is knowing the length of the next CPU request.
  • SJF scheduling is used frequently in long-term scheduling.
  • The next CPU burst is generally predicated as an exponential average of the measured lengths of previous CPU bursts.
scheduling in batch systems
Scheduling in Batch Systems

An example of shortest job first scheduling

shortest job first sjr scheduling94
Shortest-Job-First (SJR) Scheduling
  • Two schemes:
    • nonpreemptive – once CPU given to the process it cannot be preempted until it completes its CPU burst.
    • preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF).
  • SJF is optimal – gives minimum average waiting time for a given set of processes.
example of non preemptive sjf
Example of Non-Preemptive SJF

P1

P3

P2

P4

0

3

7

8

12

16

Process Arrival TimeBurst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

  • SJF (non-preemptive)
  • Average waiting time = (0 + 6 + 3 + 7)/4 = 4
example of preemptive sjf
Example of Preemptive SJF

P1

P2

P3

P2

P4

P1

11

16

0

2

4

5

7

Process Arrival TimeBurst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

  • SJF (preemptive)
  • Average waiting time = (9 + 1 + 0 +2)/4 = 3
three level scheduling
Three-Level Scheduling
  • The admission scheduler decides which jobs to admit to the system.
  • The memory scheduler decides which processes should be kept in memory and which one kept on disk.
    • It can also decide how many processes it wants in memory, called the degree of multiprogramming.
  • The CPU scheduler is actually picking one of the ready processes in main memory to run next.
scheduling in batch systems98
Scheduling in Batch Systems

Three level scheduling

scheduling in interactive systems
Scheduling in Interactive Systems
  • Round Robin Scheduling
    • list of runnable processes
    • list of runnable processes after B uses up its quantum
round robin rr
Round Robin (RR)
  • Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue.
  • If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units.
  • Performance
    • q large  FIFO
    • q small  q must be large with respect to context switch, otherwise overhead is too high.
example of rr with time quantum 20
Example of RR with Time Quantum = 20

P1

P2

P3

P4

P1

P3

P4

P1

P3

P3

0

20

37

57

77

97

117

121

134

154

162

ProcessBurst Time

P1 53

P2 17

P3 68

P4 24

  • The Gantt chart is:
  • Typically, higher average turnaround than SJF, but better response.
priority scheduling
Priority Scheduling
  • A priority number (integer) is associated with each process
  • The CPU is allocated to the process with the highest priority (smallest integer  highest priority in UNIX).
    • Preemptive
    • nonpreemptive
  • SJF is a priority scheduling where priority is the predicted next CPU burst time.
  • Problem  Starvation – low priority processes may never execute.
  • Solution  Aging – as time progresses increase the priority of the process.
example of priority scheduling
Example of Priority Scheduling

P2

P5

P3

P6

P4

P1

11

16

0

2

4

5

7

Process Burst TimePriority

P1 5.0 6

P2 2.0 1

P3 1.0 3

P4 4.0 5

P5 2.0 2

P6 2.0 4

  • Priority
  • Average waiting time = (2 + 4 + 5 + 7 + 11 +2) / 6 = 4.83
multilevel queue scheduling
Multilevel Queue Scheduling
  • Each ready queue is assigned a different priority class [CTSS - Corbato, 1962].
  • Ready queue is partitioned into separate queues:foreground (interactive)background (batch)
  • Each queue has its own scheduling algorithm, foreground – RRbackground – FCFS
  • Scheduling must be done between the queues.
    • Fixed priority scheduling; (i.e., serve all from foreground then from background). Possibility of starvation.
    • Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its processes; i.e., 80% to foreground in RR and 20% to background in FCFS
scheduling in interactive systems106
Scheduling in Interactive Systems

A scheduling algorithm with four priority classes

more scheduling
More Scheduling
  • Shortest Process Next
    • SJF can be used in an interactive environment by estimating the runtime based on past behavior. Aging is a method used to estimate runtime by taking a weighted average of the current runtime and the previous estimate.
    • Example: Let a = estimate weight, then the current estimate is: a x T0 + (1-a) x T1

where T0 is the previous estimate and T1 is the current runtime.

  • Guaranteed Scheduling
    • Suppose there are n processes, each process should get 1/n of the CPU cycles.
    • Compute ratio = actual CPU time consumed / CPU time entitled
    • Run the process with the lowest ratio
more scheduling108
More Scheduling
  • Lottery Scheduling
    • Give processes lottery tickets for various system resources
    • When a scheduling decision is made, a lottery ticket is chosen, and the process holding that ticket gets the resource.
  • Fair-Share Scheduling
    • Take into account how many processes a user owns.
    • Example: User 1 – A, B, C, D and Use 2 – E
    • Round-robin: AEBECEDE...
    • Fair-Share: if user 1 is entitled to twice as much CPU time as user 2, then a possible scheduling would be:

ABECDEABECDE….

scheduling in real time systems
Scheduling in Real-Time Systems
  • The scheduler makes real promises to the user in terms of deadlines or CPU utilization.
  • Schedulable real-time system
    • Given
      • m periodic events
      • event i occurs within period Pi and requires Ci seconds
    • Then the load can only be handled if
policy versus mechanism
Policy versus Mechanism
  • To allow a process to schedule its child processes, we cam separate what is allowed to be done (policy) with how it is done (mechanism).
    • a process knows which of its children threads are important and set priority and pass the priority policy to the kernel scheduling mechanism
  • Scheduling algorithm parameterized
    • mechanism in the kernel
  • Parameters filled in by user processes
    • policy set by user process
thread scheduling
Thread Scheduling
  • The process scheduling algorithm can be used in thread scheduling. In practice, round-robin and priority scheduling are used.
  • The only constraint is the absence of a clock to interrupt a user-level thread.
  • User-level and kernel-level threads
    • A major difference between user-level threads and kernel-level threads is the performance.
    • User-level threads can employ an application-specific thread scheduler.
thread scheduling112
Thread Scheduling

Possible scheduling of user-level threads

  • 50-msec process quantum
  • threads run 5 msec/CPU burst
thread scheduling113
Thread Scheduling

Possible scheduling of kernel-level threads

  • 50-msec process quantum
  • threads run 5 msec/CPU burst
ad