FINAL OS LAB(BCS-451)
FINAL OS LAB(BCS-451)
FINAL OS LAB(BCS-451)
Research,Bareilly
OPERATING SYSTEM
LAB SOLUTION
BCS-451
Semester: 4THYear: SECOND
Session: 2023-24
Department of Computer Science and Engineering
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
Assignment-1
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
Objective: - Study of hardware and software requirements of different operating systems (UNIX,
LINUX,WINDOWS XP, WINDOWS7/8)
Content: Hardware requirements: The most common set of requirements defined by any operating system or software
application is the physical computer resources, also known as hardware, A hardware requirements list is often
accompanied by a hardware compatibility list (HCL), especially in case of operating
systems.
Hardware and Software Minimum Requirements
1.Windows 10
I. Processor: 1 gigahertz (GHz) or faster processor or SoC
II. RAM: I gigabyte (GB) for 32-bit or 2 GB for 64-bit
III. Hard disk space: 16 GB for 32-bit OS or 20 GB for 64-bit OS
IV. Graphics card: DirectX 9 or later with WDDM 1.0 driver
V. Display: 800 x 6009 with WDDM driver
2.WINDOWS XP
The minimum hardware requirements for Windows XP Home Edition are:
I. Pentium 233-megahertz (MHz) processor or faster (300 MHz is
recommended) ii At least 64 megabytes (MB) of RAM (128 MB is
recommended)
II. At least 1.5 gigabytes (GB) of available space on the hard disk
III. CD-ROM or DVD-ROM drive
IV. Keyboard and a Microsoft Mouse or some other compatible pointing device
V. Video adapter and monitor with Super VGA (800 x 600)or higher resolution
VI. Sound card
VII. Speakers or headphones
3.UNIX OS
I. RAM: 1 GB
II. Processor: IBM 604c processor with a clock speed of 375 MHz or faster
III. Free disk space: /tmp must have 1 GB free disk space. If Tivoli Identity
Manager installs
4.Linux
I. 32-bit Intel-compatible processor running at 2 GHz or greater
II. 512 MB RAM
III. Disk space: 2.5 GB for Pipeline Pilot server plus components
IV. A DVD-ROM drive
Assignment-2
Objective: - Execute various UNIX system calls for
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
i. Process management
ii. File management
iii. Input/output Systems calls
1 . Process Management – System Calls -
Process management uses certain system calls. They are explained below -
a. To create a new process – fork () is used.
b. To run a new program = exec () is used.
c. To make the process to wait = wait () is used. sleep()
d. To terminate the process – exit () is used.
e. To find the unique process id – getpid () is used.
f. To find the parent process id – getppid () is used.
g. To bias the currently running process property – nice () is used.
Process management
a. example of fork()
#include<stdio.h>
#include<unistd.h> ------> for fork() and sleep() functions
int main()
{
int i, pid;
pid=fork();
if(pid==0)
{
for(i=0; i < 20; i++)
{
sleep(2);
printf(" from Child process %d\n", i);
}
}
else
{
for(i=0; i < 20; i=i+2)
{
sleep(2);
printf(" from Parent process %d\n", i);
}
}
return 0;
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
b. example of exec()
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int i;
char *p[ ]={"./hello", NULL};
int pid;
pid=fork();
if(pid==0)
{
for(i=0; i < 10; i++)
{
sleep(2);
printf(" from Child process %d\n", i);
}
}
else
{
for(i=0; i < 10; i=i+2)
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
{
sleep(2);
printf(" from parent process %d\n", i);
execv(p[0], p);
}
}
return 0;
}
2 . File Management – System Calls -
There are four system calls for file management -
a. open (): System call is used to know the file descriptor of user-created files. Since read and write use file descriptor
as their 1st parameter so to know the file descriptor open() system call is used.
b. read (): System call is used to read the content from the file. It can also be used to read the input from the keyboard
by specifying the 0 as file descriptor.
c. write (): System call is used to write the content to the file.
d. close (): System call is used to close the opened file, it tells the operating system that you are done with the file and
close the file.
#include<unistd.h> // for read and write functions for input from keyboard
#include<fcntl.h> // for open function and O_CREAT and O_RDWR flags
#include<stdio.h>
int main()
{
int n,fd;
char buff[50];
printf("Enter text to write in the 昀椀 le:\n");
n= read(0, buff, 50);
fd=open("Amit",O_CREAT | O_RDWR, 0777);
write(fd, buff, n);
write(1, buff, n);
int close(int fd);
return 0;
}
(b) open:
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
if (fd == -1) {
perror("Program");
}
return 0;
}
(c) close
Assignment-3
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
(a) Write a program to simulate the SJF (Non-preemptive) CPU scheduling algorithm.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,p[10]={1,2,3,4,5,6,7,8,9,10},min,k=1,btime=0;
int bt[10],temp,j,at[10],wt[10],tt[10],ta=0,sum=0;
float wavg=0,tavg=0,tsum=0,wsum=0;
printf(" -------Shortest Job First Scheduling ( NP )-------\n");
printf("\nEnter the No. of processes :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\tEnter the burst time of %d process :",i+1);
scanf(" %d",&bt[i]);
printf("\tEnter the arrival time of %d process :",i+1);
scanf(" %d",&at[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(at[i]<at[j])
{
temp=p[j];
p[j]=p[i];
p[i]=temp;
temp=at[j];
at[j]=at[i];
at[i]=temp;
temp=bt[j];
bt[j]=bt[i];
bt[i]=temp;
}
}
}
for(j=0;j<n;j++)
{
btime=btime+bt[j];
min=bt[k];
for(i=k;i<n;i++)
{
if (btime>=at[i] && bt[i]<min)
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
{
temp=p[k];
p[k]=p[i];
p[i]=temp;
temp=at[k];
at[k]=at[i];
at[i]=temp;
temp=bt[k];
bt[k]=bt[i];
bt[i]=temp;
}
}
k++;
}
wt[0]=0;
for(i=1;i<n;i++)
{
sum=sum+bt[i-1];
wt[i]=sum-at[i];
wsum=wsum+wt[i];
}
wavg=(wsum/n);
for(i=0;i<n;i++)
{
ta=ta+bt[i];
tt[i]=ta-at[i];
tsum=tsum+tt[i];
}
tavg=(tsum/n);
printf("************************");
printf("\n RESULT:-");
printf("\nProcess\t Burst\t Arrival\t Waiting\t Turn-around" );
for(i=0;i<n;i++)
{
printf("\n p%d\t %d\t %d\t\t %d\t\t\t%d",p[i],bt[i],at[i],wt[i],tt[i]);
}
(b) Write a program to simulate the SJF (Preemptive) CPU scheduling algorithm.
#include <stdio.h>
#include<conio.h>
int main()
{
int arrival_time[10], burst_time[10], temp[10];
int i, smallest, count = 0, time, limit;
double wait_time = 0, turnaround_time = 0, end;
float average_waiting_time, average_turnaround_time;
printf("\nEnter the Total Number of Processes:\t");
scanf("%d", &limit);
printf("\nEnter Details of %d Processes\n", limit);
for(i = 0; i < limit; i++)
{
printf("\nEnter Arrival Time:\t");
scanf("%d", &arrival_time[i]);
printf("Enter Burst Time:\t");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
burst_time[9] = 9999;
for(time = 0; count != limit; time++)
{
smallest = 9;
for(i = 0; i < limit; i++)
{
if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] && burst_time[i] > 0)
{
smallest = i;
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
}
burst_time[smallest]--;
if(burst_time[smallest] == 0)
{
count++;
end = time + 1;
wait_time = wait_time + end - arrival_time[smallest] - temp[smallest];
turnaround_time = turnaround_time + end - arrival_time[smallest];
}
}
average_waiting_time = wait_time / limit;
average_turnaround_time = turnaround_time / limit;
printf("\n\nAverage Waiting Time:\t%lf\n", average_waiting_time);
printf("Average Turnaround Time:\t%lf\n", average_turnaround_time);
getch();
return 0;
(ii) Write a program to simulate the Priority Based CPU scheduling algorithm.
#include<stdio.h>
#include<conio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
clrscr();
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1;
}
//sorting burst time, priority and process number in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n;
total=0;
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,sum,wt,tat,twt,ttat;
int t[10];
float awt,atat;
clrscr();
printf("Enter number of processors:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the Burst Time of the process %d",i+1);
scanf("\n %d",&t[i]);
}
printf("\n\n FIRST COME FIRST SERVE SCHEDULING ALGORITHM \n");
printf("\n Process ID \t Waiting Time \t Turn Around Time \n");
printf("1 \t\t 0 \t\t %d \n",t[0]);
sum=0;
twt=0;
ttat=t[0];
for(i=1;i<n;i++)
{
sum+=t[i-1];
wt=sum;
tat=sum+t[i];
twt=twt+wt;
ttat=ttat+tat;
printf("\n %d \t\t %d \t\t %d",i+1,wt,tat);
printf("\n\n");
}
awt=(float)twt/n;
atat=(float)ttat/n;
printf("\n Average Waiting Time %4.2f",awt);
printf("\n Average Turnaround Time %4.2f",atat);
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
}
wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\nPROCESS\t\t SYSTEM/USER PROCESS \tBURST TIME\tWAITING TIME\tTURNAROUND
TIME");
for(i=0;i<n;i++)
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],su[i],bt[i],wt[i],tat[i]);
printf("\nAverage Waiting Time is --- %f",wtavg/n);
printf("\nAverage Turnaround Time is --- %f",tatavg/n);
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
Assignment-4
(i) Write a Program to simulate Sequential File Allocation method.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int st[20],b[20],b1[20],ch,i,j,n,blocks[20][20],sz[20];
char F[20][20],S[20];
clrscr();
printf("\n Enter no. of Files ::");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter file %d name ::",i+1);
scanf("%s",&F[i]);
printf("\n Enter file%d size(in kb)::",i+1);
scanf("%d",&sz[i]);
printf("\n Enter Starting block of %d::",i+1);
scanf("%d",&st[i]);
printf("\n Enter blocksize of File%d(in bytes)::",i+1);
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
b1[i]=(sz[i]*1024)/b[i];
for(i=0;i<n;i++)
{
for(j=0;j<b1[i];j++)
blocks[i][j]=st[i]+j;
}
do
{
printf("\nEnter the Filename ::");
scanf("%s",S);
for(i=0;i<n;i++)
{
if(strcmp(S,F[i])==0)
{
printf("\nFname\tStart\tNblocks\tBlocks\n");
printf("\n---------------------------------------------\n");
printf("\n%s\t%d\t%d\t",F[i],st[i],b1[i]);
for(j=0;j<b1[i];j++)
printf("%d->",blocks[i][j]);
}
}
printf("\n---------------------------------------------\n");
printf("\nDo U want to continue ::(Y:n)");
scanf("%d",&ch);
if(ch!=1)
break;
}while(1);
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
(ii) Write a C Program to implement Linked File Allocation method using Linked List.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], p,i, st, len, j, c, k, a;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks already allocated: ");
scanf("%d",&p);
printf("Enter blocks already allocated: ");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
x: printf("Enter index starting block and length: ");
scanf("%d%d", &st,&len);
k=len;
if(f[st]==0)
{
for(j=st;j<(st+k);j++)
{
if(f[j]==0)
{
f[j]=1;
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
printf("%d-------->%d\n",j,f[j]);
}
else
{
printf("%d Block is already allocated \n",j);
k++;
}
}
}
else
printf("%d starting block is already allocated \n",st);
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter starting block and size of file%d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
Assignment -5
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
clrscr();
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++) {printf("Block %d:",i);
scanf("%d",&b[i]);}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++) {printf("File %d:",i);
scanf("%d",&f[i]);}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf && ff[i]!=0;i++) //line edited on 23/09/2013
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
Assignment-6
void main()
{
int ms, mp[10], i,
temp, n = 0;
char ch = 'y';
printf("\nEnter the total memory available (in Bytes)-- ");
scanf("%d", &ms);
temp = ms;
for (i = 0; ch == 'y'; i++, n++)
{
printf("\nEnter memory required for process %d (in Bytes) -- ", i + 1);
scanf("%d", &mp[i]);
if (mp[i] <= temp)
{
printf("\nMemory is allocated for Process %d ", i + 1);
temp = temp - mp[i];
}
else
{
printf("\nMemory is Full");
break;
}
printf("\nDo you want to continue(y/n) -- ");
scanf(" %c", &ch);
}
printf("\n\nTotal Memory Available -- %d", ms);
printf("\n\n\tPROCESS\t\t MEMORY ALLOCATED ");
for (i = 0; i < n; i++)
printf("\n \t%d\t\t%d", i + 1, mp[i]);
printf("\n\nTotal Memory Allocated is %d", ms - temp);
printf("\nTotal External Fragmentation is %d", temp);
getch();
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
void main()
{
int ms, mp[10], i,
temp, n = 0;
char ch = 'y';
printf("\nEnter the total memory available (in Bytes)-- ");
scanf("%d", &ms);
temp = ms;
for (i = 0; ch == 'y'; i++, n++)
{
printf("\nEnter memory required for process %d (in Bytes) -- ", i + 1);
scanf("%d", &mp[i]);
if (mp[i] <= temp)
{
printf("\nMemory is allocated for Process %d ", i + 1);
temp = temp - mp[i];
}
else
{
printf("\nMemory is Full");
break;
}
printf("\nDo you want to continue(y/n) -- ");
scanf(" %c", &ch);
}
printf("\n\nTotal Memory Available -- %d", ms);
printf("\n\n\tPROCESS\t\t MEMORY ALLOCATED ");
for (i = 0; i < n; i++)
printf("\n \t%d\t\t%d", i + 1, mp[i]);
printf("\n\nTotal Memory Allocated is %d", ms - temp);
printf("\nTotal External Fragmentation is %d", temp);
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
Assignment-7
Implementation of compaction for the continually changing memory layout and calculate total movement of
data
#include<stdio.h>
#include<conio.h>
void create(int,int);
void del(int);
void compaction();
void display();
int fname[10],fsize[10],fstart[10],freest[10],freesize[10],m=0,n=0,start;
void main()
int name,size,ch,i;
int *ptr;
clrscr();
ptr=(int *)malloc(sizeof(int)*100);
start=freest[0]=(int)ptr;
freesize[0]=500;
printf("\n\n");
for(i=0;i<=m;i++)
printf(" %d %d\n",freest[i],freesize[i]);
printf("\n\n");
while(1)
printf("1.Create.\n");
printf("2.Delete.\n");
printf("3.Compaction.\n");
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
printf("4.Exit.\n");
scanf("%d",&ch);
switch(ch)
case 1:
scanf("%d",&name);
scanf("%d",&size);
create(name,size);
break;
case 2:
scanf("%d",&name);
del(name);
break;
case 3:
compaction();
display();
break;
case 4:
exit(1);
default:
getch();
int i,flag=1,j,a;
for(i=0;i<=m;i++)
a=i,flag=0;
if(!flag)
{for(j=0;j<n;j++);
n++;
fname[j]=name;
fsize[j]=size;
fstart[j]=freest[a];
freest[a]=freest[a]+size;
freesize[a]=freesize[a]-size;
display();
else
flag=1;
compaction();
display();
for(i=0;i<=m;i++)
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
a=i,flag=0;
if(!flag)
for(j=0;j<n;j++);
n++;
fname[j]=name;
fsize[j]=size;
fstart[j]=freest[a];
freest[a]+=size;
freesize[a]-=size;
display();
else
int i,j,k,flag=1;
for(i=0;i<n;i++)
if(fname[i]==name)
break;
if(i==n)
flag=0;
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
else
m++;
freest[m]=fstart[i];
freesize[m]=fsize[i];
for(k=i;k<n;k++)
fname[k]=fname[k+1];
fsize[k]=fsize[k+1];
fstart[k]=fstart[k+1];
n--;
if(flag)
printf("\n\n After deletion of this process the memory map will be : \n\n");
display();
void compaction()
int i,j,size1=0,f_size=0;
if(fstart[0]!=start)
fstart[0]=start; for(i=1;i<n;i++)
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
fstart[i]=fstart[i-1]+fsize[i-1];
else
for(i=1;i<n;i++)
fstart[i]=fstart[i-1]+fsize[i-1];}
f_size=freesize[0];
for(j=0;j<=m;j++)
size1+=freesize[j];
freest[0]=freest[0]-(size1-f_size);
freesize[0]=size1;
m=0;
void display()
int i;
for(i=0;i<n;i++)
printf(" %d%10d%10d\n",fname[i],fsize[i],fstart[i]);
printf("\n\n");
for(i=0;i<=m;i++)
printf(" %d %d\n",freest[i],freesize[i]);
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
Assignment-8
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
void waitexample()
int i, stat;
pid_t pid[5];
if ((pid[i] = fork()) == 0)
{
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
sleep(1);
exit(100 + i);
// of children.
if (WIFEXITED(stat))
int main()
waitexample();
return 0;
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
Assignment-9
void main()
{
int process,resource,i,j,instanc,k=0,count1=0,count2=0;
int avail[20],max[20][20],allot[20][20],need[20][20],completed[20];
clrscr();
printf("Enter No. of Process:");
scanf("%d",&process);
printf("Enter No. of Resources:");
scanf("%d",&resource);
for(i=0;i<process;i++)
completed[i]=0;
printf("Enter No. of Available Instances");
for(i=0;i<resource;i++)
{
scanf("%d",&instanc);
avail[i]=instanc;
}
printf("Enter Maximum No. of instances of resources that a Process need:\n");
for(i=0;i<process;i++)
{
printf("\t For P[%d]",i);
for(j=0;j<resource;j++)
{
scanf("%d",&instanc);
max[i][j]=instanc;
}
}
printf(" Enter no. of instances already allocated to process of a resource:\n");
for(i=0;i<process;i++)
{
printf("\t For P[%d]\t",i);
for(j=0;j<resource;j++)
{
scanf("%d",&instanc);
allot[i][j]=instanc;
need[i][j]=max[i][j]-allot[i][j];
}
}
printf("\t Safe Sequence is:- \t");
while(count1!=process)
{
count2=count1;
for(i=0;i<process;i++)
{
for(j=0;j<resource;j++)
{
if(need[i][j]<=avail[j])
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
{
k++;
}
}
if(k==resource && completed[i]==0 )
{
printf("P[%d]\t",i);
completed[i]=1;
for(j=0;j<resource;j++)
{
avail[j]=avail[j]+allot[i][j];
}
count1++;
}
k=0;
}
if(count1==count2)
{
printf("\t\t Stop ..After this.....Deadlock ");
break; } }
getch();}
Assignment-10
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
Conversion of resource allocation graph (RAG) to wait for graph (WFG) for each type of method used for
storing graph.
#include<stdio.h>
#include<conio.h>
int main()
{
int np, nr, temp, temp1;
printf("enter number of resources: ");
scanf("%d", &nr);
printf("enter number of processs: ");
scanf("%d", &np);
int rag[nr+np][nr+np];
int i, j;
for(i=0;i<np+nr;i++)
{
for(j=0; j<np+nr;j++)
{
rag[i][j]=0;
}
}
for(i=0;i<np;i++)
{
printf("enter the number of resources process %d, holding", i);
scanf("%d", &temp);
for(j=0; j<temp;j++)
{
printf("enter the ressorce number process %d holding: ", j);
scanf("%d", &temp1);
rag[np+temp1][i]=1;
}
printf("enter the number of resources process %d, requesting", i);
scanf("%d", &temp);
for(j=0; j<temp;j++)
{
printf("enter the ressorce number process %d requesting: ", i);
scanf("%d", &temp1);
rag[i][np+temp1]=1;
}
}
for(i=0;i<np+nr;i++)
{
for(j=0; j<np+nr;j++)
{
for(j=0; j<np;j++)
{
wfg[i][j]=0;
}
}
int k;
for(i=0;i<np;i++)
{
for(j=np;j<np + nr; j++)
{
if(rag[i][j] == 1)
{
for(k=0;k<np;k++)
{
if(rag[j][k] == 1)
wfg[i][k] = 1;
}
}
}
}
for(i=0;i<np;i++)
{
for(j=0; j<np;j++)
{
printf("%d ", wfg[i][j]);
}
printf("\n ");
}
return 0;}
Assignment-11
Implement the solution for Bounded Buffer (producer-consumer)problem using inter process
communication techniques-Semaphores
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
void main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}
getch();
}
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
Assignment -12
Implement the solutions for Readers-Writers problem using inter process communication technique -
Semaphore
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
sem_t wrt;
pthread_mutex_t mutex;
int cnt = 1;
int numreader = 0;
}
void *reader(void *rno)
{
pthread_mutex_lock(&mutex);
numreader++;
if(numreader == 1) {
sem_wait(&wrt);
}
pthread_mutex_unlock(&mutex);
SRMS CET R- Shri Ram Murti Smarak College Of Engineering, Technology &
Research,Bareilly
int main()
{
pthread_t read[10],write[5];
pthread_mutex_init(&mutex, NULL);
sem_init(&wrt,0,1);
pthread_mutex_destroy(&mutex);
sem_destroy(&wrt);
return 0;