Write a program to simulate the work of processes competing for updating a criti
ID: 3542852 • Letter: W
Question
Write a program to simulate the work of processes competing for updating a critical section. Declare and initialize an array of 10 ready processes identified by their numbers from 0 to 9. Select randomly three running processes. The purpose of these processes is to update the same integer variable cs. Each process when selected updates this variable by its id for example process 7 will update cs by 7.
Use this menu to use different methods of updates
1- For loop inside which I statements specify the turn of each process
2- Randomly select process turn
3- Select process in order in the selected array (FIFO)
4- Select process in reverse order (LIFO)
5- Shortest job first by searching the minimum and execute it first
Feel free to be creative by adding more options or by choosing c++ or Java.
Explanation / Answer
A fully working code : RUN it using command in linux : gcc -oterm -pthread test.cpp
methods of updates
1- For loop inside which I statements specify the turn of each process
2- Randomly select process turn
3- Select process in order in the selected array (FIFO)
4- Select process in reverse order (LIFO).
5 - shortest job not used here because each task is updating so they take equal time which is then becomes random number generation like 1st case .
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NUM_THREADS 10
int cs=0;
void *BusyWork(void *t)
{
int i;
long tid;
double result=0.0;
tid = (long)t;
printf("Thread %ld starting... ",tid);
cs=tid;
printf("Thread %ld done. cs = %d ",tid, cs);
pthread_exit((void*) t);
}
int main (int argc, char *argv[])
{
pthread_t thread[NUM_THREADS];
pthread_attr_t attr;
int rc;
long t;
void *status;
srand (time(NULL)); //time seeded random number generation
/* Initialize and set thread detached attribute */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
int i=0;
int lifo=0;
int fifo=9;
int m;
printf("specify which methods of updates to use 1- Randomly select process turn 2- For loop inside which I statements specify the turn of each process 3- Select process in order in the selected array (FIFO) 4- Select process in reverse order (LIFO) ");
scanf("%d",&m);
while(i<4){
int temp;
switch (m){case 1: temp=(rand() % 9);break; //random process choosing
case 2: printf("enter process number : ");scanf("%d",&temp);break; // choosing by user which process to use
case 3: temp=lifo;lifo++;break; // LIFO method used
case 4: temp = fifo;fifo++;break; //FIFO method; use
default :temp=(rand() % 9);break; }
//int temp=(rand() % 9); //random process choosing
printf("Main: creating process %d ", temp);
rc = pthread_create(&thread[temp], &attr, BusyWork, (void *)temp);
if (rc) {
printf("ERROR; return code from pthread_create() is %d ", rc);
exit(-1);
}
i++;
}
pthread_exit(NULL);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.