158
03/25/22 CST 352 - Operating Systems 1 Operating Systems CST 352 Processes and Threads

9/17/2015CST 352 - Operating Systems1 Operating Systems CST 352 Processes and Threads

Embed Size (px)

Citation preview

04/19/23 CST 352 - Operating Systems 1

Operating Systems

CST 352

Processes and Threads

04/19/23 CST 352 - Operating Systems 2

Topics

• Definitions• Communications

–Process to Process–Process to Thread–Thread to Thread

• Scheduling

04/19/23 CST 352 - Operating Systems 3

Definitions - PrelimsConcurrency – The appearance

that threads are running simultaneously even though there is a single CPU.

04/19/23 CST 352 - Operating Systems 4

Definitions - PrelimsContext – The “processor” state

of a block of executing code. This includes all registers required to uniquely identify this chain of execution.

04/19/23 CST 352 - Operating Systems 5

Definitions - ProcessProcess – A group of instructions

along with the context defining the execution “state (s)” of those instructions.

Q: How does the concept of a “process” and the code that describes the process parallel the concept of a “class” and an “object”?

04/19/23 CST 352 - Operating Systems 6

Definitions - ProcessA process is an abstraction defining

processor execution resource grouping.

In the days of “batch” processing, processes executed to completion before another process was loaded, then started.

04/19/23 CST 352 - Operating Systems 7

Definitions - ProcessBatch Processing – What had to happen?1. Operator is waiting for input (reading newspaper)2. User writes a program on punch cards.3. Operator takes a tray of punch cards from a stack of jobs.4. Operator loads the cards into the system tray – computer

reads each card into memory.5. Operator starts the job.6. System compiles the code in the job.7. System jumps to the first machine instruction of

compiled code.8. Job runs to completion.9. Operator places output in out box. – Go to 1

04/19/23 CST 352 - Operating Systems 8

Definitions - ProcessBatch Processing – What had to happen?

The computer is only involved in steps 4, 6,7 , and 8.

Where did the OS reside?

04/19/23 CST 352 - Operating Systems 9

Definitions - ProcessSynchronous Processing – What had to

happen?1. System is waiting for input.

2. User writes program and compiles it on the computer.

3. User starts execution of the written program.

4. System loads program into memory for execution.

5. System jumps to the first instruction of program.

6. Program runs to completion.

7. System jumps back to state of waiting for input.

04/19/23 CST 352 - Operating Systems 10

Definitions - ProcessPreemptive Multitasking – What has to

happen?1. System is waiting for input (running SETI program).

2. User writes program and compiles it on the computer.

3. User starts execution of the written program.A. System loads next program into memory for execution.

B. System jumps to the first instruction of program.

C. Program runs to till time slice time-out or program done.

D. System jumps back to “A”.

04/19/23 CST 352 - Operating Systems 11

Definitions - ThreadIn an older OS, each process had:• Address space

– stack

– heap

• Single “thread” of control. – Serial execution of instructions in the

executing program.

04/19/23 CST 352 - Operating Systems 12

Definitions - ThreadA “thread” breaks the grouping

provided by a process of “resources and execution”.

A process main thread can “spawn” off threads. Each thread the process spawns will share the resources of the process.

04/19/23 CST 352 - Operating Systems 13

Definitions - ThreadQ:

1.Define Process2.Define Thread3.Define Context

04/19/23 CST 352 - Operating Systems 14

Processes and ThreadsProcesses:

Create Thread

Create primary thread1. Set up run-time stack.2. Set up memory segment.3. Create context.4. Set ready for execute.

Run-TimeStack

ProcessMemory

Primary Thread

04/19/23 CST 352 - Operating Systems 15

Processes and ThreadsProcesses:• A process has a memory segment assigned to it.• A process has a primary run-time stack assigned

to it (used by the primary thread).• Processes must communicate with special

mechanism.– A process has protected code segment.– A process has a protected memory segment.– The OS must provide a special address space for

interprocess communication.

04/19/23 CST 352 - Operating Systems 16

Processes and ThreadsProcesses and Threads:

Create Thread

Create Thread1. Set up run-time stack.2. Create context.3. Set ready for execute.

• Execution is controlled by parent process.

• Parent process must retain a leash on child threads.

• Threads may communicate through the process memory segment.

Run-TimeStack

ProcessMemory

Primary Thread

Run-TimeStack

Thread 2

04/19/23 CST 352 - Operating Systems 17

Processes and ThreadsThreads:• Threads share the memory segment of a process

(data and code).• A Thread is assigned it’s own run-time stack by

the Operating System.• Threads can communicate with other threads

contained in the same process using data structures in the process memory segment.

• The data structures must be “thread-safe”.• The process and thread code must be written

“thread-safe”.

04/19/23 CST 352 - Operating Systems 18

Processes and ThreadsA preemptive multitasking OS is

made up of several processes:1. Foreground processes – those that

require user interaction (shell, GUI, etc.).

2. Background processes – those that run in the background, performing their jobs without user intervention (mailers, network monitors, printing monitors, etc).

04/19/23 CST 352 - Operating Systems 19

Processes and ThreadsBackground processes that help the

OS with some task are called daemons (not demons).

Daemon - an attentive benevolent entity. An intermediary between gods and men.

04/19/23 CST 352 - Operating Systems 20

Processes and ThreadsProcess/Thread States:• Suspend – Processes that are waiting for some

event to occur (i.e. I/O, Time of Day, etc.)• Active State – Process is ready to run. Waiting

for CPU.• Execute State – Switched into the CPU and has

control of the execution unit.• Blocked State – Process that are waiting for

access to a dynamic resource.

04/19/23 CST 352 - Operating Systems 21

Processes and ThreadsProcess/Thread/Fiber States:

Keep in mind the processing hierarchy:Processes contain threads

Threads contain fibers

– Process state change implies thread state change

– Thread state change implies fiber state change

The inverse is not true.

Processes and ThreadsProcess Creation:

Scheduler creates the process control block (TCB) and places it in the suspend list.

– Create process data segment.

– Create process code segment.

– Load op codes from disk into memory

– Build run-time stack.

04/19/23 CST 352 - Operating Systems 22

Suspend

Active

System R

eq. Complete

-or-

Logical A

ctiva

te

Execute

Dis

patc

h Preem

pt

Blocked

Logical S

uspend

ProcessEntry

Sys

tem

Cal

l or L

ogic

al S

uspe

nd Resource R

equest

Logical Block

Resource Available

Suspend Cleared - Process Still BlockedProcess Exit

Process State Transition Analysis:

04/19/23 CST 352 - Operating Systems 23

Processes and ThreadsProcess Suspend -

Activation:

Process System Call is done and process is ready for execute.

Scheduler removes the TCB from the suspend list and places it on the active list.

Suspend

Active

Syste

m Req. C

omplete

-or-

Logical Activ

ate

Execute

Dis

patc

h Preem

pt

Blocked

Logical S

uspend

Process Entry

Sys

tem

Cal

l or L

ogic

al S

uspe

nd Resource R

equest

Logical Block

Resource Available

Suspend Cleared - Process Still BlockedProcess Exit

04/19/23 CST 352 - Operating Systems 24

Processes and ThreadsProcess Dispatch:

TCB is switched into the CPU based on the scheduling algorithm.

– Round Robin

– Priority Based

– Etc.

Suspend

Active

System R

eq. Complete

-or-

Logical A

ctiva

te

Execute

Dis

pat

ch

Preem

pt

Blocked

Logical S

uspend

Process Entry

Sys

tem

Cal

l or L

ogic

al S

uspe

nd Resource R

equest

Logical Block

Resource Available

Suspend Cleared - Process Still BlockedProcess Exit

04/19/23 CST 352 - Operating Systems 25

Processes and ThreadsProcess Preempt:

TCB is switched out of the CPU. Scheduler uses the scheduling algorithm to determine next TCB to be switched in.

Suspend

Active

System R

eq. Complete

-or-

Logical A

ctiva

te

Execute

Dis

patc

h Preem

pt

Blocked

Logical S

uspend

Process Entry

Sys

tem

Cal

l or L

ogic

al S

uspe

nd Resource R

equest

Logical Block

Resource Available

Suspend Cleared - Process Still BlockedProcess Exit

04/19/23 CST 352 - Operating Systems 26

Processes and ThreadsProcess Execute -

Block:

Process requests some unavailable resource.

TCB is switched out of the CPU and placed in the “blocked” list.

Suspend

Active

System R

eq. Complete

-or-

Logical A

ctiva

te

Execute

Dis

patc

h Preem

pt

Blocked

Logical S

uspend

Process Entry

Sys

tem

Cal

l or L

ogic

al S

uspe

nd Resource R

equest

Logical Block

Resource Available

Suspend Cleared - Process Still BlockedProcess Exit

04/19/23 CST 352 - Operating Systems 27

Processes and ThreadsProcess Execute -

Suspend:

Process makes a system call that requires a lengthy operation.

TCB is switched out of the CPU and placed in the “Suspend” list.

Suspend

Active

System R

eq. Complete

-or-

Logical A

ctiva

te

Execute

Dis

patc

h Preem

pt

Blocked

Logical S

uspend

Process Entry

Sys

tem

Cal

l or

Logi

cal

Sus

pend

Resource R

equest

Logical Block

Resource Available

Suspend Cleared - Process Still BlockedProcess Exit

04/19/23 CST 352 - Operating Systems 28

Processes and ThreadsProcess Suspend -

Blocked:

Process system call is done but the resource required to complete the call is unavailable.

TCB is moved from the “Suspend” list to the “Blocked” list.

Suspend

Active

System R

eq. Complete

-or-

Logical A

ctiva

te

Execute

Dis

patc

h Preem

pt

Blocked

Logical S

uspend

Process Entry

Sys

tem

Cal

l or L

ogic

al S

uspe

nd Resource R

equest

Logical Block

Resource Available

Suspend Cleared - Process StillBlockedProcess Exit

04/19/23 CST 352 - Operating Systems 29

Processes and ThreadsProcess Block-

Activated:

Requested resource becomes available.

TCB is moved from the Blocked list back to the Active List.

Suspend

Active

System R

eq. Complete

-or-

Logical A

ctiva

te

Execute

Dis

patc

h Preem

pt

Blocked

Logical S

uspend

Process Entry

Sys

tem

Cal

l or L

ogic

al S

uspe

nd Resource R

equest

Logical Block

Resource Available

Suspend Cleared - Process Still BlockedProcess Exit

04/19/23 CST 352 - Operating Systems 30

Processes and ThreadsProcess Active -

Block:

Currently dispatched process “Blocks” an active process.

TCB is moved from the Active list to the Blocked List.

Suspend

Active

System R

eq. Complete

-or-

Logical A

ctiva

te

Execute

Dis

patc

h Preem

pt

Blocked

Logical S

uspend

Process Entry

Sys

tem

Cal

l or L

ogic

al S

uspe

nd Resource R

equest

Logical Block

Resource Available

Suspend Cleared - Process Still BlockedProcess Exit

04/19/23 CST 352 - Operating Systems 31

Processes and ThreadsProcess Active -

Suspend:

Currently dispatched process “Suspends” and active process.

TCB is moved from the Active list to the Suspend List.

Suspend

Active

System R

eq. Complete

-or-

Logical A

ctiva

te

Execute

Dis

patc

h Preem

pt

Blocked

Logical Suspen

d

Process Entry

Sys

tem

Cal

l or L

ogic

al S

uspe

nd Resource R

equest

Logical Block

Resource Available

Suspend Cleared - Process Still BlockedProcess Exit

04/19/23 CST 352 - Operating Systems 32

Processes and ThreadsProcess Terminate:

Process is terminated by another process. Process exits.

