Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assuming that P(occupied) and V(occupied) each takes 1 ms and critical section c

ID: 3841250 • Letter: A

Question

Assuming that P(occupied) and V(occupied) each takes 1 ms and critical section consists of 4 instructions cr1, cr2, cr3, and cr4 of equal instruction time adding up to 1 ms and noncritical section consists of 4 instructions nc1, nc2, nc3, and nc4 of equal instruction time adding up to 2 ms.

a) Suppose OS schedules by round robin of thread 0 for 2ms, thread 1 for 2ms, thread 0 for 2ms, and thread 1 for 2ms.

Compute the real amount of time when OS moves between thread 0 and thread 1 each twice as above. Note the amount of time could be more than 8 ms since at the end of 2 ms, thread 0 may be in the middle of an atomic instruction.

b) Show which instruction is being executed in thread 0 when thread 0 and thread 1 each has been executed twice with 2ms as the time of scheduling.

c) Tabulate and show how much time is spent in each instruction of thread 0 in the first run and in the second run.

System 3 Create sema phore and initial ize value to 1 Semaphore occupied nevv Semaphore C1) start Threads C in i ti a li ze and launch both threads Thread I void main C JLO while C done 13 PC occupied o: wait 14 critical section code 16 17 18 VC occupied Signal 19 Code outside critica l ection end while 22 Thread TX

Explanation / Answer

I have written the program which tells you about the semaphore instructions for each thread. I have included the comments for each part of the code and attached the final output of it.

Let me explain you in simple and step-by-step procedure:-

Step-1:

The initial part is to know about how semaphores are being executed in a given critical section and how to guard each critical value from the thread signals.

Example:-

}

Step-2:

The next step is to use the thread management which deals with the synchronizationa using the Pthread, It creates a new thread and executes it well in main method.

Example:-

Step-3:

The final step is to use the terminate the semaphore using the thread operations.

Example:-

Semaphore Program:-


// This is the header files in C program for embedding the methods and properties of each class

#include <pthread.h>
#include <semaphoreObj.h>

// Created a function called "userIdFromName"

void *semaphoreOccupied( void *arg );

sem_t semaphoreObj;

// It is the starting point of C program

int main()
{
int semaTempName;

semaTempName = sem_init( &semaphoreObj, 0, 0 );

pthread_create( &threadObj[i], NULL, semaphoreOccupied, NULL );

pthread_join( threadObj[i], NULL );
sem_destroy( &semaphoreObj );
return 0;
}

void *semaphoreOccupied( void *arg )
{
sem_wait( &semaphoreObj );
pthread_exit( NULL );
}