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

Design of a semaphore simulator to manage multiple threads within the same addre

ID: 3557485 • Letter: D

Question

Design of a semaphore simulator to manage multiple threads within the same address space. ( C language)

Design of a semaphore simulator to manage multiple threads within the same address space Evaluation Criteria Use of ready and blocked queue Use of semaphore algorithm Use of process control block Clear Code with documentation and Comments Management of multiple random scenarios Requirements Program should ask for amount of processes, and for each process, the type of resources required. Types of resources should be made available. At least two processes should use the same type of resource. Reference (PID) to each process should be used in the queues. A process should be placed in the blocked queue when it is waiting for a resource response or when it needs a resource already in use by another process. Program should store the process context in a process control block. Programs should move from the ready queue, to the processor, and then to the blocked queue and finally back to the ready queue until completion. Program should be able to show the queues and semaphore signal status at any moment upon user request. Resource response may trigger upon pressing a designated key for a resource, which can be changed In the code by the use of macros.

Explanation / Answer

#include #include #include #include #include #include /* a structure that will be used between processes */ typedef struct { sema_t mysema; int num; } buf_t; main() { int i, j, fd; buf_t *buf; /* open a file to use in a memory mapping */ fd = open("/dev/zero", O_RDWR); /* create a shared memory map with the open file for the data structure that will be shared between processes */ buf=(buf_t *)mmap(NULL, sizeof(buf_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); /* initialize the semaphore -- note the USYNC_PROCESS flag; this makes the semaphore visible from a process level */ sema_init(&buf->mysema, 0, USYNC_PROCESS, 0); /* fork a new process */ if (fork() == 0) { /* The child will run this section of code */ for (j=0;jmysema); /* the child decremented the semaphore */ printf("Child PID(%d): decrement semaphore. ", getpid()); } /* exit the child process */ printf("Child PID(%d): exiting... ", getpid()); exit(0); } /* The parent will run this section of code */ /* give the child a chance to start running */ sleep(2); for (i=0;imysema); /* wait a second */ sleep(1); } /* exit the parent process */ printf("Parent PID(%d): exiting... ", getpid()); return(0); }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote