[go: up one dir, main page]

0% found this document useful (0 votes)
37 views25 pages

Operating Systems Lab External

Uploaded by

tejasrigurram135
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views25 pages

Operating Systems Lab External

Uploaded by

tejasrigurram135
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Operating Systems

(a) AIM : To demonstrate usage of fork(), Systemcalls


#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
main()
{
int pid;
printf("\n Demonstration of fork system call \n");
pid=fork();
if(pid<0)
{
printf("\n not possible \n");
exit(0);
}
else
if(pid==0)
{
sleep(5);
printf("child processor ID is %d \n",getpid());
printf("parent processor ID is %d \n",getppid());
}
else
{
wait();
printf("parent id is %d",getpid());
printf("\nparent of parent processor ID is %d\n",getppid());
}
}
Compile: cc fork.c
Run: ./a.out
Output: Demonstration of fork system call

Operating Systems 1
child processor ID is 3597
parent processor ID is 3596
parent id is 3596
parent of parent processor ID is 3201

(b) AIM : To demonstrate usage of wait(),sleep() System calls


#include<stdio.h>
#include<unistd.h>
int main()
{
pid_t ret_value;
printf(“\n The process id is %d\n”,getpid());
ret_value=fork();
if(ret_value<0)
{
//fork has failed
printf(“\n Fork Failure\n”);
}
Else if(ret_value==0)
{
//child process
printf(“\n child process\n”);
printf(“the process id is %d\n”,getpid());
sleep(20);
}
else
{
//parent process
wait()
printf(“parent process\n”);
printf(“the process id is %d\n”,getpid());
sleep(30);
}
return 0;
}

Operating Systems 2
Compile: cc Wait_Sleep.c
Run: ./a.out
Output: The process id is 4163
child process
the process id is 4164
parent process
the process id is 4163

( c)AIM: Program to implement execv system calls


#include<stdio.h>
#include<unistd.h>
main()
{
char *temp[3];
temp[0]="ls";
temp[1]="-l";
temp[2]=(char *)0;
execv("/bin/ls",temp);
printf("this will not print\n");
}

Compile: cc execv.c
Run: ./a.out
Output:
Total 3
-rwxr-xr-x 1be322 group 4716 feb 26 11:30 a.out
-rw-r-r-x 1be322 group 688 feb 26 11:30 fork.c
-rw-r-r-x 1be322 group 925 feb 28 11:30 wait_sleep.c

(d)AIM: Program to implement execlp function


#include<stdio.h>
#include<sys/types.h>
main()

Operating Systems 3
{
int pid;
pid=fork();
if(pid==0)
{
printf("\n fork program started");
execlp("/bin/ls","ls",NULL);
}
else
{
printf("\nend");
}
}

Compile: cc execlp.c
Run: ./a.out
Output: fork.c
Wait_Sleep.c
execv.c

SJF:

(b) AIM:To Implement Shortest Job First(SJF) Cpu Scheduling Algorithm


#include<stdio.h>
main()
{
int WT[10],BT[10],TA[10],TTA=0,TWT=0,AWT,pn[10],ATA,n,j,temp,i;
printf("enter the value of n");
scanf("%d",&n);
printf("enter BT of n process \n");
for(i=1;i<=n;i++)
{

Operating Systems 4
scanf("%d",&BT[i]);
pn[i]=i;
}
for(i=1;i<n;i++)
for(j=i;j<=n;j++)
{
if(BT[i]>BT[j])
{
temp=BT[i];
BT[i]=BT[j];
BT[j]=temp;
temp=pn[i];

pn[i]=pn[j];
pn[j]=temp;
}
]
WT[1]=0;
TA[1]=BT[1]+WT[1];
for(i=2;i<=n;i++)
{
WT[i]=BT[i-1]+WT[i-1];
TA[i]=BT[i]+WT[i];
TWT=TWT+WT[i];
TTA=TTA+TA[i];
}
AWT=TWT/n;
ATA=TTA/n;
printf("Process no.\t Burst time\t Waiting time\t Turnaround time\n");
for(i=1;i<=n;i++)
{
printf("%d\t %d\t %d\t %d\t",i,BT[i],WT[i],TA[i]);
printf("\n");
}}
Compile: cc sjf.c
Run: ./a.out

Operating Systems 5
Output: Enter the value of n 3
Enter BT of n process
5
4
7
Process no. Burst time Waiting time Turnaround time
1 4 0 4
2 5 4 9
3 7 9 16

(d) AIM: Simulation of Round-Robin CPU scheduling algorithm


#include<stdio.h>
void main()
{
int n,i,q,x=0,count=0,temp;
int bt[10],temp_bt[10],tat[10],wt[10];
float twt=0,ttat=0;
system("clear");
printf("***** RR (ROUND ROBIN) SCHEDULING ******\n");
printf("\nEnter the number of processes : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the burst time of process %d : ",i);
scanf("%d",&bt[i]);
temp_bt[i]=bt[i];
}
printf("\nEnter the time quantum : ");
scanf("%d",&q);
while(1)
{
for(i=0;i<n;i++)
{

Operating Systems 6
temp=q;
if(temp_bt[i]==0)
{
count++;
continue;
}
if(temp_bt[i]>q)
temp_bt[i]=temp_bt[i]-q;
else if(temp_bt[i]>=0)
{
temp=temp_bt[i];
temp_bt[i]=0;
}
x=x+temp;
tat[i]=x;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
twt=twt+wt[i];
ttat=ttat+tat[i];
}
printf("\nProcess\t|Burst Time\t|Wait Time\t|Turn-Around Time");
for(i=0;i<n;i++)
{
printf("\n%d\t|%d\t\t|%d\t\t|%d",i,bt[i],wt[i],tat[i]);
}
printf("\n\nTotal waiting time is %f",twt);
printf("\nAverage waiting time is %f",twt/n);
printf("\n\nTotal turn around time is %f",ttat);
printf("\nAverage turn around time is %f\n\n",ttat/n);
}

Operating Systems 7
Compile: cc roundrobin.c
Run: ./a.out
Output: ***** RR (ROUND ROBIN) SCHEDULING ******
Enter the number of processes 3
Enter the burst time of process0:6
Enter the burst time of process1:4
Enter the burst time of process2:2
Enter the time quantum
Process Burst Time Wait Time Turn-Around Time
0 6 6 12
1 4 6 10
2 2 0 6

Total waiting time is 16.00


Average waiting time is 5.333
Total turn around time is 28.00
Average turn around time is 9.333

Readers-Writer problem:
(c)AIM: To implement Readers and Writer problem using semaphore
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
sem_t mutex,writeblock;
int data = 0,rcount = 0;
void *reader(void *arg)
{
int f;
f = ((int)arg);
sem_wait(&mutex);
rcount = rcount + 1;
if(rcount==1)
sem_wait(&writeblock);
sem_post(&mutex);

Operating Systems 8
printf("Data read by the reader%d is %d\n",f,data);
sleep(1);
sem_wait(&mutex);
rcount = rcount - 1;
if(rcount==0)
sem_post(&writeblock);
sem_post(&mutex);
}
void *writer(void *arg)
{
int f;
f = ((int) arg);
sem_wait(&writeblock);
data++;
printf("Data writen by the writer%d is %d\n",f,data);
sleep(1);
sem_post(&writeblock);
}
main()
{
int i,b;
pthread_t rtid[5],wtid[5];
sem_init(&mutex,0,1);
sem_init(&writeblock,0,1);
for(i=0;i<=2;i++)
{
pthread_create(&wtid[i],NULL,writer,(void *)i);
pthread_create(&rtid[i],NULL,reader,(void *)i);
}
for(i=0;i<=2;i++)
{
pthread_join(wtid[i],NULL);
pthread_join(rtid[i],NULL);
}
}

