Os Lab External New

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

1.

File & process system calls


(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());
}
}
Output: Demonstration of fork system call
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;
}
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");
}
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()
{
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

2. SJF & Round Robin process scheduling


SJF
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,totalT=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
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++)
//individual WT by adding BT of all previous completed processes
wt[i]+=bt[j];

//total waiting time


total+=wt[i];
}
avg_wt=(float)total/n;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];

totalT+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)totalT/n;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f",avg_tat);
}
Output:
Enter number of process:3
Enter Burst Time:
p1:5
p2:4
p3:7
Process Burst Time Waiting Time Turnaround Time
p2 4 0 4
p1 5 4 9
p3 7 9 16

Average Waiting Time=4.333333


Average Turnaround Time=9.666667

RR
#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++)
{
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);
}

Output:
Enter Total Number of Processes:3
Enter Details of Process 1
Arrival Time: 2
Burst Time: 3
Enter Details of Process 2
Arrival Time: 1
Burst Time: 2
Enter Details of Process 3
Arrival Time: 3
Burst Time: 4
Enter Time Slot:5
Process ID Burst Time Turnaround Time Waiting Time

Process No 1 3 1 -2
Process No 2 2 4 2
Process No 3 4 6 2
Average Waiting Time:0.666667
Avg Turnaround Time:3.666667

3. Readers-Writers
#include<stdio.h>
#include<stdlib.h>
char arr[50];
void wait(int S)
{
while( S <= 0) ;
S--;
}
void signal(int S)
{
S++;
}
int readcount = 0;
int mutex =1;
int write=1;
int x=0;
void reader()
{
wait (mutex);
readcount ++;
if (readcount == 1)
{
wait(write);
}
signal(mutex);
wait(mutex);
readcount --;
if (readcount == 0)
{
signal (write);
}
signal(mutex);
printf("\n The reader reads: %s ",arr);
x--;
}
void writer()
{
wait(write);
signal(write);
x++;
printf("\n The writer writes: ");
scanf("%s",arr);
}
int main()
{
int n,i;
printf("\n1. Press 1 for writer"
"\n2. Press 2 for reader"
"\n3. Press 3 for exit");
printf("\nEnter your choice: ");
scanf("%d",&n);
while(n!=-1)
{
switch(n){
case 1:
writer();
break;
case 2:
reader();
break;
case 3:
exit(0);
break;
}
printf("\nEnter your choice: ");
scanf("%d",&n);
}
return 0;
}
output:
1. Press 1 for writer
2. Press 2 for reader
3. Press 3 for exit
Enter your choice: 1
The writer writes: hello
Enter your choice: 2
The reader reads: hello
Enter your choice: 3

4. Dining philosophers
#include<stdio.h>
enum{thinking,hungry,eating} state[50];
int self[50];
int signal(){ return eating; }
int wait(){ return hungry;}
void init(int n) {
int i;
for(i=0;i<n;i++)
state[i]=thinking;
}
void test(int i, int n){
if(state[(i+n-1)%n]!=eating && state[i]==hungry &&
state[(i+1)%n]!=eating){
state[i]=eating;
self[i]=signal();
}
}
void putdown(int i, int n){
state[i]=thinking;
printf("Philosopher %d went to thinking state\n",i+1);
test((i+n-1)%n,n);
test((i+1)%n,n);
}
void pickup(int i,int n) {
state[i]=hungry;
test(i,n);
if(state[i]!=eating){
self[i]=wait();
printf("Philosopher %d waiting i.e., hungry\n",i+1);
}
else
printf("Philosopher %d eating\n",i+1);
}
void showstats( int n ){
int i=0;
for(i=0;i<n;i++){
if(state[i]==thinking)
printf("Philosopher %d is Thinking\n",i+1);
else if(state[i]==hungry)
printf("Philosopher %d is Hungry\n",i+1);
else
printf("Philosopher %d is Eating\n",i+1);
}
}
void main(){
int i=0,n=0;
printf("Enter the number of Philosophers: ");
scanf("%d",&n);
init(n);
while(1)
{
printf("\n1.Pickup()\t2.Putdown()\t3.Showstats()\t4.Exit\n");
printf("Enter the choice : ");
scanf("%d",&i);
switch(i){
case 1: printf("Enter Philosopher number: ");
scanf("%d",&i);
pickup(i-1,n);
break;
case 2: printf("Enter Philosopher number: ");
scanf("%d",&i);
putdown(i-1,n);
break;
case 3: showstats(n);
break;
case 4:
return;
}
}
}

output:
Enter the number of Philosophers: 5
1.Pickup() 2.Putdown() 3.Showstats() 4.Exit
Enter the choice : 2
Enter Philosopher number: 1
Philosopher 1 went to thinking state

1.Pickup() 2.Putdown() 3.Showstats() 4.Exit


Enter the choice : 3
Philosopher 1 is Thinking
Philosopher 2 is Thinking
Philosopher 3 is Thinking
Philosopher 4 is Thinking
Philosopher 5 is Thinking

1.Pickup() 2.Putdown() 3.Showstats() 4.Exit


Enter the choice : 4

5.Producer-Consumer
#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)
{
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);
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);
}
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:2
Consumer consumes item 2
Enter your choice:2
Consumer consumes item 1
Enter your choice:3

6. FIFO page replacement


#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);
printf("\n");
if(count==f)
count=0;
}
printf("\n The number of Page Faults using FIFO are %d",pf);
}
Output:
Enter the length of reference string -- 5
Enter the reference string -- 1 2 3 45 3
Enter no. of frames -- 2
The Page Replacement Process is
1 -1 PF No. 1
1 2 PF No. 2
3 2 PF No. 3
3 45 PF No. 4
3 45

The number of Page Faults using FIFO are 4


7. LRU page replacement
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter the length of the string");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}

Output:
Enter the length of the string 5
5
Enter the reference string:1 2 3 4 5
Enter no of frames:3
1
1 2
1 2 3
4 2 3
4 5 3

The no of page faults is 5

8. Bankers algorithm
#include<stdio.h>
struct file
{
int all[10];
int max[10];
int need[10];
int flag;
};
void main()
{
struct file f[10];
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++)
{
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)
{
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");
}
}
Output:
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
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 010 753 743
P1 302 322 020
P2 302 902 600
P3 211 222 011
P4 002 433 431

9. FCFS disk scheduling


#include<stdio.h>
int main()
{
int t[20], n, i, j, tohm[20], tot=0;
float avhm;
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);
}
Output:
enter the no.of tracks9
enter the tracks to be traversed55 58 60 70 18 90 150 160 184
Tracks traversed Difference between tracks
0 55
55 3
58 2
60 10
70 52
18 72
90 60
150 10
160 24

Average header movements:32.000000

10. SSTF disk scheduling


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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy