Q1 The first known correct software solution to the critical-section problem for
ID: 3909396 • Letter: Q
Question
Q1 The first known correct software solution to the critical-section problem for n processes with a lower bound on waiting of n - 1 turns was presented by Eisenberg and McGuire. The processes share the following variables: enum pstate idle, want in, in cs; pstate flag[n]; int turn; All the elements of flag are initially idle. The initial value of turn is immaterial (betweenOand n-1). The structure of process Pi is shown below Prove that the algorithm satisfies all three requirements for the critical-section problem, namely: Mutual exclusion - Progress Bounded waiting while (true) ( flag[i] wt in; turn; while (j-i) { if (flag[j] !-idle) j-turn; else j (j + 1) % n; flag [i] = in cs; while ( (j )&&(turn-i flag [turn] -idle)) break; /critical section */ (turn + 1) % n; (j + 1) % n; while (flag [j]-idle) j turn flag[i]idle /*remainder section */ ) while (true)Explanation / Answer
hi, Please go threw the code. If You have any query please comment.
#include <stdio.h>
#include <pthread.h>
#define n 10
pthread_mutex_t lock;
enum pstate{idle,want_in, in_cs};
enum pstate flag[n];
int turn =0;
void * thread(void * arg)
{
int i =0 , j=0;
do {
while(1){
flag[i] = want_in;
j = turn;
while(j != i){
if(flag[j] != idle) j = turn;
else j= (j+1)%n;
}
flag[i] = in_cs;
j=0;
while((j<n) && (j == i || flag[j] != in_cs)) j++;
if( (j >= n) && (turn == i || flag[turn] == idle)) break;
}
pthread_mutex_lock(&lock);
j = (turn + 1) %n;
while(flag[j] == idle) j = (j+1) % n;
turn =j;
flag[i] = idle;
pthread_mutex_unlock(&lock);
}while(1);
pthread_exit(NULL);
}
int main(void)
{
pthread_t t[n];
int i =0;
for(i=0; i <n ;i++)
{
pthread_create(&t[i], NULL, thread, NULL);
}
for(i=0; i <n ;i++)
{
pthread_join(t[i], NULL);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.