My code keeps on creating a deadlock . This is written in C– using JBaci interpreter. This implements a simulation of user programs with the OS to…

My code keeps on creating a deadlock . This is written in C– using JBaci interpreter.

This implements a simulation of user programs with the OS to execute I/O operation on different devices.

Can anyone see why this deadlocks?

const int operations = 20; //total number of I/O requests

const int size = 5; // Total number of requests per user

const int users = 4;  //number of users

//buffers for user program

int bufid[operations];

int bufaddr[operations];

int bufdev[operations];

int userid;

int addr;

int dev;

int countdisk = 0;

int countdriver = 0;

//buffers for DOIO Driver 1

int iorqid1[operations];

int iorqaddr1[operations];

//buffers for DOIO Driver 2

int iorqid2[operations];

int iorqaddr2[operations];

//buffer for Driver 1

int daddr1[operations];

int did1;

//buffer for Driver 2

int daddr2[operations];

int did2;

//variable for Disk 1

int seek1 = 0;

//variable for Disk 2

int seek2 = 0;

//declarations of semaphores

semaphore full1;

semaphore full2;

semaphore full3;

semaphore full4;

semaphore full5;

binarysem mutex1;

binarysem mutex2;

binarysem mutex3;

binarysem mutex4;

binarysem mutex5;

semaphore print;

semaphore reqservice[users];

semaphore ioreq;

semaphore reqpend1;

semaphore reqpend2;

semaphore physid1;

semaphore physid2;

semaphore opcomplete1;

semaphore opcomplete2;

//functions

void User(int userid)

{

  int m = 0;

  int i;

  for(i = 0; i < size; i++)

 {

    addr = random(20)+1;

    dev = random(2)+1;

    p(full1);

    p(mutex1);

    bufid[m] = userid;

    bufaddr[m] = addr;

    bufdev[m] = dev;

    p(print);

    cout << “User ” << userid << ” executes system call SIO or DOIO.” << endl;

    v(print);

    m++;

   if(m == size){

      m = 0;

    }

    v(mutex1);

    v(ioreq);

    p(reqservice[userid]);

  }

}

void DOIO()

{

  int kid;

  int kaddr;

  int kdev;

  int i;

  int k = 0;

  int j = 0;

  for(i = 0; i < operations; i++)

  {

    //consumer

    p(ioreq);

    p(mutex1);

    kid = bufid[k];

    kaddr = bufaddr[k];

    kdev = bufdev[k];

    v(mutex1);

    v(full1);

    //producer

    if(kdev == 1)

    {

      p(full2);

      p(mutex2);

      iorqid1[k] = kid;

      iorqaddr1[k] = kaddr;

Leave a Reply