operating systems
DESCRIPTION
Operating Systems. CST 352 Processes and Threads. Topics. Definitions Communications Process to Process Process to Thread Thread to Thread Scheduling. Definitions - Prelims. Concurrency – The appearance that threads are running simultaneously even though there is a single CPU. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/1.jpg)
04/24/23 CST 352 - Operating Systems 1
Operating Systems
CST 352Processes and Threads
![Page 2: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/2.jpg)
04/24/23 CST 352 - Operating Systems 2
Topics• Definitions• Communications
–Process to Process–Process to Thread–Thread to Thread
• Scheduling
![Page 3: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/3.jpg)
04/24/23 CST 352 - Operating Systems 3
Definitions - PrelimsConcurrency – The appearance
that threads are running simultaneously even though there is a single CPU.
![Page 4: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/4.jpg)
04/24/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.
![Page 5: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/5.jpg)
04/24/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”?
![Page 6: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/6.jpg)
04/24/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.
![Page 7: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/7.jpg)
04/24/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
![Page 8: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/8.jpg)
04/24/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?
![Page 9: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/9.jpg)
04/24/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.
![Page 10: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/10.jpg)
04/24/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”.
![Page 11: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/11.jpg)
04/24/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.
![Page 12: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/12.jpg)
04/24/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.
![Page 13: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/13.jpg)
04/24/23 CST 352 - Operating Systems 13
Definitions - ThreadQ:
1.Define Process2.Define Thread3.Define Context
![Page 14: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/14.jpg)
04/24/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
![Page 15: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/15.jpg)
04/24/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.
![Page 16: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/16.jpg)
04/24/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
![Page 17: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/17.jpg)
04/24/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”.
![Page 18: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/18.jpg)
04/24/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).
![Page 19: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/19.jpg)
04/24/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.
![Page 20: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/20.jpg)
04/24/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.
![Page 21: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/21.jpg)
04/24/23 CST 352 - Operating Systems 21
Processes and ThreadsProcess/Thread/Fiber States:
Keep in mind the processing hierarchy:Processes contain threadsThreads contain fibers
– Process state change implies thread state change– Thread state change implies fiber state change
The inverse is not true.
![Page 22: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/22.jpg)
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/24/23 CST 352 - Operating Systems 22
Suspend
Active
System R
eq. Complete
-or-
Logica
l Acti
vate
Execute
Dis
patc
h Preem
pt
Blocked
Logica
l Susp
end
ProcessEntry
Syst
em C
all o
r Log
ical S
uspe
nd Resource Request
Logical Block
Resource Available
Suspend Cleared - Process Still BlockedProcess Exit
Process State Transition Analysis:
![Page 23: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/23.jpg)
04/24/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
System Req. Complete
-or-
Logical Activ
ate
Execute
Dis
patc
h Preem
pt
Blocked
Logica
l Susp
end
Process Entry
Syst
em C
all o
r Log
ical S
uspe
nd Resource Request
Logical Block
Resource Available
Suspend Cleared - Process Still BlockedProcess Exit
![Page 24: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/24.jpg)
04/24/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-
Logica
l Acti
vate
Execute
Dis
patc
h Preem
pt
Blocked
Logica
l Susp
end
Process Entry
Syst
em C
all o
r Log
ical S
uspe
nd Resource Request
Logical Block
Resource Available
Suspend Cleared - Process Still BlockedProcess Exit
![Page 25: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/25.jpg)
04/24/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-
Logica
l Acti
vate
Execute
Dis
patc
h Preempt
Blocked
Logica
l Susp
end
Process Entry
Syst
em C
all o
r Log
ical S
uspe
nd Resource Request
Logical Block
Resource Available
Suspend Cleared - Process Still BlockedProcess Exit
![Page 26: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/26.jpg)
04/24/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-
Logica
l Acti
vate
Execute
Dis
patc
h Preem
pt
Blocked
Logica
l Susp
end
Process Entry
Syst
em C
all o
r Log
ical S
uspe
nd Resource Request
Logical Block
Resource Available
Suspend Cleared - Process Still BlockedProcess Exit
![Page 27: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/27.jpg)
04/24/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-
Logica
l Acti
vate
Execute
Dis
patc
h Preem
pt
Blocked
Logica
l Susp
end
Process Entry
Syst
em C
all o
r Log
ical
Susp
end
Resource Request
Logical Block
Resource Available
Suspend Cleared - Process Still BlockedProcess Exit
![Page 28: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/28.jpg)
04/24/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-
Logica
l Acti
vate
Execute
Dis
patc
h Preem
pt
Blocked
Logica
l Susp
end
Process Entry
Syst
em C
all o
r Log
ical S
uspe
nd Resource Request
Logical Block
Resource Available
Suspend Cleared - Process StillBlockedProcess Exit
![Page 29: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/29.jpg)
04/24/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-
Logica
l Acti
vate
Execute
Dis
patc
h Preem
pt
Blocked
Logica
l Susp
end
Process Entry
Syst
em C
all o
r Log
ical S
uspe
nd Resource Request
Logical Block
Resource Available
Suspend Cleared - Process Still BlockedProcess Exit
![Page 30: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/30.jpg)
04/24/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-
Logica
l Acti
vate
Execute
Dis
patc
h Preem
pt
Blocked
Logica
l Susp
end
Process Entry
Syst
em C
all o
r Log
ical S
uspe
nd Resource Request
Logical Block
Resource Available
Suspend Cleared - Process Still BlockedProcess Exit
![Page 31: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/31.jpg)
04/24/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-
Logica
l Acti
vate
Execute
Dis
patc
h Preem
pt
Blocked
Logical Suspend
Process Entry
Syst
em C
all o
r Log
ical S
uspe
nd Resource Request
Logical Block
Resource Available
Suspend Cleared - Process Still BlockedProcess Exit
![Page 32: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/32.jpg)
04/24/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-
Logica
l Acti
vate
Execute
Dis
patc
h Preem
pt
Blocked
Logica
l Susp
end
Process Entry
Syst
em C
all o
r Log
ical S
uspe
nd Resource Request
Logical Block
Resource Available
Suspend Cleared - Process Still BlockedProcess Exit
![Page 33: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/33.jpg)
04/24/23 CST 352 - Operating Systems 33
Processes and ThreadsQ:
1. What states are required for implementation of a multitasking kernel?
![Page 34: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/34.jpg)
04/24/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?
![Page 35: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/35.jpg)
04/24/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.
![Page 36: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/36.jpg)
04/24/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.
![Page 37: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/37.jpg)
04/24/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.
![Page 38: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/38.jpg)
04/24/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.
![Page 39: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/39.jpg)
04/24/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.
![Page 40: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/40.jpg)
04/24/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.
![Page 41: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/41.jpg)
04/24/23 CST 352 - Operating Systems 41
Processes and ThreadsQ:1. Define Swapping2. Define Paging3. Does swapping require virtual
memory? Why or why not?
![Page 42: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/42.jpg)
04/24/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)
![Page 43: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/43.jpg)
04/24/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
![Page 44: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/44.jpg)
04/24/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.
![Page 45: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/45.jpg)
04/24/23 CST 352 - Operating Systems 45
OS Requirements for Thread ImplementationUser 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.
![Page 46: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/46.jpg)
04/24/23 CST 352 - Operating Systems 46
OS Requirements for Thread ImplementationHybrid 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).
![Page 47: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/47.jpg)
04/24/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.
![Page 48: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/48.jpg)
04/24/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.
![Page 49: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/49.jpg)
04/24/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.
![Page 50: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/50.jpg)
04/24/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;
}
![Page 51: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/51.jpg)
04/24/23 CST 352 - Operating Systems 51
Process SynchronizationRace Conditions: - Example
1. Initial Conditions:Thread T1 – varOne = 5, varTwo = 8Thread 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.
![Page 52: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/52.jpg)
04/24/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.
![Page 53: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/53.jpg)
04/24/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.
![Page 54: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/54.jpg)
04/24/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.
![Page 55: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/55.jpg)
04/24/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
![Page 56: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/56.jpg)
04/24/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?
![Page 57: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/57.jpg)
04/24/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.
![Page 58: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/58.jpg)
04/24/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:
![Page 59: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/59.jpg)
04/24/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.
![Page 60: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/60.jpg)
04/24/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.
![Page 61: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/61.jpg)
04/24/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];
};
![Page 62: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/62.jpg)
04/24/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
}}
}
![Page 63: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/63.jpg)
04/24/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);}
}
![Page 64: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/64.jpg)
04/24/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;};
![Page 65: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/65.jpg)
04/24/23 CST 352 - Operating Systems 65
Process SynchronizationQ:1 – What is the difference between Strict Alteration
and Peterson’s Algorithm?
![Page 66: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/66.jpg)
04/24/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 SetCheck the valueIf the value was 1, loop until it test and set returns a previous value of 0.
![Page 67: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/67.jpg)
04/24/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.
![Page 68: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/68.jpg)
04/24/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.
![Page 69: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/69.jpg)
04/24/23 CST 352 - Operating Systems 69
Process SynchronizationProducer – Consumer – Code sample
// Shared resource – Bounded Bufferint buffer[MAX_ITEMS];int nextInsert = 0; // Next index to produce toint nextRemove = 0; // Next index to consume fromint count = 0; // Number of items in the buffer
![Page 70: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/70.jpg)
04/24/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
}
![Page 71: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/71.jpg)
04/24/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;
}
![Page 72: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/72.jpg)
04/24/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.
![Page 73: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/73.jpg)
04/24/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.
![Page 74: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/74.jpg)
04/24/23 CST 352 - Operating Systems 74
Process SynchronizationCounting Semaphores
A semaphore is a construct that allows the saving of “wakeup” signals.
![Page 75: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/75.jpg)
04/24/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.
![Page 76: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/76.jpg)
04/24/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.
![Page 77: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/77.jpg)
04/24/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.
![Page 78: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/78.jpg)
04/24/23 CST 352 - Operating Systems 78
Process SynchronizationQ:1. Write the pseudo code for a
counting semaphore.
![Page 79: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/79.jpg)
04/24/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.
![Page 80: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/80.jpg)
04/24/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
};
![Page 81: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/81.jpg)
04/24/23 CST 352 - Operating Systems 81
Process SynchronizationProducer – Consumer – semaphore impl.
// Shared resourcesclass BoundedBuffer{private:
int buffer[MAX_ITEMS];int nextInsert = 0; // Next index to produce toint nextRemove = 0; // Next index to consume from
semaphore mutex(1); // resource access controlsemaphore empty(MAX_ITEMS); // empty behavior controlsemaphore full(0); // full behavior control
Public:void AddItem(int newItem);int RemoveItem( void );
}
![Page 82: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/82.jpg)
04/24/23 CST 352 - Operating Systems 82
Process SynchronizationProducer – Consumer – Code sample
// Producerstatic BoundedBuffer bb;
while (1){
bb.AddItem(rand( )); // Produce}
![Page 83: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/83.jpg)
04/24/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
}
![Page 84: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/84.jpg)
04/24/23 CST 352 - Operating Systems 84
Process SynchronizationProducer – Consumer – Code sample
// Consumerextern BoundedBuffer bb;int consumerInt;…while (1){
consumerInt = bb.RemoveItem ( ); // Consume}
![Page 85: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/85.jpg)
04/24/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);
}
![Page 86: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/86.jpg)
04/24/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”;
![Page 87: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/87.jpg)
04/24/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;
![Page 88: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/88.jpg)
04/24/23 CST 352 - Operating Systems 88
Process SynchronizationQ:1. What is the difference between a
Semaphore and a Mutex?
![Page 89: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/89.jpg)
04/24/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.
![Page 90: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/90.jpg)
04/24/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.
![Page 91: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/91.jpg)
04/24/23 CST 352 - Operating Systems 91
Process SynchronizationMonitorsA monitor is an encapsulation of
procedures and data. Entry into the monitor is done through a public procedure.
![Page 92: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/92.jpg)
04/24/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.
![Page 93: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/93.jpg)
04/24/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.
![Page 94: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/94.jpg)
04/24/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
![Page 95: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/95.jpg)
04/24/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.
![Page 96: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/96.jpg)
04/24/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).
![Page 97: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/97.jpg)
04/24/23 CST 352 - Operating Systems 97
Process SynchronizationMessage Passing
Two processes or threads will use “send( )” and “receive( )” to pass an agreed upon message.
![Page 98: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/98.jpg)
04/24/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.
![Page 99: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/99.jpg)
04/24/23 CST 352 - Operating Systems 99
Process SynchronizationMessage Passing
Process 1 Process 2
Message Queue
Send Receive
![Page 100: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/100.jpg)
04/24/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.
![Page 101: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/101.jpg)
04/24/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?
![Page 102: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/102.jpg)
04/24/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.
![Page 103: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/103.jpg)
04/24/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.
![Page 104: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/104.jpg)
04/24/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.
![Page 105: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/105.jpg)
04/24/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).
![Page 106: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/106.jpg)
04/24/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
![Page 107: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/107.jpg)
04/24/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.
![Page 108: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/108.jpg)
04/24/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.
![Page 109: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/109.jpg)
04/24/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.
![Page 110: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/110.jpg)
04/24/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?
![Page 111: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/111.jpg)
04/24/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 fork5. Think6. Repeat (go to 1).
Why does this fail?
![Page 112: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/112.jpg)
04/24/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)
![Page 113: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/113.jpg)
04/24/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.
![Page 114: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/114.jpg)
04/24/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.
![Page 115: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/115.jpg)
04/24/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.
![Page 116: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/116.jpg)
04/24/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.
![Page 117: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/117.jpg)
04/24/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.
![Page 118: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/118.jpg)
04/24/23 CST 352 - Operating Systems 118
Classic IPCSleeping Barber
There is a barber shopone barber.one barber chair.“n” chairs for waiting customers.
![Page 119: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/119.jpg)
04/24/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.
![Page 120: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/120.jpg)
04/24/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.
![Page 121: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/121.jpg)
04/24/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.
![Page 122: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/122.jpg)
04/24/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.
![Page 123: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/123.jpg)
04/24/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.
![Page 124: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/124.jpg)
04/24/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.
![Page 125: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/125.jpg)
04/24/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?
![Page 126: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/126.jpg)
04/24/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.
![Page 127: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/127.jpg)
04/24/23 CST 352 - Operating Systems 127
SchedulingThe area of
scheduling impact is shown in blue.
Suspend
Active
System R
eq. Complete
-or-
Logica
l Acti
vate
Execute
Dis
patc
h Preempt
Blocked
Logica
l Susp
end
Process Entry
Syst
em C
all o
r Log
ical S
uspe
nd Resource Request
Logical Block
Resource Available
Suspend Cleared - Process Still BlockedProcess Exit
![Page 128: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/128.jpg)
04/24/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.
![Page 129: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/129.jpg)
04/24/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.
![Page 130: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/130.jpg)
04/24/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.
![Page 131: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/131.jpg)
04/24/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?
![Page 132: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/132.jpg)
04/24/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?
![Page 133: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/133.jpg)
04/24/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?
![Page 134: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/134.jpg)
04/24/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).
![Page 135: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/135.jpg)
04/24/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.
![Page 136: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/136.jpg)
04/24/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.
![Page 137: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/137.jpg)
04/24/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.
![Page 138: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/138.jpg)
04/24/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).
![Page 139: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/139.jpg)
04/24/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.
![Page 140: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/140.jpg)
04/24/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.
![Page 141: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/141.jpg)
04/24/23 CST 352 - Operating Systems 141
SchedulingRound Robin Scheduling
PCB 1
PCB 3PCB 4
PCB 5 PCB 2
Exec
![Page 142: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/142.jpg)
04/24/23 CST 352 - Operating Systems 142
SchedulingRound Robin Scheduling
PCB 1
PCB 3PCB 4
PCB 5 PCB 2
Exec
![Page 143: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/143.jpg)
04/24/23 CST 352 - Operating Systems 143
SchedulingRound Robin Scheduling
PCB 1
PCB 3PCB 4
PCB 5 PCB 2
Exec
![Page 144: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/144.jpg)
04/24/23 CST 352 - Operating Systems 144
SchedulingRound Robin Scheduling
PCB 1
PCB 3PCB 4
PCB 5 PCB 2
Exec
![Page 145: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/145.jpg)
04/24/23 CST 352 - Operating Systems 145
SchedulingRound Robin Scheduling
PCB 1
PCB 3PCB 4
PCB 5 PCB 2Exec
![Page 146: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/146.jpg)
04/24/23 CST 352 - Operating Systems 146
SchedulingRound Robin Scheduling
PCB 1
PCB 3PCB 4
PCB 5 PCB 2
Exec
![Page 147: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/147.jpg)
04/24/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.
![Page 148: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/148.jpg)
04/24/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.
![Page 149: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/149.jpg)
04/24/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
![Page 150: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/150.jpg)
04/24/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.
![Page 151: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/151.jpg)
04/24/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.
![Page 152: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/152.jpg)
04/24/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.
![Page 153: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/153.jpg)
04/24/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.
![Page 154: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/154.jpg)
04/24/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
![Page 155: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/155.jpg)
04/24/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.
![Page 156: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/156.jpg)
04/24/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.
![Page 157: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/157.jpg)
04/24/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%.
![Page 158: Operating Systems](https://reader035.vdocument.in/reader035/viewer/2022062816/56813a84550346895da281f7/html5/thumbnails/158.jpg)
04/24/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.