0% found this document useful (0 votes)
83 views23 pages

OS Lab Practical File 073

The document contains 5 experiments related to operating system concepts like processes, threads, synchronization, and scheduling. Each experiment provides source code to implement a different concept: 1) fork system call 2) critical section problem 3) producer-consumer problem 4) non-preemptive scheduling algorithm 5) preemptive scheduling algorithm 6) Banker's algorithm for deadlock avoidance The experiments cover key process and thread synchronization techniques.

Uploaded by

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

OS Lab Practical File 073

The document contains 5 experiments related to operating system concepts like processes, threads, synchronization, and scheduling. Each experiment provides source code to implement a different concept: 1) fork system call 2) critical section problem 3) producer-consumer problem 4) non-preemptive scheduling algorithm 5) preemptive scheduling algorithm 6) Banker's algorithm for deadlock avoidance The experiments cover key process and thread synchronization techniques.

Uploaded by

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

SHRI RAMSWAROOP MEMORIAL

UNIVERSITY

Operating System Lab (BCS4519)


Lab Practical File

Submitted To : Submitted By:


Mr. Anand Rai MISBAH NAQVI
B.Tech (CSE) 2nd year
201810101110073
Group : 43
Experiment : 1

Objective : Write a program to implement fork system call .


Program :

#include <stdio.h> #include


<stdlib.h> #include <unistd.h>
#include <sys/types.h> int
main(void)
{
pid_t pid;
char cwdir_cld[100]; char
cwdir_prt[100]; if ((pid = fork())
< 0)
{
perror("fork error"); exit(1);
}
if (pid == 0)
{
if(chdir("/tmp") < 0)
{
perror("chdir error");
}
if (getcwd(cwdir_cld, 100) != NULL)
{
printf("Current working directory of the child is : %s", cwdir_cld);
}
else
{
perror("getcwd error");
}
}
else
{
if (getcwd(cwdir_prt, 100) != NULL)
{
printf("Current working directory of the parent is : %s", cwdir_prt);
}
else
{
perror("getcwd error");
}
}
printf(""); return(0);
}
Experiment : 2

Objective : Write a program to implement Critical Section Problem .


Program :

#include <stdio.h> #include


<pthread.h> #include <stdlib.h>

int flag[2]; int turn;


const int MAX = 1e9; int ans = 0;

void lock_init()
{
flag[0]=flag[1]=0; turn = 0;
}
void lock(int self)
{
flag[self]=1; turn = 1-self;
while(flag[1-self]==1 && turn == 1-self);
}
void unlock(int self)
{
flag[self]=0;
}
void* func(void *s)
{
int i=0;
int *limitptr = (int*) s; int self =
*limitptr;
printf("Thread %d in queue for critical section\n",self); lock(self);
printf("Thread %d in critical section\n",self);
for(i=0;i<MAX;i++)
{
ans++;
}
printf("Thread %d done counting\n",self); printf("Thread %d is
exiting critical section\n",self); unlock(self);
}
int main()
{
pthread_t p1, p2; int a=0,b=1;
lock_init();
pthread_create(&p1, NULL, func, &a);
pthread_create(&p2, NULL, func, &b);
pthread_join(p1, NULL); pthread_join(p2, NULL);
printf("Exiting Main\n");
return 0;
}
Experiment : 3

Objective : Write a program to implement classical problems of process synchronization .


Program : Producer – Consumer Problem .

#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);
}
Experiment : 4

Objective : Write a program to implement non preemptive scheduling algorithm .


Program :

#include<stdio.h> int main()


{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=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;
}
//sorting of burst times
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++)
wt[i]+=bt[j]; total+=wt[i];
}
avg_wt=(float)total/n; total=0;
printf("\nProcesst Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=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)total/n;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%fn",avg_tat);
}
Experiment : 5

Objective : Write a program to implement preemptive scheduling algorithm .


Program :

#include<stdio.h> int main()


{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat; printf("Enter Total
Number of Process:");
scanf("%d",&n);
printf("\nEnter Burst Time and Priority\n");
for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //contains process number
}
//sorting burst time, priority and process number in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i; for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos]) pos=j;
}
temp=pr[i]; pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i]; bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i]; p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process is zero
//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0; for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n; //average waiting time total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=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=total/n; //average turnaround time printf("\n\nAverage
Waiting Time=%d",avg_wt); printf("\nAverage Turnaround
Time=%d\n",avg_tat); return 0;
}
Experiment : 6