• Process resources are returned to the OS.

• Process memory segment is cleaned up.

• Process run-time stack is cleaned up.

Suspend

Active

System R

eq. Complete

-or-

Logical A

ctiva

te

Execute

Dis

patc

h Preem

pt

Blocked

Logical S

uspend

Process Entry

Sys

tem

Cal

l or L

ogic

al S

uspe

nd Resource R

equest

Logical Block

Resource Available

Suspend Cleared - Process Still BlockedProcess Exit

04/19/23 CST 352 - Operating Systems 33

Processes and ThreadsQ:

1. What states are required for implementation of a multitasking kernel?

04/19/23 CST 352 - Operating Systems 34

Processes and Threads• Processes must be managed in

kernel space. Why is this the case?• It is possible to manage threads in

user space. Why is this the case?• What are advantages of managing

threads in user space vs. kernel space?

04/19/23 CST 352 - Operating Systems 35

OS Requirements for Process ImplementationTo exist, a process needs:• Process Control Block – Contains the

context of the process main thread.• Process Code Segment – Contains the code

relocated to a physical address space.• Process Data Segment – Contains the

memory “heap” assigned to the process.• Process Run-time Stack – Contains the call

stack assigned to the process main thread.

04/19/23 CST 352 - Operating Systems 36

Processes and ThreadsWhen a process gets suspended it can be

moved out of physical memory.

This activity is called “swapping”.

Don’t confuse swapping with virtual memory.

04/19/23 CST 352 - Operating Systems 37

Processes and ThreadsSwapping of a process involves taking

all the process components (TCBs, Code Segment, Data Segment, Heap, and run-time stack(s)) and moving them from memory to disk.

04/19/23 CST 352 - Operating Systems 38

Processes and ThreadsPaging of a process involves taking a

piece of a process (e.g. a page of memory) and moving it from memory to disk.

04/19/23 CST 352 - Operating Systems 39

Processes and ThreadsThe disk space where the process

components get written to disk is called the swap file (page file virtual memory systems).

• Windows 7 uses virtual memory. The page file is normally on the “C:” drive.

• UNIX sets aside a partition on the hard drive.

04/19/23 CST 352 - Operating Systems 40

Processes and ThreadsAn OS that has a poorly tuned process

management scheme will do what is known as “thrashing”.

Thrashing – The OS spends more time moving process elements from disk to memory and back again than it does running processes.

04/19/23 CST 352 - Operating Systems 41

Processes and ThreadsQ:1. Define Swapping2. Define Paging3. Does swapping require virtual

memory? Why or why not?

04/19/23 CST 352 - Operating Systems 42

OS Requirements for Multitasking Implementation• Data Structures of TCBs for process

control:– Suspend List (a linked list)– Active Queue (priority queues, circular

linked list)– Blocked List (a table of queues keyed on

a resource ID)

04/19/23 CST 352 - Operating Systems 43

OS Requirements for Multitasking Implementation

• Interrupt Service routine to handle:

– I/O devices

– CPU interrupts» Interrupt asserted for task switching

» This ISR calls the dispatcher

04/19/23 CST 352 - Operating Systems 44

OS Requirements for Thread ImplementationKernel Space Threads: • Process needs to maintain some

reference to threads it owns in a thread table associated with the TCB.

• The Kernel schedules threads based on run-time activity of controlling processes.

04/19/23 CST 352 - Operating Systems 45

OS Requirements for Thread Implementation

User Space Threads: • Process needs to maintain some reference to threads it owns

in a thread table.

• User space system calls are made to perform thread management. These calls cause the process to change “thread” context.

• The kernel only handles the scheduling of processes.

• User space thread management is analogous to “Cooperative Multitasking”. If a thread in a process “runs amok”, the process threads will starve.

04/19/23 CST 352 - Operating Systems 46

OS Requirements for Thread Implementation

Hybrid Threads (one-scheme): • Processes live in kernel space.

• Threads are managed in kernel space.

• User space threads are provided, known as “light-weight” threads (fibers).

04/19/23 CST 352 - Operating Systems 47

OS Requirements for Thread ImplementationHybrid Threads (another scheme): • Processes live in kernel space.• Threads live in user space.• When a thread blocks, it gives the kernel

what is basically a pointer to a call-back routine. The kernel then calls the “call-back” when the thread resource become available.

04/19/23 CST 352 - Operating Systems 48

Process SynchronizationRace Conditions:

Two or more threads are reading or writing to a shared resource. The final result depends on who writes at what precise time.

04/19/23 CST 352 - Operating Systems 49

Process SynchronizationRace Conditions: - Example

1. Process Thread A writes to a memory area then gets switched out.

2. Process Thread B writes to the same memory area then gets switched out.

3. Process Thread A gets switched back in and uses the memory area to make some calculation.

4. Process Thread A has no knowledge that Process Thread B has changed the value used for the calculation.

04/19/23 CST 352 - Operating Systems 50

Process SynchronizationRace Conditions: - Example

Consider the function:

void swap( int& intOne, int& intTwo )

{static int temp;

temp = intTwo;

intTwo = intOne;

intOne = temp;

}

04/19/23 CST 352 - Operating Systems 51

Process SynchronizationRace Conditions: - Example

1. Initial Conditions:Thread T1 – varOne = 5, varTwo = 8

Thread T2 – varOne = 15, varTwo = 25

2. Thread T1 calls swap(varOne, varTwo);

3. 8 gets put in temp in the swap procedure.

4. Thread T1 gets switched out of the CPU.

5. Thread T2 gets switched in and calls swap(varOne, varTwo);

6. 25 gets put into temp. Thread T2 finishes swap.

7. Thread T1 gets switched back in. swap finishes.

8. In Thread T1, varOne now contains 25 and varTwo contains 5. T1 has no idea there was a problem.

04/19/23 CST 352 - Operating Systems 52

Process SynchronizationRace Conditions:

Race conditions are a real problem to track because the behavior changes based on CPU Load, current active processes, amount of memory, etc.

04/19/23 CST 352 - Operating Systems 53

Process SynchronizationRace Conditions:

Necessary conditions for avoidance:1. No two threads may be simultaneously accessing a

shared resource.2. No assumptions should be made about the CPU as a

resource.3. Any threads busy accessing a shared resource cannot

be blocked by another thread (with the scheduler as an exception).

4. No thread should have to wait indefinitely for a shared resource.

04/19/23 CST 352 - Operating Systems 54

Process SynchronizationCritical Regions:

Create an area in a program where a thread cannot be interrupted while it is executing.

This way, a thread will be guaranteed that it completes it’s task before any other thread has the chance to mess it up.

04/19/23 CST 352 - Operating Systems 55

Process SynchronizationCritical Regions:

A critical region can be implemented by writing a function call such that:»first thread can enter the code

»subsequent threads will be blocked until the first thread is done

04/19/23 CST 352 - Operating Systems 56

Process SynchronizationCritical Regions:

Consider the scenario:

1. Thread 1 enters the critical region.

2. Thread 2 will not get CPU cycles.

3. Thread 1 GPFs inside the critcal region.

4. What now happens to Thread 2?

04/19/23 CST 352 - Operating Systems 57

Process SynchronizationMutual Exclusion – Busy Waiting1. Disabling Interrupts

Dangerous because of the disabling thread dies, our system needs to be reset.

2. Using a locking variable.Does not work properly since there is a possibility that between checking the lock and setting it, the thread gets switched out.

04/19/23 CST 352 - Operating Systems 58

Process SynchronizationMutual Exclusion – Busy Waiting3. Strict Alteration

Threads grant other threads by setting a “turn” variable in a cooperative manner. “turn” is a shared variable.Called “Strict Alteration” because thread code needs to be specifically altered for exclusion.

While (TRUE)

{

while (turn != 0);

criticalRegion( );

turn = 1;

nonCriticalRegion( );

};

While (TRUE)

{

while (turn != 1);

criticalRegion( );

turn = 0 ;

nonCriticalRegion( );

};

Thread 0: Thread 1:

04/19/23 CST 352 - Operating Systems 59

Process SynchronizationMutual Exclusion – Busy Waiting

3. Strict AlterationWhile Thread 1 is waiting for Thread 0, it is “polling” the turn variable.

This uses up processor CPU cycles.

04/19/23 CST 352 - Operating Systems 60

Process SynchronizationMutual Exclusion – Busy Waiting4. Petersons Algorithm.

Similar to Strict Alteration.

Keep a list of PIDs for processes that want a turn for use of a shared resource.

04/19/23 CST 352 - Operating Systems 61

Process SynchronizationMutual Exclusion – Busy Waiting4. Petersons Algorithm - Globals

#define MAX_PROC 500 // Maximum number of processes

struct petersonVars{

int turn;int interested[MAX_PROC];

};

04/19/23 CST 352 - Operating Systems 62

Process SynchronizationMutual Exclusion – Busy Waiting4. Petersons Algorithm.

void enterRegion(int PID, petersonVars* peteVar){

int other = peteVar->turn; // Capture who is currently // using the resourcepeteVar->interested[PID] = TRUE; // Set PID interest to truepeteVar->turn = PID; // Set turn to our PID

//// Loop until it is our turn and "we" are the interested process.//while( peteVar->turn == PID && peteVar->interested[other] == TRUE ) // spin-lock{

if (peteVar->turn != PID) // Somebody else got our turn{

other = peteVar->turn;// Get the current userpeteVar->turn = PID; // Set the turn to us

}}

}

04/19/23 CST 352 - Operating Systems 63

Process SynchronizationMutual Exclusion – Busy Waiting4. Petersons Algorithm (cont’d) - Use

void thread( void *dummy ){

thrdArgs* threadArgs = (thrdArgs*)dummy;

petersonVars* pvars = threadArgs->pVars;

int PID = threadArgs->pid;

while( 1 ){

enterRegion(PID, pvars);

shared_var = PID;

cout << "Child thread " << PID << endl;

if ( shared_var != PID ){

cout << "OOPS!!!! ****** Child thread " << PID << endl;}

leaveRegion(PID, pvars);}

}

04/19/23 CST 352 - Operating Systems 64

Process SynchronizationMutual Exclusion – Busy Waiting4. Petersons Algorithm (cont’d).

void leaveRegion(int pid, petersonVars* peteVar){

peteVar->interested[pid] = FALSE; // We are done. Set our interest to FALSE;};

04/19/23 CST 352 - Operating Systems 65

Process SynchronizationQ:

1 – What is the difference between Strict Alteration and Peterson’s Algorithm?

04/19/23 CST 352 - Operating Systems 66

Process SynchronizationMutual Exclusion – Busy Waiting4. TSL instruction.

This is a special instruction that sets a memory area to 1 and returns the previous value in one execution cycle.

Test and Set

Check the value

If the value was 1, loop until it test and set returns a previous value of 0.

04/19/23 CST 352 - Operating Systems 67

Process SynchronizationMutual Exclusion is a valid method for resource sharing.The major drawback with mutual exclusion is the requirement of a busy wait.Low priority processes (or threads) may never gain access to a resource since they will never be the process that gets the resource access flag.The inverse problem is once a low priority process (or thread) gains access to a resource, all higher priority processes must wait for the slow one to finish.

04/19/23 CST 352 - Operating Systems 68

Process SynchronizationProducer – Consumer

Two processes (or threads) share a fixed size buffer.One process puts information into the buffer (producer).The other process takes information out of the buffer (consumer).The producer will block when the buffer is full.The consumer will block when the buffer is empty.Either will block if the other has access of the buffer.

04/19/23 CST 352 - Operating Systems 69

Process SynchronizationProducer – Consumer – Code sample

// Shared resource – Bounded Buffer

int buffer[MAX_ITEMS];

int nextInsert = 0; // Next index to produce to

int nextRemove = 0; // Next index to consume from

int count = 0; // Number of items in the buffer

04/19/23 CST 352 - Operating Systems 70

Process SynchronizationProducer – Consumer – Code sample

// Producer

…while (1){

bb.addItem(rand());}

// Bounded Buffer

…void addItem (int itemToAdd){

if ( count = = MAX_ITEMS ) sleep( ); // Suspendbuffer[nextInsert] = itemToAdd; // Produce++nextInsert %= MAX_ITEMS; // Increment the insert pointcount++; // Increment the countif (count > 0 ) wakeup(consumer); // Activate the consumer

}

04/19/23 CST 352 - Operating Systems 71

Process SynchronizationProducer – Consumer – Code sample

// Consumer…int consumerInt;while (1){

consumerInt = removeItem();}

// Bounded Buffer…int removeItem ( ){

int retVal;if ( count = = 0 ) sleep( ); // SuspendretVal = buffer[nextRemove]; // Consume++nextRemove %= MAX_ITEMS; // Increment the remove pointcount--; // Decrement the countif (count < MAX_ITEMS )

wakeup(producer); // Activate the producerreturn retVal;

}

04/19/23 CST 352 - Operating Systems 72

Process SynchronizationProducer – ConsumerWe can get into trouble here because of the unconstrained

access to the shared resource variables.If the count is set to 0, the consumer sleeps, but the sleep state is not fully entered since the scheduler switches out the Consumer.Now the Producer puts a value in the buffer and increments the count. Count == 1, so the producer signals the consumer to wakeup.Since the consumer is not yet asleep, the signal is ignored. The consumer is then switched back in and finishes going to sleep, never to wake again.

04/19/23 CST 352 - Operating Systems 73

Process SynchronizationProducer – ConsumerThe problem exist because the consumer lost the

wakeup signal since it was still in the process of going to sleep.

To avoid this problem, the “wakeup” signals must be saved until processes are in a state that will allow the signal to be correctly applied by the OS.

04/19/23 CST 352 - Operating Systems 74

Process SynchronizationCounting Semaphores

A semaphore is a construct that allows the saving of “wakeup” signals.

04/19/23 CST 352 - Operating Systems 75

Process SynchronizationCounting Semaphores

Two operations:1. dec( ) – same as “down” in the

book.» If semaphore count == 0

Put “this” thread on “this” semaphore block list.

Remove “this” thread from the active thread list.

» Decrement count and continue on.

04/19/23 CST 352 - Operating Systems 76

Process SynchronizationCounting Semaphores

Two operations:

2. inc( ) – same as “up” in the book.» Increment semaphore count.

» If a thread is suspended waiting for “this” semaphore Choose a waiting thread and move it to

the active list.

04/19/23 CST 352 - Operating Systems 77

Process SynchronizationCounting Semaphores

For the semaphore to work properly, the checking of the count, incrementing it and performing some “suspend” or “activate” operation, must be done without interrupt.

To do this, the OS must disable interrupts before the operation starts, then enable them when it is done.

04/19/23 CST 352 - Operating Systems 78

Process SynchronizationQ:

1. Write the pseudo code for a counting semaphore.

04/19/23 CST 352 - Operating Systems 79

Process SynchronizationCounting SemaphoresThe Producer/Consumer problem can be solved

using semaphores:• Semaphore called full – counts the number of

full slots.• Semaphore called empty – counts the number of

empty slots.• Semaphore called access – initialized to 1 to

control access to shared structures.

04/19/23 CST 352 - Operating Systems 80

Process SynchronizationProducer – Consumer – semaphore impl.

class semaphore{private:

int count;TCB* tcb_Q;

public:int inc( ); // increment and activateint dec( ); // decrement and suspendint getCount( ); // return semaphore state

};

04/19/23 CST 352 - Operating Systems 81

Process SynchronizationProducer – Consumer – semaphore impl.

// Shared resources

class BoundedBuffer

{

private:int buffer[MAX_ITEMS];

int nextInsert = 0; // Next index to produce to

int nextRemove = 0; // Next index to consume from

semaphore mutex(1); // resource access control

semaphore empty(MAX_ITEMS); // empty behavior control

semaphore full(0); // full behavior control

Public:void AddItem(int newItem);

int RemoveItem( void );

}

04/19/23 CST 352 - Operating Systems 82

Process SynchronizationProducer – Consumer – Code sample

// Producerstatic BoundedBuffer bb;

while (1){

bb.AddItem(rand( )); // Produce}

04/19/23 CST 352 - Operating Systems 83

Process SynchronizationProducer – Consumer – Code sample

// Add Itemvoid BoundedBuffer::AddItem (int newItem){

empty.dec( ); // Suspend if buffer is fullmutex.dec( ); // Wait if shared data is in usebuffer[nextInsert] = newItem; // Add to the buffer++nextInsert %= MAX_ITEMS; // Increment the insert pointmutex.inc( ); // Done with shared datafull.inc( ); // Kick off any consumers waiting

}

04/19/23 CST 352 - Operating Systems 84

Process SynchronizationProducer – Consumer – Code sample

// Consumerextern BoundedBuffer bb;int consumerInt;…while (1){

consumerInt = bb.RemoveItem ( ); // Consume}

04/19/23 CST 352 - Operating Systems 85

Process SynchronizationProducer – Consumer – Code sample

// Consumerint BoundedBuffer::RemoveItem(void){

int retVal;

full.dec( ); // Suspend if buffer is emptymutex.dec( ); // Lock up the shared dataretVal = buffer[nextRemove]; // Remove an item++nextRemove %= MAX_ITEMS; // Increment the remove pointmutex.inc( ); // Unlock shared dataempty.inc( ); // Kick off any producersreturn (retVal);

}

04/19/23 CST 352 - Operating Systems 86

Process SynchronizationMutexes

A semaphore provides a general purpose method to control multiple access to resources.When simple process exclusion is all that is required, a mutex can be used.A Mutex is a counting semaphore with count initialized to “1”;

04/19/23 CST 352 - Operating Systems 87

Process SynchronizationMutexesA Mutex has two methods:1. lock( ) – decrement the mutex value

to 0. If already 0, put the process to sleep;

2. unlock( ) – increment the mutex value from 0 to 1;

04/19/23 CST 352 - Operating Systems 88

Process SynchronizationQ:1. What is the difference between a

Semaphore and a Mutex?

04/19/23 CST 352 - Operating Systems 89

Process SynchronizationMonitorsA Monitor is a programming

language construct providing the functionality of a semaphore, however, a monitor is a programming language construct.

04/19/23 CST 352 - Operating Systems 90

Process SynchronizationMonitorsE.g. a monitor requires support of a

compiler.

It has been implemented in various flavors of Pascal, Modula, and Java.

04/19/23 CST 352 - Operating Systems 91

Process SynchronizationMonitors

A monitor is an encapsulation of procedures and data. Entry into the monitor is done through a public procedure.

04/19/23 CST 352 - Operating Systems 92

Process SynchronizationMonitorsCharacteristics:

All data variables are private.Access to the monitor is done only through public methods.Only one process may be active in the monitor at any given time.

04/19/23 CST 352 - Operating Systems 93

Process SynchronizationMonitors

Once a process/thread is inside the monitor, it must engage in cooperative multitasking using

cwait(c) – suspend process based on some condition ccsignal(c) – inform OS that any processes waiting on c may resume.

04/19/23 CST 352 - Operating Systems 94

Process SynchronizationMonitors

A Monitor provides an abstraction where data and a set of associated procedures are protected through compiler generated signaling primitives.

Monitor

Condition c1 Q

Condition c2 Q

Condition cn Q

cwait(c1)

cwait(c2)

cwait(cn)

Entry Q

Local Data

Condition Variables

Procedure 1

Init Code

Exit

Procedure n

Monitor WaitingArea

04/19/23 CST 352 - Operating Systems 95

Process SynchronizationMonitors

Remember, what we are dealing with here is defined entry points to data.

The interface defines the points where a process may gain access.

The process has no control over when the monitor will block it.

04/19/23 CST 352 - Operating Systems 96

Process SynchronizationMonitors

A process (thread) enters the monitor by calling one of the public functions.

The process (thread) will end up in the waiting area if it calls cwait(c) and c is a condition not yet met.

As soon as the condition c is met, if there are processes (threads) queued waiting on it, the next one in the queue will be unblocked.

A process (thread) will cause a condition to be met by calling csignal(c).

04/19/23 CST 352 - Operating Systems 97

Process SynchronizationMessage Passing

Two processes or threads will use “send( )” and “receive( )” to pass an agreed upon message.

04/19/23 CST 352 - Operating Systems 98

Process SynchronizationMessage Passing

send(message msg) – send a message to a shared message area. Block if the buffer is full.message receive( ) – receive a message from a shared message buffer. Block if the buffer is empty.

04/19/23 CST 352 - Operating Systems 99

Process SynchronizationMessage Passing

Process 1 Process 2

Message Queue

Send Receive

04/19/23 CST 352 - Operating Systems 100

Process SynchronizationMessage Passing

The OS manages the shared memory area containing message queues.The OS manages the blocking and activating processes using the message queue.

04/19/23 CST 352 - Operating Systems 101

Process SynchronizationMessage Passing

What is the quality of service you are capable of providing?

Guaranteed delivery?Ack/Nak protocol?How can you guarantee delivery for deadlock avoidance?

How will you provide security?

04/19/23 CST 352 - Operating Systems 102

Process SynchronizationBarriers

Barriers apply to a group of processes or threads within a process.A Barrier is used to make sure a group of processes (or threads) will all wait for a particular synchronization point.This concept is useful for problems that are divided into parallel computations.

04/19/23 CST 352 - Operating Systems 103

Process SynchronizationBarriers1. A process spawns off threads to

perform a computation.2. The OS assigns threads to run on one

of 32 processors.3. The parent process then waits for all

children threads to finish before continuing on using the results of the computation.

04/19/23 CST 352 - Operating Systems 104

Communication - IPCShared Memory

A segment of memory that can be accessed from two different process context.The OS must manage access to this memory segment.Address of a shared memory segment will not resolve to the process heap.

04/19/23 CST 352 - Operating Systems 105

Communication - IPCShared Memory

There are usually no synchronization primitives built into a shared memory segment.

The shared memory must be protected through the use of some synchronization primitive (see above).

04/19/23 CST 352 - Operating Systems 106

Communication - IPCNamed Pipes

A named pipe is a message passing mechanism.The message queue (the pipe) is usually implemented as a file on a disk.

This allows IPC to have a persistent behavior (communication could occur across process life).Disk access is slower than memory

04/19/23 CST 352 - Operating Systems 107

Communication - IPCNamed Pipes

There are also implementations that can cross network boundaries.

_pipe( ), _popen( ), _wpopen( ) in the MSDN to see Microsoft implementation.

04/19/23 CST 352 - Operating Systems 108

Classic IPCDining Philosophers

1. Five philosophers are at a table

2. Each plate has two forks next to it.

3. For a philosopher to eat, he/she must have two forks.

4. The life of a philosopher consists of eating and thinking.

04/19/23 CST 352 - Operating Systems 109

Classic IPCDining Philosophers

5. When a philosopher gets hungry, he first tries to acquire his left fork, then his right fork.

6. If he is successful, he eats for a while, then puts down the forks and continues to think.

Write a program that will allow all philosophers to live a well fed and well thought life.

04/19/23 CST 352 - Operating Systems 110

Classic IPCDining Philosophers

Try #1:1. Take left fork. If unavailable, block until it is available.

2. Take right fork. If unavailable, block until it is available.

3. Eat.

4. Put down right then left fork.

5. Think.

6. Repeat (go to 1).

Why does this fail?

04/19/23 CST 352 - Operating Systems 111

Classic IPCDining Philosophers

Try #2:1. Take left fork. If right fork is unavailable, put down the left

fork and wait for a while. Try again.

2. Take right fork.

3. Eat.

4. Put down right then left fork

5. Think

6. Repeat (go to 1).

Why does this fail?

04/19/23 CST 352 - Operating Systems 112

Classic IPCDining Philosophers

Try #2:If each philosopher waited a random

amount of time, the chances of a deadlock would be reduced.

This is what CSMA/CD does in the Ethernet protocol. (Carrier Sense Multiple Access/Collision Detection)

04/19/23 CST 352 - Operating Systems 113

Classic IPCDining Philosophers

Try #3:Use an array of state information to keep track of the state each philosopher is in – Eating, Thinking or Hungry (trying to acquire forks to eat).A Philosopher can only move into the Eating state if neither neighbor is eating.

04/19/23 CST 352 - Operating Systems 114

Classic IPCDining Philosophers

Try #3:Both neighbor philosophers must be checked before allowing the philosopher to eat.This analogy applies to processes in the CPU

An eating philosopher is a process in the CPU. The forks are shared resources requiring exclusive access for a process to run.

04/19/23 CST 352 - Operating Systems 115

Classic IPCDining Philosophers

User space solution:Each fork is protected by a semaphore.

The semaphore is implemented by the OS.

04/19/23 CST 352 - Operating Systems 116

Classic IPCReaders and Writers

Many processes and contained threads are attempting access to the same information.At any given time, any number of processes and contained threads may read the information source.Only one thread may write to the information source at any time.

04/19/23 CST 352 - Operating Systems 117

Classic IPCReaders and Writers

Any reader can gain access.A writer must wait until all readers are done before it can be allowed to write to the information source.This problem is typical of DBMS systems.

04/19/23 CST 352 - Operating Systems 118

Classic IPCSleeping Barber

There is a barber shopone barber.

one barber chair.

“n” chairs for waiting customers.

04/19/23 CST 352 - Operating Systems 119

Classic IPCSleeping Barber

If there are no customers present, the barber sits down in the barber chair and sleeps.

When a customer arrives, he wakes up the barber to get a haircut.

If an additional customer arrives, he will sit down if there are empty chairs. If there are no chairs, the customer will leave.

04/19/23 CST 352 - Operating Systems 120

Classic IPCSleeping Barber

The problem is to allow the customers to get their hair cut without getting into race conditions.

04/19/23 CST 352 - Operating Systems 121

Classic IPCSleeping Barber

One solution – use three semaphoresA semaphore to count the number of waiting customers.

A semaphore to keep track of the number of barbers.

A Mutex (binary semaphore) to keep track of when a customer is waiting for for the barber to start cutting.

Waiting – a counter to check the number of waiting customers.

04/19/23 CST 352 - Operating Systems 122

Communication - PTCVia Shared Heap

Process-to-Thread communication can be done through the shared heap.The shared heap acts as a shared memory segment, however, it does not require intervention of the OS.

04/19/23 CST 352 - Operating Systems 123

Communication - PTCVia Shared Heap1. Main thread creates shared data

structures in the shared heap.2. Main thread spawns child threads and

passes the address to the created data structures to the child threads.

3. Child threads use the shared memory to post information to the parent thread.

04/19/23 CST 352 - Operating Systems 124

Communication - TTCVia Shared Heap1. Parent thread sets up shared heap data

structures.2. Parent thread passes address of

structures to child threads.3. Threads then use shared structures to

pass information.4. Shared structures must be protected.

04/19/23 CST 352 - Operating Systems 125

Communication - TTCQ:1. What is the difference between

inter-process communication and thread to thread communication?

…yes, one is between processes and the other is between threads….but what else?

04/19/23 CST 352 - Operating Systems 126

SchedulingGoals

Maximize CPU utilization.

Minimize process waiting.

Enforce system processing priorities.

Provide all system resident processes with fair resource allocation.

04/19/23 CST 352 - Operating Systems 127

SchedulingThe area of

scheduling impact is shown in blue.

Suspend

Active

System R

eq. Complete

-or-

Logical A

ctiva

te

Execute

Dis

pat

ch

Preem

pt

Blocked

Logical S

uspend

Process Entry

Sys

tem

Cal

l or L

ogic

al S

uspe

nd Resource R

equest

Logical Block

Resource Available

Suspend Cleared - Process Still BlockedProcess Exit

04/19/23 CST 352 - Operating Systems 128

SchedulingDefinitions

Scheduler – The OS process or thread that is responsible for moving a TCB from the Active list to the an area where it will be dispatched next into the CPU.

Scheduling Algorithm – The policy the scheduler uses to determine what TCB to move.

04/19/23 CST 352 - Operating Systems 129

SchedulingDefinitions

Compute Bound Process – A process that spends the majority of it’s time waiting for the CPU.I/O Bound Process– A process that spends the majority of it’s time waiting for some I/O device.

04/19/23 CST 352 - Operating Systems 130

SchedulingBounded Curve:

CPU Speed

Deg

ree

of B

ound

edne

ssCPU Bounded

I/O Bounded

As CPUs get faster, they outpace the increases in I/O devices, causing processes to spend more of their run-time waiting for I/O devices.

04/19/23 CST 352 - Operating Systems 131

SchedulingQ:1.What happens to a compute bound

process as CPU speed increases?2.What happens to an IO bound

process as CPU speed increases?

04/19/23 CST 352 - Operating Systems 132

SchedulingWhen does the scheduler get invoked:1. Process/Thread Creation – A new process or

thread gets created by the OS. What process will run next?

2. Process/Thread Exit – What process or thread will run next?

3. I/O block or process/thread synchronization block – The current process or thread is suspended or blocked. What process or thread will run next?

04/19/23 CST 352 - Operating Systems 133

SchedulingWhen does the scheduler get invoked:

4. I/O interrupt – An I/O device is now available. What process or thread will run next?

5. Clock interrupt – The current process or thread turn is over. Next?

04/19/23 CST 352 - Operating Systems 134

SchedulingScheduling algorithms apply to both the

scheduling of CPU cycles from a “process” point of view (e.g. a collection of 1 or more threads), a “thread” point of view (e.g. how much “process” time should a contained thread get).

04/19/23 CST 352 - Operating Systems 135

SchedulingFirst Come – First Serve

The first process and/or contained threads in the active queue will run in the CPU to completion.

This algorithm defines a non-preemptive system.

04/19/23 CST 352 - Operating Systems 136

SchedulingShortest Process First

Based on historical run-time information or job-time estimates.Run the “shortest” job first.This is a non-preemptive algorithm.Only works if all processes to run are pre-determined.Typically used for older batch processing systems.

04/19/23 CST 352 - Operating Systems 137

SchedulingShortest Process First (preemptive

version):Run the process with the estimated “shortest” time left.Must keep a running estimate of how much time a job will take after each run.

04/19/23 CST 352 - Operating Systems 138

SchedulingThree Level Scheduling

Keep three queues1. Entering processes2. Processes “swapped” out to disk

(which ones should come in next?)3. CPU scheduling – from memory to

the CPU (active dispatch to execute).

04/19/23 CST 352 - Operating Systems 139

SchedulingThree Level Scheduling

This requires tracking of what types of processes are in the system

I/O bound processes (these may “thrash” the system).CPU bound processes (these may be process pigs).How much CPU time has the process used.How much I/O resource has the process used.Etc.

04/19/23 CST 352 - Operating Systems 140

SchedulingRound Robin Scheduling

Keep a circular linked list of TCBs.

Each TCB gets a turn in the CPU in a circular fashion.

Must balance context switching overhead with process run-time.

04/19/23 CST 352 - Operating Systems 141

SchedulingRound Robin Scheduling

PCB 1

PCB 3PCB 4

PCB 5 PCB 2

Exec

04/19/23 CST 352 - Operating Systems 142

SchedulingRound Robin Scheduling

PCB 1

PCB 3PCB 4

PCB 5 PCB 2

Exec

04/19/23 CST 352 - Operating Systems 143

SchedulingRound Robin Scheduling

PCB 1

PCB 3PCB 4

PCB 5 PCB 2

Exec

04/19/23 CST 352 - Operating Systems 144

SchedulingRound Robin Scheduling

PCB 1

PCB 3PCB 4

PCB 5 PCB 2

Exec

04/19/23 CST 352 - Operating Systems 145

SchedulingRound Robin Scheduling

PCB 1

PCB 3PCB 4

PCB 5 PCB 2Exec

04/19/23 CST 352 - Operating Systems 146

SchedulingRound Robin Scheduling

PCB 1

PCB 3PCB 4

PCB 5 PCB 2

Exec

04/19/23 CST 352 - Operating Systems 147

SchedulingPriority Scheduling

Each process or contained thread is assigned a priority.The process or contained threads with the highest priority get to run for a time slice.To prevent the highest priority process (thread) from stealing the CPU, the OS may decrease priority based on CPU time used.

04/19/23 CST 352 - Operating Systems 148

SchedulingPriority Scheduling

I/O bound processes (threads) should get a higher priority when the I/O device becomes available.Simple algorithm – Increase the priority to 1/f where f = fraction of time slice used by the process.

04/19/23 CST 352 - Operating Systems 149

SchedulingPriority Scheduling

Example:

If in a time slice of 30 ticks, a process uses 2 ticks, the priority will be increased by 15.

e.g. 1/(2/30) = 15

04/19/23 CST 352 - Operating Systems 150

SchedulingPriority Queue Scheduling

TCBs are assigned to queues.

Each queue has a priority.

A TCB from a higher priority queue will get a larger time quantum multiple than a TCB from a lower priority queue.

04/19/23 CST 352 - Operating Systems 151

SchedulingGuaranteed Scheduling

Guarantee a certain level of CPU time to a user process.Given the number of processes and contained threads running on the CPU, calculate the amount of time each process will get.

04/19/23 CST 352 - Operating Systems 152

SchedulingGuaranteed Scheduling

Example:• There are “n” processes (or

threads) running on a system.• Guarantee each (process or

thread) will receive 1/nth of the CPU time.

04/19/23 CST 352 - Operating Systems 153

SchedulingGuaranteed Scheduling

Example (cont’d):• The OS must keep track of how

much time in the CPU each process (and threads) uses.

04/19/23 CST 352 - Operating Systems 154

SchedulingGuaranteed Scheduling

Example (cont’d):For instance:

4 processes on a CPU.Each process gets ¼ (.25) of the cycles.2 processes blockedProcess “2” has used 10 of 100 possible CPU cycles -> 1/10 -> .10 -> .10/.25 -> ratio of .4

04/19/23 CST 352 - Operating Systems 155

SchedulingGuaranteed Scheduling

Example (cont’d):Process 1 has used 50/100 cycles = .50.Process 1 has used .25 excess cycles -> ratio of .50/.25 -> ratio of 2.0Dispatch process with the lowest ratio.

04/19/23 CST 352 - Operating Systems 156

SchedulingLottery

Processes (threads) are chosen from a pool based on a random decision.

Processes (threads) can be given more “tickets” to increase their chances of obtaining the right to be dispatched.

04/19/23 CST 352 - Operating Systems 157

SchedulingFair Share

Base scheduling on not only the process but also the process origination.

If user A starts up 1 process and user B starts up 9 User A process will get 50% of the CPU time User B process will divide the remaining 50%.

04/19/23 CST 352 - Operating Systems 158

SchedulingReal-Time Policies

Hard real-time policiesConfigure a process to react in a given amount of time.Failure to meet this deadline is considered a failure.

Soft real-time systemsTime constraint must be met within certain tolerances.