Operating Systems 9
Compile: cc ReadersWriter.c -lpthread
Run: ./a.out
Output: Data writen by the writer0 is 1
Data read by the reader0 is 1
Data read by the reader1 is 1
Data read by the reader2 is 1
Data writen by the writer1 is 2
Data writen by the writer2 is 3

Dining Philosopher’s Problem:


(a) AIM: To implement Dining Philosopher’s Problem using semaphore
#include<stdio.h>
#include<pthread.h>
pthread_mutex_t chopstick[5];
pthread_t philosopher[5];
void* runner(void* arg)
{
int i=(int)arg;
printf("Philosopher %d is thinking \n",i);
sleep(2);
pthread_mutex_lock(&chopstick[i]);
pthread_mutex_lock(&chopstick[(i+1)%5]);
printf("philosopher %d is eating \n",i);
sleep(3);
pthread_mutex_unlock(&chopstick[i]);
pthread_mutex_unlock(&chopstick[(i+1)%5]);
printf("Philosopher %d finished eating \n",i);
}
int main()
{
int i;
for(i=0;i<5;i++)
{
pthread_create(&philosopher[i],NULL,runner,&i);

Operating Systems 10
sleep(1);
}
for(i=0;i<5;i++)
pthread_join(philosopher[i],NULL);
}
Compile: cc Dining.c -lpthread
Run: ./a.out
Output: philosopher 0 is hungry
philosopher 1 is hungry
philosopher 1 is eating
philosopher 4 is hungry
philosopher 3 is hungry
philosopher 2 is hungry
philosopher 0 is eating
philosopher 1 is thinking
philosopher 0 is thinking
philosopher 4 is eating
philosopher 3 is eating
philosopher 4 is thinking

Producer and Consumer Problem:


(b) AIM: To implement Producer &Consumer problem using semaphore
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)

Operating Systems 11
{
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;
}
}
return 0;
}
int wait(int s)
{
return (--s);
}
int signal(int s)

{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);

Operating Systems 12
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);
}
Compile: cc ProducesConsumer.c
Run: ./a.out
Output: 1.Producer
2.Consumer
3.Exit
Enter your choice:1
Producer produces the item 1
Enter your choice:1
Producer produces the item 2
Enter your choice:1
Producer produces the item 3
Enter your choice:1
Buffer is full!!
Enter your choice:2
Consumer consumes item 3
Enter your choice:2
Consumer consumes item 2
Enter your choice:2
Consumer consumes item 1
Enter your choice:2
Buffer is empty!!
Enter your choice:3

Operating Systems 13
FIFO:
(a) AIM: To implement FIFO page replacement algorithms

#include<stdio.h>
int main()
{
int i, j, k, f, pf=0, count=0, rs[25], m[10], n;
printf("\n Enter the length of reference string -- ");
scanf("%d",&n);
printf("\n Enter the reference string -- ");
for(i=0;i<n;i++)
scanf("%d",&rs[i]);
printf("\n Enter no. of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
m[i]=-1;
printf("\n The Page Replacement Process is \n");
for(i=0;i<n;i++)
{
for(k=0;k<f;k++)
{
if(m[k]==rs[i])
break;
}
if(k==f)
{
m[count++]=rs[i];
pf++;
}
for(j=0;j<f;j++)
printf("\t%d",m[j]);
if(k==f)
printf("\tPF No. %d",pf);

Operating Systems 14
printf("\n");
if(count==f)
count=0;
}
printf("\n The number of Page Faults using FIFO are %d",pf);
}
Compile: cc FIFO.c
Run: ./a.out
Output: Enter the length of reference string -- 20
Enter the reference string -- 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Enter no. of frames -- 3
The Page Replacement Process is
7 -1 -1 PF No. 1
7 0 -1 PF No. 2
7 0 1 PF No. 3
2 0 1 PF No. 4
201
2 3 1 PF No. 5
2 3 0 PF No. 6
4 3 0 PF No. 7
4 2 0 PF No. 8
4 2 3 PF No. 9
0 2 3 PF No. 10
023
023
0 1 3 PF No. 11
0 1 2 PF No. 12
012
012
7 1 2 PF No. 13
7 0 2 PF No. 14
7 0 1 PF No. 15
The number of Page Faults using FIFO are 15

Operating Systems 15
LRU:
(b) AIM: To implement LRU page replacement algorithms
#include<stdio.h>
main()
{
int i, j , k, min, rs[25], m[10], count[10], flag[25], n, f, pf=0, next=1;
printf("Enter the length of reference string -- ");
scanf("%d",&n);
printf("Enter the reference string -- ");
for(i=0;i<n;i++)
{
scanf("%d",&rs[i]);
flag[i]=0;
}
printf("Enter the number of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i]=0; m[i]=-1;
}
printf("\nThe Page Replacement process is -- \n");
for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
if(m[j]==rs[i])
{
}
if(flag[i]==0)
{
flag[i]=1;
count[j]=next;
next++;}

Operating Systems 16
if(i<f)
{
}
else
{
m[i]=rs[i];
count[i]=next;
next++;
min=0;
}
pf++;
}
for(j=1;j<f;j++)
if(count[min] > count[j]) min=j;
m[min]=rs[i];
count[min]=next;
next++;
for(j=0;j<f;j++) printf("%d\t", m[j]);
if(flag[i]==0)
printf("PF No. -- %d" , pf);
printf("\n");
}
printf("\nThe number of page faults using LRU are %d",pf); }
Compile: cc LRU.c
Run: ./a.out
Output: Enter the length of reference string -- 20
Enter the reference string -- 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Enter the number of frames – 3
The Page Replacement process is --
7 -1 -1 PF No. -- 1
7 0 -1 PF No. -- 2
7 0 1 PF No. -- 3
2 0 1 PF No. -- 4
201

Operating Systems 17
2 0 3 PF No. -- 5
203
4 0 3 PF No. -- 6
4 0 2 PF No. -- 7
4 3 2 PF No. -- 8
0 3 2 PF No. -- 9
032
032
1 3 2 PF No. -- 10
132
1 0 2 PF No. -- 11
102
1 0 7 PF No. -- 12
107
107
The number of page faults using LRU are 12

Bankers algorithm:
AIM: To implement Bankers algorithm for the purpose of deadlock avoidance.

#include<stdio.h>
struct file
{
int all[10];
int max[10];
int need[10];
int flag;
};
void main()
{
struct file f[10];

Operating Systems 18
int fl;
int i, j, k, p, b, n, r, g, cnt=0, id, newr;
int avail[10],seq[10];
printf("Enter number of processes -- ");
scanf("%d",&n);
printf("Enter number of resources -- ");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("Enter details for P%d",i);
printf("\nEnter allocation\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].all[j]);
printf("Enter Max\t\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].max[j]);
f[i].flag=0;
}
printf("\nEnter Available Resources\t -- \t");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
printf("\nEnter New Request Details -- ");
printf("\nEnter pid \t -- \t");
scanf("%d",&id);
printf("Enter Request for Resources \t -- \t");
for(i=0;i<r;i++)
{
scanf("%d",&newr);
f[id].all[i] += newr;
avail[i]=avail[i] - newr;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{

Operating Systems 19
f[i].need[j]=f[i].max[j]-f[i].all[j];
if(f[i].need[j]<0)
f[i].need[j]=0;
}
}
cnt=0;
fl=0;
while(cnt!=n)
{
g=0;
for(j=0;j<n;j++)
{
if(f[j].flag==0)
{
b=0;
for(p=0;p<r;p++)
{
if(avail[p]>=f[j].need[p])
b=b+1;
else
b=b-1;
}
if(b==r)
{
printf("\nP%d is visited",j);
seq[fl++]=j;
f[j].flag=1;
for(k=0;k<r;k++)
avail[k]=avail[k]+f[j].all[k];
cnt=cnt+1;
printf("("); for(k=0;k<r;k++)
printf("%3d",avail[k]);
printf(")");
g=1;
} }}
if(g==0)

Operating Systems 20
{
printf("\n REQUEST NOT GRANTED -- DEADLOCK OCCURRED");
printf("\n SYSTEM IS IN UNSAFE STATE");
goto y;
}}
printf("\nSYSTEM IS IN SAFE STATE");
printf("\nThe Safe Sequence is -- (");
for(i=0;i<fl;i++)
printf("P%d ",seq[i]);
printf(")");
y: printf("\nProcess\t\tAllocation\t\tMax\t\t\tNeed\n");
for(i=0;i<n;i++)
{
printf("P%d\t",i);
for(j=0;j<r;j++)
printf("%6d",f[i].all[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].max[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].need[j]);
printf("\n");
}
}

Compile: cc Bankers.c
Run: ./a.out
Output: Enter number of processes – 5
Enter number of resources – 3
Enter details for P0
Enter allocation -- 0 1 0
Enter Max -- 7 5 3
Enter details for P1
Enter allocation -- 2 0 0
Enter Max -- 3 2 2
Enter details for P2

Operating Systems 21
Enter allocation -- 3 0 2
Enter Max -- 9 0 2
Enter details for P3
Enter allocation -- 2 1 1
Enter Max -- 2 2 2
Enter details for P4
Enter allocation -- 0 0 2
Enter Max -- 4 3 3
Enter Available Resources -- 3 3 2
Enter New Request Details –
Enter pid -- 1
Enter Request for Resources -- 1 0 2
P1 is visited( 5 3 2)
P3 is visited( 7 4 3)
P4 is visited( 7 4 5)
P0 is visited( 7 5 5)
P2 is visited( 10 5 7)
SYSTEM IS IN SAFE STATE
The Safe Sequence is -- (P1 P3 P4 P0 P2 )
Process Allocation Max Need
P0 0 1 0 7 5 3 7 4 3
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 9 1

FCFS:
#include<stdio.h>
int main()
{
int t[20], n, i, j, tohm[20], tot=0;
float avhm;

Operating Systems 22
printf("enter the no.of tracks");
scanf("%d",&n);
printf("enter the tracks to be traversed");
for(i=2;i<n+2;i++)
scanf("%d",&t[i]);
for(i=1;i<n+1;i++)
{
tohm[i]=t[i+1]-t[i];
if(tohm[i]<0)
tohm[i]=tohm[i]*(-1);
}
for(i=1;i<n+1;i++)
tot+=tohm[i];
avhm=(float)tot/n;
printf("Tracks traversed\tDifference between tracks\n");
for(i=1;i<n+1;i++)
printf("%d\t\t\t%d\n",t[i],tohm[i]);
printf("\nAverage header movements:%f",avhm);
}
Compile: cc fcfs_disk.c
Run: ./a.out
Output: Enter no.of tracks:9
Enter track position:55 58 60 70 18 90 150 160 184
OUTPUT
Tracks traversed difference between tracks
55 45
58 3
60 2
70 10
18 52
90 72
150 60
160 10
184 24
Average header movements:30.888889

Operating Systems 23
SSTF:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,TotalHeadMoment=0,initial,count=0;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
// logic for sstf disk scheduling

/* loop will execute until all process is completed*/


while(count!=n)
{
int min=1000,d,index;
for(i=0;i<n;i++)
{
d=abs(RQ[i]-initial);
if(min>d)
{
min=d;
index=i;
}
}
TotalHeadMoment=TotalHeadMoment+min;
initial=RQ[index];
// 1000 is for max
// you can use any number

Operating Systems 24
RQ[index]=1000;
count++;
}
printf("Total head movement is %d",TotalHeadMoment);
return 0;
}
OUTPUT:
Enter the number of Requests
7
82
170
43
140
24
16
190
Enter initial head position
50
Total head moment is
208

Operating Systems 25

You might also like