Objective : Write a program to implement Banker’s Algorithm .


Program :

#include <stdio.h>

int current[5][5], maximum_claim[5][5], available[5]; int


allocation[5] = {0, 0, 0, 0, 0};
int maxres[5], running[5], safe = 0;
int counter = 0, i, j, exec, resources, processes, k = 1;

int main()
{
printf("\nEnter number of processes: "); scanf("%d",
&processes);
for (i = 0; i < processes; i++)
{
running[i] = 1; counter++;
}
printf("\nEnter number of resources: "); scanf("%d",
&resources); printf("\nEnter Claim Vector:");
for (i = 0; i < resources; i++)
{
scanf("%d", &maxres[i]);
}
printf("\nEnter Allocated Resource Table:\n"); for (i = 0; i
< processes; i++)
{
for(j = 0; j < resources; j++) scanf("%d",
&current[i][j]);
}
printf("\nEnter Maximum Claim Table:\n"); for (i = 0; i
< processes; i++)
for(j = 0; j < resources; j++)
scanf("%d", &maximum_claim[i][j]);
printf("\nThe Claim Vector is: ");
for (i = 0; i < resources; i++)
{
printf("\t%d", maxres[i]);
}
printf("\nThe Allocated Resource Table:\n"); for (i = 0; i
< processes; i++)
{
for (j = 0; j < resources; j++)
{
printf("\t%d", current[i][j]);
}
printf("\n");
}
printf("\nThe Maximum Claim Table:\n"); for (i = 0; i
< processes; i++)
{
for (j = 0; j < resources; j++)
{
printf("\t%d", maximum_claim[i][j]);
}
printf("\n");
}
for (i = 0; i < processes; i++)
{
for (j = 0; j < resources; j++)
{
allocation[j] += current[i][j];
}
}
printf("\nAllocated resources:"); for (i = 0; i
< resources; i++)
{
printf("\t%d", allocation[i]);
}
for (i = 0; i < resources; i++)
{
available[i] = maxres[i] - allocation[i];
}
printf("\nAvailable resources:"); for (i = 0; i
< resources; i++)
{
printf("\t%d", available[i]);
}
printf("\n");
while (counter != 0)
{
safe = 0;
for (i = 0; i < processes; i++)
{
if (running[i])
{
exec = 1;
for (j = 0; j < resources; j++)
{
if (maximum_claim[i][j] - current[i][j] > available[j])
{
exec = 0; break;
}
}
if (exec)
{
printf("\nProcess%d is executing\n", i + 1); running[i] =
0;
counter--; safe = 1;
for (j = 0; j < resources; j++)
{
available[j] += current[i][j];
}
break;
}
}
}
if (!safe)
{
printf("\nThe processes are in unsafe state.\n"); break;
}
else
{
printf("\nThe process is in safe state"); printf("\nAvailable
vector:");
for (i = 0; i < resources; i++)
{
printf("\t%d", available[i]);
}
printf("\n");
}
}
return 0;
}
Experiment : 7

Objective : Write a program to implement Page Replacement Algorithm .


Program :

#include<stdio.h>

int findLRU(int time[], int n)


{
int i, minimum = time[0], pos = 0; for(i = 1; i <
n; ++i)
{
if(time[i] < minimum)
{
minimum = time[i]; pos = i;
}
}
return pos;
}
int main()
{
int no_of_frames, no_of_pages, frames[10],pages[30],counter = 0, time[10], flag1, flag2; int i, j, pos,
faults = 0;
printf("Enter number of frames: "); scanf("%d",
&no_of_frames); printf("Enter number of
pages: "); scanf("%d", &no_of_pages);
printf("Enter reference string: "); for(i = 0; i <
no_of_pages; ++i)
{
scanf("%d", &pages[i]);
}
for(i = 0; i < no_of_frames; ++i)
{
frames[i] = -1;
}
for(i = 0; i < no_of_pages; ++i)
{
flag1 = flag2 = 0;
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == pages[i])
{
counter++; time[j] = counter;
flag1 = flag2 = 1; break;
}
}
if(flag1 == 0)
{
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == -1)
{
counter++; faults++;
frames[j] = pages[i]; time[j] =
counter; flag2 = 1;
break;
}
}
}
if(flag2 == 0)
{
pos = findLRU(time, no_of_frames); counter++;
faults++;
frames[pos] = pages[i]; time[pos] =
counter;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j)
{
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults); return 0;
}
Experiment : 8

Objective : Write a program to implement Disk Scheduling Algorithm .


Program :

#include<stdio.h>
#include<math.h> int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],
temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp; temp1++;
}
else
{
queue2[temp2]=temp; temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}

for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]>queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
{
queue[i]=queue1[j];
queue[i]=max; queue[i+1]=0;
}
for(i=temp1+3,j=0;j<temp2;i++,j++)
{
queue[i]=queue2[j];
queue[0]=head;
}
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]); seek+=diff;
printf("Disk head moves from %d to %d with seek %d\n",
queue[j],queue[j+1],diff);

}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg); return 0;

}
Experiment : 9

Objective : Write a program to implement File Allocation Methods .


Program :

#include < stdio.h>


#include<conio.h> void main()
{
int f[50], i, st, len, j, c, k, count = 0; clrscr();
for(i=0;i<50;i++)
{
f[i]=0;
}
printf("Files Allocated are : \n"); x: count=0;
printf(“Enter starting block and length of files: ”); scanf("%d
%d", &st,&len); for(k=st;k<(st+len);k++)
{
if(f[k]==0) count++;
}
if(len==count)
{
for(j=st;j<(st+len);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("%d\t%d\n",j,f[j]);
}
if(j!=(st+len-1))
{ }

}
else
{

}
printf(” The file is
allocated to disk\n");
printf(” The file is not allocated \n");
printf("Do you want to enter more file(Yes - 1/No - 0)"); scanf("%d",
&c);
if(c==1)
goto x; else
exit();
}
getch();
}
Experiment : 10

Objective : Write a program for MFT and MVT first fit and best fit .
Program : First Fit .

#include<stdio.h> void main()


{
int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j; for(i = 0; i <
10; i++)
{
flags[i] = 0;
allocation[i] = -1;
}
printf("Enter no. of blocks: "); scanf("%d",
&bno);
printf("\nEnter size of each block: "); for(i = 0; i <
bno; i++)
scanf("%d", &bsize[i]); printf("\nEnter no. of
processes: "); scanf("%d", &pno);
printf("\nEnter size of each process: "); for(i = 0; i <
pno; i++)
scanf("%d", &psize[i]);
for(i = 0; i < pno; i++) //allocation as per first fit for(j = 0; j <
bno; j++)
if(flags[j] == 0 && bsize[j] >= psize[i])
{
allocation[j] = i; flags[j] = 1;
break;
}
//display allocation details
printf("\nBlock no.\tsize\t\tprocess no.\t\tsize"); for(i = 0; i <
bno; i++)
{
printf("\n%d\t\t%d\t\t", i+1, bsize[i]); if(flags[i]
== 1)
printf("%d\t\t\t%d",allocation[i]+1,psize[allocation[i]]);
else
printf("Not allocated");
}
}
Program : Best fit .

#include<stdio.h> void main()


{
int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999; static int
barray[20],parray[20];
printf("\n\t\t\tMemory Management Scheme - Best Fit");
printf("\nEnter the number of blocks:"); scanf("%d",&nb);
printf("Enter the number of processes:");
scanf("%d",&np);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block no.%d:",i);
scanf("%d",&b[i]);
}
printf("\nEnter the size of the processes :-\n");
for(i=1;i<=np;i++)
{
printf("Process no.%d:",i);
scanf("%d",&p[i]);
}
for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{
if(barray[j]!=1)
{
temp=b[j]-p[i]; if(temp>=0)
if(lowest>temp)
{
parray[i]=j; lowest=temp;
}
}
}
fragment[i]=lowest;
barray[parray[i]]=1;
lowest=10000;
}
printf("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment"); for(i=1;i<=np
&& parray[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],parray[i],b[parray[i]],fragment[i]);
}

You might also like