Professional Documents
Culture Documents
LAB MANUAL
II-B.TECH II-SEM
JNTUH-R18
Faculty In-charge:
Mr.A.SATEESH, Asst.Prof.
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING
LIST OF EXPERIMENTS:
a) Paging b) Segmentation
Course Objectives:
To provide an understanding of the design aspects of operating system
concepts through
simulation
Introduce basic Unix commands, system call interface for process
management, interprocess communication and I/O in Unix
Course Outcomes:
Simulate and implement operating system concepts such as scheduling,
deadlock
management, file management and memory management.
Able to implement C programs using Unix system calls.
3. Work quietly and carefully and equally share the work with your partners.
14. Return all the equipment you have signed out for the purpose of your experiment.
Aim:
include<stdio.h>
#include<conio.h>
void main()
{
int i,j,bt# [10],n,wt[10],tt[10],w1=0,t1=0;
float aw,at;
clrscr();
printf("enter no. of processes:\n");
scanf("%d",&n);
printf("enter the burst time of processes:");
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
for(i=0;i<n;i++)
{
wt[0]=0;
tt[0]=bt[0];
wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i+1];
w1=w1+wt[i];
t1=t1+tt[i];
}
aw=w1/n;
at=t1/n;
printf("\nbt\t wt\t tt\n");
for(i=0;i<n;i++)
printf("%d\t %d\t %d\n",bt[i],wt[i],tt[i]);
printf("aw=%f\n,at=%f\n",aw,at);
getch();
}
OUTPUT:
Enter no of processes
3
enter bursttime
12
8
20
bt wt tt
12 0 12
8 12 20
20 20 40
aw=10.666670
at=24.00000
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,bt[10],t,n,wt[10],tt[10],w1=0,t1=0;
float aw,at;
clrscr();
printf("enter no. of processes:\n");
scanf("%d",&n);
printf("enter the burst time of processes:");
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
if(bt[i]>bt[j])
{
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
}
}
for(i=0;i<n;i++)
printf("%d",bt[i]);
for(i=0;i<n;i++)
{
wt[0]=0;
tt[0]=bt[0];
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 8
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING
wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i+1];
w1=w1+wt[i];
t1=t1+tt[i];
}
aw=w1/n;
at=t1/n;
printf("\nbt\t wt\t tt\n");
for(i=0;i<n;i++)
printf("%d\t %d\t %d\n",bt[i],wt[i],tt[i]);
printf("aw=%f\n,at=%f\n",aw,at);
getch();
}
OUTPUT:
Enter no of processes
3
Enter burst time
12
8
20
bt wt tt
12 8 20
8 0 8
20 20 40
aw=9.33
at=22.64
C) Round Robin:
#include<stdio.h>
#include<conio.h>
void main()
{
int st[10],bt[10],wt[10],tat[10],n,tq;
int i,count=0,swt=0,stat=0,temp,sq=0;
float awt=0.0,atat=0.0;
clrscr();
printf("Enter number of processes:");
scanf("%d",&n);
printf("Enter burst time for sequences:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
st[i]=bt[i];
}
printf("Enter time quantum:");
scanf("%d",&tq);
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}
if(st[i]>tq)
st[i]=st[i]-tq;
else
if(st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=(float)swt/n;
atat=(float)stat/n;
printf("Process_no Burst time Wait time Turn around time");
for(i=0;i<n;i++)
printf("\n %d\t %d\t %d\t %d",i+1,bt[i],wt[i],tat[i]);
printf("\n Avg wait time is %f Avg turn around time is %f",awt,atat);
getch();
}
OUTPUT:
Enter no of jobs
4
Enter burst time
5
12
0038
20
Bt wt tt
5 0 5
12 5 13
8 13 25
20 2545
aw=10.75000
at=22.000000
D) PRIORITY:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,pno[10],prior[10],bt[10],n,wt[10],tt[10],w1=0,t1=0,s;
float aw,at;
clrscr();
printf("enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("The process %d:\n",i+1);
printf("Enter the burst time of processes:");
scanf("%d",&bt[i]);
printf("Enter the priority of processes %d:",i+1);
scanf("%d",&prior[i]);
pno[i]=i+1;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(prior[i]<prior[j])
{
s=prior[i];
prior[i]=prior[j];
prior[j]=s;
s=bt[i];
bt[i]=bt[j];
bt[j]=s;
s=pno[i];
pno[i]=pno[j];
pno[j]=s;
}
}
}
for(i=0;i<n;i++)
{
wt[0]=0;
tt[0]=bt[0];
wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i+1];
w1=w1+wt[i];
t1=t1+tt[i];
aw=w1/n;
at=t1/n;
}
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 12
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING
14
printf(" \n job \t bt \t wt \t tat \t prior\n");
for(i=0;i<n;i++)
printf("%d \t %d \t %d\t %d\t %d\n",pno[i],bt[i],wt[i],tt[i],prior[i]);
printf("aw=%f \t at=%f \n",aw,at);
getch();
}
OUTPUT:
Enter no of jobs
4
Enter bursttime
10
2
4
7
Enter priority values
4
2
1
3
Bt priority wt tt
4104
2246
7 3 6 13
10 4 13 23
aw=5.750000
at=12.500000
Aim:
2. Write programs using the I/O system calls of UNIX/LINUX operating system
{
DIR *dir;
struct dirent *rddir;
printf("\n Listing the directory content\n");
dir=opendir(argv[1]);
while((rddir=readdir(dir))!=NULL)
{
printf("%s\t\n",rddir->d_name);
}
closedir(dir);
}
OUTPUT:
RP roshi.c
first.c
pk6.c
f2
abc
FILE1
fd[0]=open(“file1”, O_RDWR);
fd[1]=open(“file2”, O_RDWR);
write(fd[0], buf1, strlen(buf1));
printf(“\n Enter the text now….”);
gets(buf1);
close(fd[0]);
close(fd[1]);
printf(“\n”);
return0;
}
OUTPUT:
Enter the text now….progress
Cat file1 Just a test progress
Aim:
3. Write a C program to simulate Bankers Algorithm for Deadlock Avoidance and Prevention
{
if(completed[i] == 0)//if not completed
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}
if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}
while(count != p && process != -1);
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 17
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING
if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
getch();
}
OUTPUT :
Enter the no of processes : 5
Enter the no of resources : 3
Enter the Max Matrix for each process :
For process 1 : 7
5
3
For process 2 : 3
2
2
For process 3 : 7
0
2
For process 4 : 2
2
2
For process 5 : 4
3
3
For process 2 : 2
0
0
For process 3 : 3
0
2
For process 4 : 2
1
1
For process 5 : 0
0
2
000 000
000 000
000 000
433 002
Aim:
3) b) Write a C program to simulate Bankers Algorithm for Dead Lock Prevention
#include<stdio.h>
#include<conio.h>
void main()
{
int allocated[15][15],max[15][15],need[15][15],avail[15],tres[15],work[15],flag[15];
int pno,rno,i,j,prc,count,t,total;
count=0;
clrscr();
printf("\n Enter number of process:");
scanf("%d",&pno);
printf("\n Enter number of resources:");
scanf("%d",&rno);
for(i=1;i<= pno;i++)
{
flag[i]=0;
}
printf("\n Enter total numbers of each resources:");
for(i=1;i<= rno;i++)
scanf("%d",&tres[i]);
printf("\n Enter Max resources for each process:");
for(i=1;i<= pno;i++)
{
printf("\n for process %d:",i);
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 20
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING
for(j=1;j<= rno;j++)
scanf("%d",&max[i][j]);
}
printf("\n Enter allocated resources for each process:");
for(i=1;i<= pno;i++)
{
printf("\n for process %d:",i);
for(j=1;j<= rno;j++)
scanf("%d",&allocated[i][j]);
}
printf("\n available resources:\n");
for(j=1;j<= rno;j++)
{
avail[j]=0;
total=0;
for(i=1;i<= pno;i++)
{
total+=allocated[i][j];
}
avail[j]=tres[j]-total;
work[j]=avail[j];
printf("%d \t",work[j]);
}
do
{
for(i=1;i<= pno;i++)
{
for(j=1;j<= rno;j++)
{
need[i][j]=max[i][j]-allocated[i][j];
}
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 21
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING
}
printf("\n Allocated matrix Max need");
for(i=1;i<= pno;i++)
{
printf("\n");
for(j=1;j<= rno;j++)
{
printf("%4d",allocated[i][j]);
}
printf("|");
for(j=1;j<= rno;j++)
{
printf("%4d",max[i][j]);
}
printf("|");
for(j=1;j<= rno;j++)
{
printf("%4d",need[i][j]);
}
}
prc=0;
for(i=1;i<= pno;i++)
{
if(flag[i]==0)
{
prc=i;
for(j=1;j<= rno;j++)
{
if(work[j]< need[i][j])
{
prc=0;
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 22
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING
break;
}
}
}
if(prc!=0)
break;
}
if(prc!=0)
{
printf("\n Process %d completed",i);
count++;
printf("\n Available matrix:");
for(j=1;j<= rno;j++)
{
work[j]+=allocated[prc][j];
allocated[prc][j]=0;
max[prc][j]=0;
flag[prc]=1;
printf(" %d",work[j]);
}
}
}while(count!=pno&&prc!=0);
if(count==pno)
printf("\nThe system is in a safe state!!");
else
printf("\nThe system is in an unsafe state!!");
getch();
}
OUTPUT :
Enter number of process:5
available resources:
2 3 0
Available matrix: 10 5 5
Process 5 completed
Available matrix: 10 5 7
The system is in a safe state!!
Aim:
4. Write a C program to implement the Producer – Consumer problem using semaphores using
Break;
case 2: if(in == out)
printf(“\nBuffer is Empty”);
else
{
consume = buffer[out];
printf(“\nThe consumed value is %d”, consume);
out = (out+1)%bufsize;
}
break;
} } }
OUTPUT
1. Produce 2. Consume 3. Exit Enter your choice: 2
Buffer is Empty
1. Produce 2. Consume 3. Exit Enter your choice: 1
Enter the value: 100
1. Produce 2. Consume 3. Exit Enter your choice: 2
The consumed value is 100
1. Produce 2. Consume 3. Exit Enter your choice: 3
Aim:
5) a) Pipes
#include <stdio.h>
#include <sys/types.h>
#include<stdlib.h>
#include <unistd.h>
int main()
{
int fd[2];
if(pipe(fd)<0)
exit(1);
if(fork())
{
close(fd[0]);
write(fd[1],"Message from KITS COLLEGE",27);
}
else
{
char buf[100];
close(fd[1]);
read(fd[0],buf,100);
printf("Received by Students of KITS COLLEGE: %s\n",buf);
fflush(stdout);
}
exit(0);
return 0;
}
OUTPUT :
5) b) FIFOs
#include<fcntl.h>
#include<string.h>
int main()
{
int fd,messageLen,i;
char message[100];
/* prepare message */
sprintf(message,"Hello from PID %d", getpid());
messageLen=strlen(message)+1;
do /* keep trying to open the file until successful */
{
fd=open("aPipe",O_WRONLY); /* open named pipe for writing */
if(fd==-1)
sleep(1); /* try again in 1 second */
}while(fd==-1);
INPUT :
[srinivas@localhost ~]$ cc reader.c
[srinivas@localhost ~]$ cc writer.c -o b.out
srinivas@localhost ~]$ ./a.out & ./b.out & ./b.out &
OUTPUT :
[1] 3472 /* reader process */
[2] 3473 /* first writer process */
[3] 3474 /* second writer process */
Hello from PID 3473
Hello from PID 3474
Hello from PID 3473
Hello from PID 3474
Hello from PID 3473
Hello from PID 3474
[1] Done ./a.out /* reader exits */
[2]- Done ./b.out /* second writer exits */
[3]+ Done ./b.out /* first writer exits */
c) Message Queues
#include <stdio.h>
#include <sys/ipc.h>
#include <fcntl.h>
#include<stdlib.h>
#include<string.h>
#define MAX 255
struct mesg
{
long type;
char mtext[MAX];
} *mesg;
char buff[MAX];
main()
{
int mid,fd,n,count=0;;
if((mid=msgget(1006,IPC_CREAT | 0666))<0)
{
printf("\n Can.t create Message Q");
exit(1);
}
printf("\n Queue id:%d", mid);
mesg=(struct mesg *)malloc(sizeof(struct mesg));
mesg ->type=6;
fd=open("fact",O_RDONLY);
while(read(fd,buff,25)>0)
{
strcpy(mesg ->mtext,buff);
if(msgsnd(mid,mesg,strlen(mesg ->mtext),0)== -1)
printf("\n Message Write Error");
}
if((mid=msgget(1006,0))<0)
{
printf("\n Can.t create Message Q");
exit(1);
}
while((n=msgrcv(mid,&mesg,MAX,6,IPC_NOWAIT))>0)
write(1,mesg,n);
count++;
if((n==-1)&&(count==0))
printf("\n No Message Queue on Queue:%d", mid);
5) d) Shared Memory
#include <stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<stdlib.h>
#include<string.h>
Aim:
a) Paging b) Segmentation
6) a) Paging
#include<stdio.h>
#include<conio.h>
main()
{
int np,ps,i; int *sa; clrscr();
printf("enter how many pages\n"); scanf("%d",&np);
printf("enter the page size \n"); scanf("%d",&ps); sa=(int*)malloc(2*np); for(i=0;i<np;i++)
{
sa[i]=(int)malloc(ps);
printf("page%d\t address %u\n",i+1,sa[i]);
}
getch();
}
Input:
6) b) Segmentation :
#include<stdio.h>
#include<unistd.h>
void main()
{
int b[20],l[20],n,i,pa,s,a,d;
printf(“\nProgram for segmentation”);
printf(“\nEnter the number of segments:”);
scanf(“%d”,&n);
printf(“\nEnter the base address and limit register:”);
for(i=0;i<n;i++)
{
scanf(“%d”,&b[i]);
scanf(“%d”,&l[i])
}
printf(“\nEnter the logical address:”);
scanf(“%d”,&d);
for(i=0;i<n;i++)
{
if(i==s)
{
if(d<l[i])
{
pa=b[i]+d;
a=b[i];
printf(“(“\n\tPageNo.\t BaseAdd. PhysicalAdd. \n\t %d \t %d \t
%d \t ”,s,a,pa);
exit(0);
}
else
{
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 33
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING