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

Hello. Can someone please help me with this program. It is written in C-- based

ID: 3708185 • Letter: H

Question

Hello. Can someone please help me with this program. It is written in C-- based on the BACI interpreter. First I will post the instructions and then I will post the codes that I have so far (which doesn't perform the functions highlighted and may have errors).

Please note the yellow highlights, It's important that the program performs these functions that are highlighted as well.

And also, please include comments so that I can better understand how each function works.

Thank you in advance!

----------------- Below are the instructions on how the program should perform ---------------------

--------- Here is the code I have so far (Please don't repost this as the answer!)--------------------------

//Declare our variables
const int bufferSize = 5;
const int loopAmount = 5;

semaphore FULL1, FULL2, FULL3;
semaphore MUTEX1, MUTEX2, MUTEX3;
semaphore PRINT;
semaphore PIO, SIO;
semaphore operationComplete;
semaphore requestPending;
semaphore requestService[bufferSize];

int IORQid[bufferSize];
int IORQaddr[bufferSize];
int bufferAddress = 0, bufferID = 0, track = 0;


//Function to intialize user to execute system call
void User (int userID){
int a, b, address = 0;

for(b = 0; b < bufferSize; b++){
address = random(a + 1) % 200;

p(FULL1);
p(MUTEX1);

bufferID = userID;
bufferAddress = address;

p(PRINT);
cout << "User " << userID + 1 << " executes system call SIO or DOIO" << endl;

v(PRINT);
v(MUTEX1);
v(SIO);

p(requestService[userID]);

}
}

//Function for assembly and insertion
void DOIO(){
int a = 0, tempID, tempAddr, index;

for (index = 0; index < loopAmount; index++){

p(SIO);
p(MUTEX1);

tempID = bufferID;
tempAddr = bufferAddress;

v(MUTEX1);
v(FULL1);

p(FULL2);
p(MUTEX2);

IORQid[a] = tempID;
IORQaddr[a] = tempAddr;

a = (a + 1) % 5;

p(PRINT);
cout << "DOIO assembles IORB and inserts it in IORQ" << endl;

v(PRINT);
v(MUTEX2);
v(requestPending);

}
}

//Driver function
void DeviceDriver(){
int b = 0, driverAddress = 0, driverID = 0, a;

for (a = 0; a < loopAmount; a++){

p(requestPending);
p(MUTEX2);

driverID = IORQid[b];
driverAddress = IORQaddr[b];

b = (b + 1) % 5;

v(MUTEX2);
v(FULL2);

p(FULL3);
p(MUTEX3);

track = driverAddress;

v(MUTEX3);
v(PIO);

p(PRINT);
cout << "Driver initiates I/O operation for user " << driverID + 1<< endl;

v(PRINT);
cout << "Disk Completes I/O operation (disk does not know what process initiated the I/O operation)" << endl;

p(operationComplete);

v(requestService[driverID]);

p(PRINT);

v(PRINT);
}
}

//Disk function
void Disk(){
int a = 0, seek = 0, diskAddress = 0, b;

for (b = 0; b < loopAmount; b++){
p(PIO);
p(MUTEX3);

seek = track;

v(MUTEX3);

diskAddress = seek * 20;   

for (a = 1; a <= diskAddress; a++){
}

v(operationComplete);

p(PRINT);
cout << "Driver signal user 1 (operation complete)" << endl;

v(PRINT);
v(FULL3); //Release control of disk
}
}

//Main function for initialization of request
main (){

initialsem (MUTEX1, 1);
initialsem (MUTEX2, 1);
initialsem (MUTEX3, 1);
initialsem (PRINT,1);
initialsem (FULL1, 1);
initialsem (FULL2, 5);
initialsem (FULL3, 1);
initialsem (SIO, 0);
initialsem (PIO, 0);
initialsem (requestPending, 0);
initialsem (operationComplete, 0);
initialsem (requestService[0], 0);
initialsem (requestService[1], 0);
initialsem (requestService[2], 0);
initialsem (requestService[3], 0);
initialsem (requestService[4], 0);

cobegin{
User(0);
DOIO();
DeviceDriver();
Disk();
}
}

User programs will communicate with DOIo (OS) to request an I O operation. (This will simulate a system call) User programs will give to DOIO three parameters. User id, device number dev is a random number in the range 1 and 2 that represents device one or device two) and an address (addr is a random number in the range 1 and 20.) (addr is an integer that represents a track number in the hard drive).| User programs will pass the parameters to DOIO through three buffers of size one each (bufid, bufdev, and bufaddr) Once the parameters are stored in the buffers, user programs executes a p request served?index" ]) operation to wait for the completion of the IO operation. You will need a semaphore array request served[5] There will be five users running concurrently and each will execute 5 IO operations DOIO: DOIO will collect and id, device(dev), and address(addr) from bufid, bufdev, and bufaddr to assemble the IORHB DOIO will check on device number to decide which one of the two devices will get the IORB Once the device has been selected, DOIO will store the IORB (id and addr) into the two buffers that represent the IORO (iorgid and iorgaddr) of the selected device. Notice that you need separate buffers (one for each device: iorgid and iorgaddr and iorqid2 and iorqaddr2)

Explanation / Answer

Code

#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
#include <semaphore.h>
#include <sys/types.h>
#include <pthread.h>
#include <unistd.h>


const int COUNTER = 5;      

int TOTAL = 25;          

int DRIVERS = 2;      

int USERS = 5;          

int

main ()
{
sem_t fullUser;
sem_t ioReq;
sem_t mutexUser;
sem_init (&mutexUser, 0, 1);   /* initialize mutex to 1 - binary semaphore */

sem_t fullDriver1[DRIVERS];
sem_t reqPend[DRIVERS];
sem_t mutexDriver1;
sem_init (&mutexDriver1, 0, DRIVERS);

sem_t fullDriver2[DRIVERS];
sem_t reqPend2[DRIVERS];
sem_t mutexDriver2;
sem_init (&mutexDriver2, 0, DRIVERS);

sem_t fullDisk1[DRIVERS];
sem_t physicalio1[DRIVERS];
sem_t mutexDisk1;
sem_init (&mutexDisk1, 0, DRIVERS);

sem_t physicalio2[DRIVERS];
sem_t fullDisk2[DRIVERS];
sem_t mutexDisk2;
sem_init (&mutexDisk2, 0, DRIVERS);

sem_t reqService;
sem_init (&reqService, 0, USERS);

sem_t opComplete1;
sem_init (&opComplete1, 0, DRIVERS);

sem_t opComplete2;
sem_init (&opComplete2, 0, DRIVERS);


sem_t print;
sem_init (&print, 0, 1);

int idUser;
int addrUser;
int devUser;
int idIorq[DRIVERS];
int addrIorq[DRIVERS];
int pio[DRIVERS];
int countdriver = 0;
int countdisk = 0;      

void User (int id)

{
int i, addr, dev;      

for (i = 0; i <= COUNTER; i++)
{
addr = rand () % 200 + 1;
dev = rand () % 2;
p (fullUser);
p (mutexUser);
addrUser = addr;
devUser = dev;
p (print);
v (print);
   printf ("executes system call SIO or DOIO");
   v (mutexUser);      

}
}

void Doio ()
{
int j, idIorb, addrIorb, devIorb;
for (j = 0; j <= TOTAL; j++)
{
   p (ioReq);
p (mutexUser);
idIorb = idUser;
   addrIorb = addrUser;
   devIorb = devUser;
   v (mutexUser);
v (fullUser);
p (mutexDriver1[devIorb]);
idIorq[devIorb] = idIorb;
addrIorq[devIorb] = addrIorb;
v (mutexDriver1[devIorb]);
v (reqPend[devIorb]);
p (print);
printf ("DOIO assembles IORB and inserts it into IORQ for User");
   v (print);
p (ioReq);
p (mutexUser);
idIorb = idUser;
   addrIorb = addrUser;
   devIorb = devUser;  

p (fullDriver2[devIorb]);
p (mutexDriver2[devIorb]);
idIorq[devIorb] = idIorb;
addrIorq[devIorb] = addrIorb;
v (mutexDriver2[devIorb]);
v (reqPend2[devIorb]);
}
}


void Driver1 (int id)
{
int idDriver, addrDriver, k;
//Local variables
p (reqPend[id]);
//Check to see if there is a request from DOIO
while (countdriver <= 25)
{
//Loop through the requests
   if (countdriver == 25)
   {
//Check for out of bounds
   v (reqPend2[k]);
   break;
   }
   p (mutexDriver1[id]);
//Gain exclusive editting access to IORQ
   idDriver = idIorq[id];
//Copy user ID into Driver
   addrDriver = addrIorq[id];
//Copy address into Driver
   v (mutexDriver1[id]);
//Release editting control of I/O request
   v (fullDriver1[id]);
//Increment available buffer location by 1
   p (print);
//Control of printer
/*cout &lt;&lt; &quot;Driver 1 initiates I/O operation for user &quot; &lt;&lt; idDriver& lt;&lt;
endl;*/
   printf ("Driver 1 initiates I/O operation for user");
   v (print);
//Release control of printer
   p (fullDisk1[id]);
//Decrement available buffer locations
   p (mutexDisk1[id]);
//Gain access to Disk request
   pio[id] = addrDriver;
//Pass adress to shared resource
   p (print);
/*cout &lt;&lt; &quot;Driver 1 signals User &quot;& lt;&lt; idDriver &lt;&lt; &quot;that the operation
is complete.&quot; &lt;&lt; endl;*/
   printf ("operation complete");
   countdriver++;
   v (print);
   v (mutexDisk1[id]);
//Release control of Disk request
   v (physicalio1[id]);
//Inform disk request is pending
   p (opComplete1[id]);
//Waiting for disk operation to complete
   v (reqService[idDriver]);
//Inform user request is complete } if ( countdriver == 25 ) {
   v (reqPend[k]);
   break;
}               //Check for out of bounds

}

void Driver2 (int id)
{
int idDriver2, addrDriver2, i = 0;
//Local variables
p (reqPend2[id]);
//Check to see if there is a request from DOIO
while (countdriver <= 25)
{
//Loop through the requests
   if (countdriver == 25)
   {
//Check for out of bounds
   v (reqPend[i]);
   break;
   }
   p (mutexDriver2[id]);
//Gain exclusive access to I/O request
   idDriver2 = idIorq[id];
//Copy user ID into Driver
   addrDriver2 = addrIorq[id];
//Copy address into Driver
   v (mutexDriver2[id]);
//Release control of Disk Request
   v (fullDriver2[id]);
//Increment available buffer location
   p (print);
//Access printer
/*cout &lt;&lt; &quot;Driver 2 initiates I/O operation for user &quot; &lt;&lt; idDriver2& lt;&lt;
endl;*/
   v (print);
   printf ("Driver 2 initiates I/O operation for user");
//Release access to printer
   p (fullDisk2[id]);
//Decerement available buffer locations
   p (mutexDisk2[id]);
//Gain access to Disk 2 request
   pio[id] = addrDriver2;
//Pass address to a shared resource
   p (print);
/*cout &lt;&lt; &quot;Driver 2 signals User &quot;& lt;&lt; idDriver2 &lt;&lt; &quot;that the operation
is complete.&quot; &lt;&lt; endl;*/
   printf ("request served");
   countdriver++;
   v (print);
   v (mutexDisk2[id]);
//Release control of disk request
   v (physicalio2[id]);
//Inform disk 2 request is pending
   p (opComplete2[id]);
//Waiting for disk 2 to complete
   v (reqService[idDriver2]);
//Inform user the request is complete
   if (countdriver == 25)
   {
   v (reqPend[i]);
   break;
   }
}
}

void Disk1 (int id)
{
int i, seek;       //Check for out of while(countdisk &lt; 25){
p (physicalio1[id]);
//Decrement available buffer locations
p (mutexDisk1[id]);
//Gain exclusive access to I/O request
seek = pio[id];
//Grabbing the address from shared resource
v (mutexDisk1[id]);
//Releasing control of Disk 1
v (fullDisk1[id]);
//Increment available buffer location
for (i = 1; i <= seek; i++)
{
}
//Dummy loop that iterates from i to seek
p (print);
//Gain access to the printer
/*cout &lt;&lt; &quot;Disk 1 Completes I/O operation.&quot; &lt;&lt; endl;*/
v (print);
//Release acess to the printer.
countdisk++;
v (opComplete1[id]);
//Inform Driver operation is complete
}
}


void
Disk2 (int id)
{
int i, seek;           //Local variables while(countdisk &lt; 25){
//Loop through the requests
p (physicalio2[id]);
//Decrement available buffer locations
p (mutexDisk2[id]);
//Gain exclusive access to I/O request
seek = pio[id];
//Grabbing the address from shared resources
v (mutexDisk2[id]);
//Releasing control of Disk 2
v (fullDisk2[id]);
//Increment available buffer location
for (i = 1; i <= seek; i++)
{
}
//Dummy loop that iterates from i to seek
/*cout &lt;&lt; &quot;Disk 2 Completes I/O operation.&quot; &lt;&lt; endl;*/
printf ("Disk 2 Completes I/O operation") countdisk++;
v (opComplete2[id]);
//Inform Driver operation is complete
}
}


int z = 0;
initialsem (fullUser, 1);
initialsem (ioReq, 0);
initialsem (mutexUser, 1);
for (z = 0; z & lt; USERS; z++)
{
initialsem (reqService[z], 0);
}

for (z = 0; z <= DRIVERS; z++)
{
initialsem (fullDriver1[z], 1);
initialsem (reqPend[z], 0);
initialsem (mutexDriver1[z], 1);
initialsem (fullDriver2[z], 1);
initialsem (reqPend2[z], 0);
initialsem (mutexDriver1[z], 1);
initialsem (fullDisk1[z], 1);
initialsem (physicalio1[z], 0);
initialsem (mutexDisk1[z], 1);
initialsem (fullDisk2[z], 1);
initialsem (physicalio2[z], 0);
initialsem (mutexDisk2[z], 1);
initialsem (opComplete1[z], 0);
initialsem (opComplete2[z], 0);
}

initialsem (print, 1);
cobegin
{
User (0);
User (1);
User (2);
User (3);
User (4);
Doio ();
Driver1 (0);
Driver2 (0);
Disk1 (0);
Disk2 (0);
}

return 0;
}