os lab
os lab
Sl.NO Experiments
1 Develop a c program to implement the Process system calls (fork (), exec(), wait(),
create process(), terminate process())
2 Simulate the following CPU scheduling algorithms to find turnaround time and waiting
time a) FCFS b) SJF c) Round Robin d) Priority.
3 Develop a C program to simulate producer-consumer problem using semaphores.
4 Develop a C program which demonstrates interprocess communication between a
reader
process and a writer process. Use mkfifo, open, read, write and close APIs in your
program.
#include<stdio.h>
#include <unistd.h>
#include<sys/types.h>
int main()
{
int id,childid;
id=getpid();
if((childid=fork())>0)
{
printf("\n i am in the parent process %d",id);
printf("\n i am in the parent process %d",getpid());
printf("\n i am in the parent process %d\n",getppid());
}
else
{
printf("\n i am in child process %d",id);
printf("\n i am in the child process %d",getpid());
printf("\n i am in the child process %d",getppid());
}
}
Output
$ ./a.out
b)To write a program to implement the system calls wait() and exit().
#include<stdio.h>
#include<unistd.h>
#include <stdlib.h>
int main( )
{
int i, pid;
pid=fork( );
if(pid== -1)
{
printf("fork failed");
exit(0);
}
else if(pid==0)
{
printf("\n Child process starts");
for(i=0; i<5; i++)
{
printf("\n Child process %d is called", i);
}
printf("\n Child process ends");
}
else
{
wait(0);
printf("\n Parent process ends");
}
exit(0);
}
Output
./a.out
#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
void main()
{
printf("before execl\n");
execl("/bin/ls","ls",(char*)0);
printf("after Execl\n");
}
Output
./a.out
before execl
1.c fcfs.c manual.c pg11.c ram1.c
a.out ff.c Music Pictures ram2.c
best.c fifo.c nie.c poorni ram.c
dars1.c first.c p11.c poorni.c rr.c
dars2.c get-docker.sh p12.c pr.c sjf.c
2 cpu scheduling algorithms
A)FCFS
#include<stdio.h>
int main()
{
char pn[10][10];
int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,n;
int totwt=0,tottat=0;
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Process Name, Arrival Time & Burst Time:");
scanf("%s%d%d",&pn[i],&arr[i],&bur[i]);
}
for(i=0;i<n;i++)
{
if(i==0)
{
star[i]=arr[i];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
else
{
star[i]=finish[i-1];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
}
printf("\nPName Arrtime Burtime Start TAT Finish");
for(i=0;i<n;i++)
{
printf("\n%s\t%6d\t\t%6d\t\t%6d\t\t%6d\t\t%6d",pn[i],arr[i],bur[i],star[i],tat[i],finish[i]);
totwt+=wt[i];
tottat+=tat[i];
}
printf("\nAverage Waiting time:%f", (float)totwt/n);
printf("\nAverage Turn Around Time:%f", (float)tottat/n);
}
Output
mycem@mycem-H110:~$ ./a.out
Enter the number of processes:3
Enter the Process Name, Arrival Time & Burst Time:1 2 3
Enter the Process Name, Arrival Time & Burst Time:2 5 6
Enter the Process Name, Arrival Time & Burst Time:3 6 7
b)sjf
#include<stdio.h>
#include<string.h>
main()
{
int i=0,pno[10],bt[10],n,wt[10],temp=0,j,tt[10];
float sum,at;
printf("\n Enter the no of process ");
scanf("\n %d",&n);
printf("\n Enter the burst time of each process");
for(i=0;i<n;i++)
{
printf("\n p%d",i);
scanf("%d",&bt[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(bt[i]>bt[j])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pno[i];
pno[i]=pno[j];
pno[j]=temp;
}
}
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=bt[i-1]+wt[i-1];
sum=sum+wt[i];
}
printf("\n process no \t burst time\t waiting time \t turn around time\n");
for(i=0;i<n;i++)
{
tt[i]=bt[i]+wt[i];
at+=tt[i];
printf("\n p%d\t\t%d\t\t%d\t\t%d",i,bt[i],wt[i],tt[i]);
}
printf("\n\n\t Average waiting time%f\n\t Average turn around time%f", sum/n, at/n);
}
OUTPUT
./a.out
p14
p23
p36
p0 2 0 2
p1 3 2 5
p2 4 5 9
p3 6 9 15
p[i].f=0;
}
printf("\nprocess # %-3d %-10d %-10d %-10d", i+1, time, time+j,
p[i].burst-p[i].comp);
time+=j;
}
}
}
printf("\n\n------------------");
printf("\nProcess \t Waiting Time TurnAround Time ");
for(i=0;i<n;i++)
{
printf("\nProcess # %-12d%-15d%-15d",i+1,p[i].wait,p[i].wait+p[i].burst);
totalwait=totalwait+p[i].wait;
totalturn=totalturn+p[i].wait+p[i].burst;
}
printf("\n\nAverage\n------------------ ");
printf("\nWaiting Time: %fms",totalwait/(float)n);
printf("\nTurnAround Time : %fms\n\n",totalturn/(float)n);
return 0;
}
Output
./a.out
Order Of Execution
------------------
Process Waiting Time TurnAround Time
Process # 1 10 20
Process # 2 20 35
Process # 3 25 45
Average
------------------
Waiting Time: 18.333334ms
TurnAround Time : 33.333332ms
d)priority
#include<stdio.h>
void main()
{
int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i;
printf("Enter the number of process : ");
scanf("%d",&n);
printf("\n Enter process : time priorities \n");
for(i=0;i<n;i++)
{
printf("\nProcess no %d : ",i+1);
scanf("%d %d",&pt[i],&pp[i]);
p[i]=i+1;
}
for(i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(pp[i]>pp[j])
{
x=pp[i];
pp[i]=pp[j];
pp[j]=x;
x=pt[i];
pt[i]=pt[j];
pt[j]=x;
x=p[i];
p[i]=p[j];
p[j]=x;
}
}
}
w[0]=0;
awt=0;
t[0]=pt[0];
atat=t[0];
for(i=1;i<n;i++)
{
w[i]=t[i-1];
awt+=w[i];
t[i]=w[i]+pt[i];
atat+=t[i];
}
printf("\n\n process \t Burst Time \t Wait Time \t Turn Around Time Priority \n");
for(i=0;i<n;i++)
printf("\n %d \t\t %d \t\t %d \t\t %d \t\t %d \n",p[i],pt[i],w[i],t[i],pp[i]);
awt/=n;
atat/=n;
printf("\n Average Wait Time : %d \n",awt);
printf("\n Average Turn Around Time : %d \n",atat);
}
Output
#include <stdio.h>
#include <stdlib.h>
int mutex = 1; // Initializing the mutex variable with the value 1.
int full = 0; // Initializing the full variable with the value 0.
int empty = 10, data = 0; // empty variable will store the number of empty slots in the buffer
{
--mutex; // decrementing the value of mutex
++full; // Increase the number of full slots
--empty; // decrementing the number of slots available
data++;// incrementing data which means that the data is produced
printf("\nProducer produces item number: %d\n", data);
++mutex; // incrementing the value of mutex
void consumer()// A function that will resemble the consumer's consumption of data
{
--mutex;
--full;
++empty;
printf("\nConsumer consumes item number: %d.\n", data);
data--;
++mutex;
}
int main()
{
int n, i;
printf("\n1. Enter 1 for Producer"
"\n2. Enter 2 for Consumer"
"\n3. Enter 3 to Exit");
{
case 1: if ((mutex == 1) && (empty != 0))
{
producer();
}
else
{
printf("The Buffer is full. New data cannot be produced!");
}
break;
case 2:
if ((mutex == 1) && (full != 0))
{
consumer();
}
else
{
printf("The Buffer is empty! New data cannot be consumed!");
}
break;
case 3:
exit(0);
break;
}
}
}
Output
user@user-System-Product-Name:~$ cc semaphores.c
user@user-System-Product-Name:~$ ./a.out
1. Enter 1 for Producer
2. Enter 2 for Consumer
3. Enter 3 to Exit
Enter your choice: 1
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
char arr1[80], arr2[80];
while(1)
{
fd = open(myfifo, O_WRONLY);
fgets(arr2, 80, stdin);
write(fd, arr2, strlen(arr2)+1);
close(fd);
fd = open(myfifo, O_RDONLY);
read(fd, arr1, sizeof(arr1));
output
./a.out
hi
helloUser2:
^C
mycem@mycem-H110:~$ ./a.out
User1:
vandana
P2:READ First
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd1;
char * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
char str1[80], str2[80];
while (1)
{
fd1 = open(myfifo,O_RDONLY);
read(fd1, str1, 80);
printf("User1: %s\n", str1);
close(fd1);
fd1 = open(myfifo,O_WRONLY);
fgets(str2, 80, stdin);
write(fd1, str2, strlen(str2)+1);
close(fd1);
}
return 0;
}
OUTPUT
mycem@mycem-H110:~$ cc read.c
mycem@mycem-H110:~$ ./a.out
User1: hi
User1: hi
User1: vandana
#include<stdio.h>
struct process
{
int allocation[3];
int max[3];
int need[3];
int finish;
}p[10];
int main()
{
int n,i,I,j,avail[3],work[3],flag,count=0,sequence[10],k=0;
printf("\nEnter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the %dth process allocated resources:",i);
scanf("%d%d%d",&p[i].allocation[0],&p[i].allocation[1],&p[i].allocation[2]);
printf("\nEnter the %dth process maximum resources:",i);
scanf("%d%d%d",&p[i].max[0],&p[i].max[1],&p[i].max[2]);
p[i].finish=0;
p[i].need[0]=p[i].max[0]-p[i].allocation[0];
p[i].need[1]=p[i].max[1]-p[i].allocation[1];
p[i].need[2]=p[i].max[2]-p[i].allocation[2];
}
printf("\nEnter the available vector:");
scanf("%d%d%d",&avail[0],&avail[1],&avail[2]);
for(i=0;i<3;i++)
work[i]=avail[i];
while(count!=n)
{
count=0;
for(i=0;i<n;i++)
{
flag=1;
if(p[i].finish==0)
if(p[i].need[0]<=work[0])
if(p[i].need[1]<=work[1])
if(p[i].need[2]<=work[2])
{
for(j=0;j<3;j++)
work[j]+=p[i].allocation[j];
p[i].finish=1;
sequence[k++]=i;
flag=0;
}
if(flag==1)
count++;
}
}
count=0;
for(i=0;i<n;i++)
if(p[i].finish==1)
count++;
else
printf("SYSTEM IS NOT IN A SAFE STATE \n\n");
return 0;
}
OUTPUT
user@user-System-Product-Name:~$ ./a.out
Problem
Given memory partition of 100 KB, 500 KB, 200 KB, 300 KB and 600 KB (in
order), how would each of the first-fit, best fit and worst fit algorithms place
processes of 212 KB, 417 KB, 112 KB and 426 KB (in order)? Which
algorithm makes the most efficient use of memory? Either it is first fit or best fit
or worst fit
a)First fit
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
int main()
{
int n,m,i,count=0,j,pn[100];
int p[100],size[100];
bool flag[100];
printf("ENTER THE NO PROCESS AND MEMOR :\n ");
scanf("%d%d",&n,&m);
printf("ENTER THE SIZE OF PROCESS \n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("ENTER THE SIZE OF MEMOR PARTION \n\n");
for(i=0;i<m;i++)
{
scanf("%d",&size[i]);
flag[i]=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(p[i]<=size[j]&&flag[j]==0)
{
flag[j]=true;
pn[j]=i;
count++;
j=j+m;
}
}
}
printf("NO OF PROCESS CAN ACOMADATE :%d\n\n",count);
printf("MEMOR\tPROCESS\n");
for(i=0;i<m;i++ )
{
if(flag[i]==1)
{
printf("%d <-->%d\n",size[i],p[pn[i]]);
}
else
printf("%d\tMEMOR NOT ALLOCATED\n",size[i]);
}
return 0;
}
Execution:
Input:
4 5 212 417 112 426 100 500 200 300 600
Output:
ENTER THE NO PROCESS AND MEMOR :
ENTER THE SIZE OF PROCESS
ENTER THE SIZE OF MEMOR PARTION
NO OF PROCESS CAN ACOMADATE :3
MEMOR PROCESS
100 MEMOR NOT ALLOCATED
500 <-->212
200 <-->112
300 MEMOR NOT ALLOCATED
600 <-->417
6.b To write a C Program to Implement the Memory Management Scheme
Using Best Fit.
Program:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
struct p{
int acum[100];
int jp[100];
}st;
int main()
{
int n,m,i,count=0,j,pn[100];
int p[100],size[100];
bool flag[100];
printf("ENTER THE NO PROCESS AND MEMORY :\n ");
scanf("%d%d",&n,&m);
printf("ENTER THE SIZE OF PROCESS \n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("ENTER THE SIZE OF MEMORY PARTION \n");
for(i=0;i<m;i++)
{
scanf("%d",&size[i]);
flag[i]=0;
}
for(i=0;i<n;i++)
{
int ic=0,in=0;
for(j=0;j<m;j++)
{
if(p[i]<=size[j]&&flag[j]==0)
{
int k;
st.acum[in]=size[j];
st.jp[in]=j;
in++;
ic++;
for(k=ic-1;k>0;k--)
{
if(st.acum[k]<=st.acum[k-1])
{
int temp=st.acum[k];
st.acum[k]=st.acum[k-1];
st.acum[k-1]=temp;
temp=st.jp[k];
st.jp[k]=st.jp[k-1];
st.jp[k-1]=temp;
}
}
}
}
if(ic>0)
{
j=st.jp[0];
flag[j]=true;
pn[j]=i;
count++;
}
}
printf("NO OF PROCESS CAN ACOMADATE :%d\n\n",count);
printf("MEMORY\tPROCESS\n");
for(i=0;i<m;i++ )
{
if(flag[i]==1)
{
printf("%d <-->%d\n",size[i],p[pn[i]]);
}
else
printf("%d\tMEMORY NOT ALLOCATED\n",size[i]);
}
return 0;
}
Execution
Input:
4 5 212 417 112 426 100 500 200 300 600
Output:
ENTER THE NO PROCESS AND MEMORY :
ENTER THE SIZE OF PROCESS
ENTER THE SIZE OF MEMORY PARTION
NO OF PROCESS CAN ACOMADATE :4
MEMORY PROCESS
100 MEMORY NOT ALLOCATED
500 <-->417
200 <-->112
300 <-->212
600 <-->426
Program:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
struct p{
int acum[100];
int jp[100];
}st;
int main()
{
int n,m,i,count=0,j,pn[100];
int p[100],size[100];
bool flag[100];
printf("ENTER THE NO PROCESS AND MEMORY :\n ");
scanf("%d%d",&n,&m);
printf("ENTER THE SIZE OF PROCESS \n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("ENTER THE SIZE OF MEMORY PARTION \n");
for(i=0;i<m;i++)
{
scanf("%d",&size[i]);
flag[i]=0;
int k;
st.acum[i]=size[i];
st.jp[i]=i;
for(k=i;k>0;k--)
{
if(st.acum[k]<=st.acum[k-1])
{
int temp=st.acum[k];
st.acum[k]=st.acum[k-1];
st.acum[k-1]=temp;
temp=st.jp[k];
st.jp[k]=st.jp[k-1];
st.jp[k-1]=temp;
}
}
}
int x=m-1;
for(i=0;i<n;i++)
{
if(p[i]<=st.acum[x]&&flag[st.jp[x]]==0)
{
flag[st.jp[x]]=true;
pn[st.jp[x]]=i;
x--;
count++;
}
}
printf("NO OF PROCESS CAN ACOMADATE :%d\n\n",count);
printf("MEMORY\tPROCESS\n");
for(i=0;i<m;i++ )
{
if(flag[i]==1)
{
printf("%d <-->%d\n",size[i],p[pn[i]]);
}
else
printf("%d\tMEMORY NOT ALLOCATED\n",size[i]);
}
return 0;
}
Execution:
Input:
4 5 212 417 112 426 100 500 200 300 600
Output:
ENTER THE NO PROCESS AND MEMORY :
ENTER THE SIZE OF PROCESS
ENTER THE SIZE OF MEMORY PARTION
NO OF PROCESS CAN ACOMADATE :3
MEMORY PROCESS
100 MEMORY NOT ALLOCATED
500 <-->417
7 PAGE REPLACEMENT
a)fifo
#include<stdio.h>
main()
{
int a[5],b[20],n,p=0,q=0,m=0,h,k,i,q1=1;
char f='F';
printf("Enter the Number of Pages:");
scanf("%d",&n);
printf("Enter %d Page Numbers:",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{
if(p==0)
{
if(q>=3)
q=0;
a[q]=b[i];
q++;
if(q1<3)
{