Previous Next
#include”stdio.h”
int main()
{
typedef int semaphore;
semaphore sread=0,swrite=0;
int ch,r=0;
clrscr();
printf("\t\t\tREADER - WRITER PROBLEM\n");
printf("\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~\n");
do
{
printf("\n\t 1. READ FROM THE FILE");
printf("\n\t 2. WRITE TO THE FILE");
printf("\n\t 3. EXIT A READER");
printf("\n\t 4. EXIT A WRITER");
printf("\n\t 5. STATUS OF THE FILE");
printf("\n\t 6. EXIT");
printf("\n\n ENTER THE CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:if(swrite==0)
{
sread=1;
r=r+1;
printf("\n Reader %d reads the File\n",r);
}
else
printf("\n Cannot Read while Writer Writes to the File\n");
break;
case 2:if(sread==0 && swrite==0)
{
swrite=1;
printf("\n Writer Write to the File\n");
}
else
if(sread==1)
printf("\n Cannot Write While Reader Reads the File\n");
else if(swrite==1)
printf("\n Already One Writer is Writing to the File\n");
break;
case 3:if(r!=0)
{
printf("\n Reader %d Closes the File\n",r);
r=r-1;
if(r==0)
{
printf("\n Currently no Readers are using the File\n");
sread=0;
}
else if(r==1)
printf("\n Currently Only One Reader is Reading the File\n");
else
printf("\n Currently %d Readers are Reading the File\n",r);
}
break;
case 4: if(swrite==1)
{
printf("\n Writer Closes the File");
swrite=0;
}
else
printf("\n No Writer Writing to the File");
break;
case 5: if(sread==1)
{
if(r==1)
printf("\n One Reader is Reading the File");
else
printf("\n%d Readers are Reading the File",r);
}
else if(swrite==1)
printf("\n One Writer is Writing the File");
else
printf("\n File is not in Use\n");
break;
case 6: break;
}
}
while(ch!=6);
return 0;
}
No comments:
Post a Comment