0% found this document useful (0 votes)
12 views

os lab

Uploaded by

dhanyakrish143
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

os lab

Uploaded by

dhanyakrish143
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

OPERATING SYSTEM LAB MANUAL

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.

5 Develop a C program to simulate Bankers Algorithm for DeadLock Avoidance.

6 Develop a C program to simulate the following contiguous memory allocation


Techniques: a) Worst fit b) Best fit c) First fit.
7 Develop a C program to simulate page replacement
algorithms: a) FIFO b) LRU
8 Simulate following File Organization Techniques
a) Single level directory b) Two level directory
9 Develop a C program to simulate the Linked file allocation strategies.

1.a)To write a program to implement fork() system call

#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

i am in the parent process 2290


i am in the parent process 2290
i am in the parent process 1499

i am in child process 2290


i am in the child process 2291
i am in the child process 2290

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

Child process starts


Child process 0 is called
Child process 1 is called
Child process 2 is called
Child process 3 is called
Child process 4 is called
Child process ends
Parent process ends

c)program to implement system call execl()

#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

PName Arrtime Burtime Start TAT Finish


1 2 3 2 3 5
2 5 6 5 6 11
3 6 7 11 12 18
Average Waiting time:1.666667
Average Turn Around Time:7.000000

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

Enter the no of process 4

Enter the burst time of each process


p02

p14

p23

p36

process no burst time waiting time turn around time

p0 2 0 2
p1 3 2 5
p2 4 5 9
p3 6 9 15

Average waiting time4.000000


Average turn around time7.750000
2 c)round robin
#include<stdio.h>
struct process
{
int burst,wait,comp,f;
}p[20]={0,0};
int main()
{
int n,i,j,totalwait=0,totalturn=0,quantum,flag=1,time=0;
printf("\nEnter The No Of Process :");
scanf("%d",&n);
printf("\nEnter The Quantum time (in ms) :");
scanf("%d",&quantum);
for(i=0;i<n;i++)
{
printf("Enter The Burst Time (in ms) For Process #%2d :",i+1);
scanf("%d",&p[i].burst);
p[i].f=1;
}
printf("\nOrder Of Execution \n");
printf("\nProcess Starting Ending Remaining");
printf("\n\t\tTime \tTime \t Time");
while(flag==1)
{
flag=0;
for(i=0;i<n;i++)
{
if(p[i].f==1)
{
flag=1;
j=quantum;
if((p[i].burst-p[i].comp)>quantum)
{
p[i].comp+=quantum;
}
else
{
p[i].wait=time-p[i].comp;
j=p[i].burst-p[i].comp;
p[i].comp=p[i].burst;

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

Enter The No Of Process :3

Enter The Quantum time (in ms) :5


Enter The Burst Time (in ms) For Process # 1 :10
Enter The Burst Time (in ms) For Process # 2 :15
Enter The Burst Time (in ms) For Process # 3 :20

Order Of Execution

Process Starting Ending Remaining


Time Time Time
process # 1 0 5 5
process # 2 5 10 10
process # 3 10 15 15
process # 1 15 20 0
process # 2 20 25 5
process # 3 25 30 10
process # 2 30 35 0
process # 3 35 40 5
process # 3 40 45 0

------------------
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

enter the number of process : 3


Enter process : time priorities
Process no 1 : 5 2
Process no 2 : 4 1
Process no 3 : 6 3
process Burst Time Wait Time Turn Around Time Priority
2 4 0 4 1
1 5 4 9 2
3 6 9 15 3
Average Wait Time : 4
Average Turn Around Time : 9

3.Develop a c program to stimulate producer consumer problem using


semaphores

#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

void producer()// A function that will resemble producers' production of data

{
--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");

for (i = 1; i > 0; i++)


{
printf("\nEnter your choice: ");
scanf("%d", &n);
switch (n) // using switch case as there can be multiple types of choice.

{
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

Producer produces item number: 1

Enter your choice: 1

Producer produces item number: 2

Enter your choice: 1

Producer produces item number: 3

Enter your choice: 2

Consumer consumes item number: 3.

Enter your choice: 2

Consumer consumes item number: 2.

Enter your choice: 2

Consumer consumes item number: 1.

Enter your choice: 2


The Buffer is empty! New data cannot be consumed!
Enter your choice: 3

4) Develop a c program which demonstrate interprocess communication between


a reader process and a writer process.use mkfifo,open,read,write and close APIs
in your program.

P1: write first


#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

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));

printf("User2: %s\n", arr1);


close(fd);
}
return 0;
}

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

Open two terminals and run the programs separately

5)develop a c program to stimulate bankers algorithm for deadlock avoidance

#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++;

printf("\n The safe sequence is:\t");


if(count++==n)
for(i=0;i<k;i++)
printf("%d\n",sequence[i]);

else
printf("SYSTEM IS NOT IN A SAFE STATE \n\n");
return 0;
}

OUTPUT
user@user-System-Product-Name:~$ ./a.out

Enter the number of process:3

Enter the 0th process allocated resources:1 2 3

Enter the 0th process maximum resources:4 5 6

Enter the 1th process allocated resources:3 4 5

Enter the 1th process maximum resources:6 7 8

Enter the 2th process allocated resources:1 2 3

Enter the 2th process maximum resources:3 4 5

Enter the available vector:10 12 11

The safe sequence is: 0


1
2
6)develop a c program to stimulate the following contiguous memory allocation
techniques

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

6.C To write a C Program to Implement the Memory Management Scheme


Using Worst 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;
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

200 MEMORY NOT ALLOCATED


300 <-->112
600 <-->212

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)
{

You might also like