0% found this document useful (0 votes)
16 views3 pages

Lab 6

The document discusses key concepts related to operating systems, particularly focusing on critical sections, race conditions, and semaphore usage. It outlines the problems associated with critical sections and provides solutions, including the Paterson's solution, while illustrating the behavior of processes in various scenarios. Additionally, it explains the application of semaphores in process synchronization and the execution flow of tasks among multiple processes.

Uploaded by

zirunw6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Lab 6

The document discusses key concepts related to operating systems, particularly focusing on critical sections, race conditions, and semaphore usage. It outlines the problems associated with critical sections and provides solutions, including the Paterson's solution, while illustrating the behavior of processes in various scenarios. Additionally, it explains the application of semaphores in process synchronization and the execution flow of tasks among multiple processes.

Uploaded by

zirunw6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

COMP 2670SEF/S267F/8670SEF/S267W (2025) Operating Systems

Tutorial 6

Stage 1: Concepts and Keywords of Chapter 5


1. The outcome of processes depending on the execution order of the instructions is Race condition
known as _____ _____.
2. Maintaining _____ _____requires mechanisms to ensure the orderly execution of Data consistency
cooperating processes.
3. _______ __________ in the multiprogramming context refers that only one of the Mutual selection
two (or many) processes can hold a resource.
4. The parts of the program or software system that are involved in concurrent Critical selection
access to a shared resource or data is called a ____________ __________.
5. _________ is a requirement of a proper critical section solution that specifies a progress
waiting process must eventually be allowed to move into critical section.
6. In the producer-consumer system example covered in lecture, calculation error interleaved
occurred when the execution of produced and consumer ____________ before
the whole statement of add one and subtract one is completed.
7. The execution sequence of instructions from multiple processes is decided by Cpu scheduler
________ ____________.
8. For semaphore, the ____ function plays the role of a gate, which stops processes if Wait()
the semaphore variable is not positive. the ____ function plays the role of a
notifier, which notifies if there is any process waiting for entry into the critical Signal()
section.

Stage 2: A Solution for Critical Section


The following shows a solution for the critical section problem.

Process 1 Process 2
flag[1] = false; flag[2] = false;
... ...

while (flag[2] == true) while (flag[1] ==true)


<-Entry
<-Entry
; /* busy wait */ selection ; /* busy wait */
selection
flag[1] = true; flag[2] = true;

**Critical Section** **Critical Section**

flag[1] = false; <-Exit flag[2] = false; <-Exit


selection selection

Remainder Section; Remainder Section;

1. Identify the critical section, entry section, and exit section in the program code.
2. Discuss if this solution satisfies the essential criteria for a proper critical section solution.
It does not even satisfy mutual exclusion. The following scheduling pattern for the processes will end up with
both into the critical section. One example is sufficient to falsify the condition:
Process 1 is scheduled to run
Process 1 arrives and reach the Entry selection
Process 1 found flag[2] is false and break from the while loop
Process 1 is pre-empted
Process 2 runs and arrives and reach the Entry selection
Process 2 found flag[1]is false and break from the while loop
Process 2 set flag[2] to true and moves into the critical selection
Process 2 is pre-empted
Process 1 set flag[1] to true and moves into the critical selection
Stage 3: The Paterson's Solution
In the lecture we showed the Paterson's solution to satisfy the three conditions to a critical section solution.
The following shows the version given in the lecture.

Process 1 Process 2
/* PROCESS 1 */ /* PROCESS 2 */
while (true) { while (true) {

flag[1] = true; flag[2] = true;


turn = 2; turn = 1;

while (flag[2] == true && turn == 2) while (flag[1] == true && turn == 1)
; /* busy wait */ ; /* busy wait */

Critical Section; Critical Section;

flag[1] = false; flag[2] = false;

Remainder Section; Remainder Section;


} }

In each of the following situations, state what is stored in each of the variables flag[1], flag[2], and turn.
 Only Process 1 arrived and reached the while loop, and Process 2 never arrived.
If process 2 never arrives , then flag[2] is False. When Process 1 reached the while loop, then flag[1]is
True and turn is 2.The while condition is False. Process 1 is not trapped by the while loop.
 Only Process 1 arrived and entered the critical section, and Process 2 never arrived.
If process 2 never arrives , then flag[2] is False. When Process 1 is in the critical selection, then flag[1]
is True and turn is 2

 Only Process 2 arrived and reached the while loop, and Process 1 never arrived.
If process 1 never arrives , then flag[1] is False. When Process 2 reached the while loop, then flag[2]is
True and turn is 1.The while condition is False. Process 2 is not trapped by the while loop.

 Process 1 and Process 2 arrived at the same time and they both reached the while loop.
Both flag[1]and flag[2] are True because Process 1 and 2 have arrived. The variable turn maybe 1 or
2,depending on marginally which Process arrived a bit earlier. In this case, for the two while
loops,only 1 of them would trap the Process, and the other one will let the Process entering the
critical section.

Stage 4: Application of Semaphores in General Problems


In addition to implementing critical sections, semaphores are useful for implementing other process
synchronization problems.
1. Simulate what would happen if the following processes were to be executed. Assume that the initial value
of S is 0.
Process 1 Process 2 Process 3

...

Wait(S) Wait(S) Do Task C

Do Task A Do Task B Signal(S)

Signal(S) Signal(S)

Exit

Process 1 is not allowed to do Task A nor Process 2 is not allowed to do Task B before Process 3 comes and
send the Signal after doing task C

2. If Process 3 terminates after sending the Signal, will both Task A and Task B be executed?
Yes, both should be executed if Process 3 sends out the signal.One of Process 1 or 2 will receive the
signal and return from the Wait function call and do the Task. After completing the Task, the process
will send a signal to allow another process to do the Task.

You might also like