There are two processes running on two cores in one CPU same

There are two processes running on two cores in one CPU (same OS). They are sharing a memory location called “share” (with a start value of 0).

P: while(1) { temp = share; temp = temp + 1; share = temp; }

C: while(1) { temp = share; cout << temp; }

(a) Will the numbers: 1,2,3,4,5 etc print consecutively (briefly explain)?

(b) We want print the numbers: 1,2,3,4,5 etc consecutively with no gaps. Using a binary semaphore called “s” and the semaphore operations: signal(s) – which indicates the semaphore (resource) will be used and wait(s) – which waits until the resource is available, modify P and C to print, in order.

(c) Show how, using the machine instructions “TestAndSet” or “Swap” you can implement the signal operation. What does signal do, what does it return?

(d) With your modifications, can P and C, running concurrently, deadlock? How or why not?

* I understand I am not supposed to ask multiple questions at once, but these questions cannot be asked individually. I apologize in advance.

Solution

a) The numbers will not get printed consecutively, since P and C may not execute sequentially. It is possible that P can execute the while loop multiple times before C can execute or vice versa. In such case, the numbers will not print in order.

b)

wait(S) {

   while(S<=0)
   ; //busy wait
  
   S = 0;

}

signal(S) {
  
   S = 1;
}


P:
while(1) {
   wait(S);
   temp = share;
   temp = temp + 1;
   share = temp;
   signal(S);
}

C:
while(1) {
   wait(S);
   temp = share;
   cout << temp;
   signal(S);
}

c)

signal() {
  
   S = 1;
   TestAndSet(S);

}

d) Deadlock can occur if P and C both execute the wait(S) one after another. In that case bothe P and C will wait for each other to execute signal(s) and deadlock will occur

There are two processes running on two cores in one CPU (same OS). They are sharing a memory location called “share” (with a start value of 0). P: while(1) { te
There are two processes running on two cores in one CPU (same OS). They are sharing a memory location called “share” (with a start value of 0). P: while(1) { te

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site