Professional Documents
Culture Documents
Os Lab
Os Lab
algorithms.
// printing of outputs
//Driver function
int main()
{
int n,check_ar=0;
int Cmp_time=0;
float Total_WT=0,Total_TAT=0,Avg_WT,Avg_TAT;
printf("Enter the number of process \n");
scanf("%d",&n);
printf("Enter the Arrival time and Burst time of the process\n");
printf("AT BT\n");
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].AT,&a[i].BT);
a[i].id=i+1;
// here we are checking that arrival time
// of the process are same or different
if(i==0)
check_ar=a[i].AT;
if(check_ar!=a[i].AT )
check_ar=1;
}
// if process are arrived at the different time
// then sort the process on the basis of AT
if(check_ar!=0)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j].AT>a[j+1].AT)
{
swap(&a[j].id,&a[j+1].id);
swap(&a[j].AT,&a[j+1].AT);
swap(&a[j].BT,&a[j+1].BT);
}
}
}
}
}
a[i].WT=Cmp_time-a[i].AT;
Total_WT=Total_WT+a[i].WT;
// completion time of the process
Cmp_time=Cmp_time+a[i].BT;
}
a[i].WT=Cmp_time-a[i].AT;
Avg_WT=Total_WT/n;
Avg_TAT=Total_TAT/n;
//Driver function
int main()
{
int n,check_ar=0;
int Cmp_time=0;
float Total_WT=0,Total_TAT=0,Avg_WT,Avg_TAT;
printf("Enter the number of process \n");
scanf("%d",&n);
printf("Enter the Arrival time , Burst time and priority of the process\n");
printf("AT BT PR\n");
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&a[i].AT,&a[i].BT,&a[i].PR);
a[i].id=i+1;
// here we are checking that arrival time
// of the process are same or different
if(i==0)
check_ar=a[i].AT;
if(check_ar!=a[i].AT )
check_ar=1;
}
// if process are arrived at the different time
// then sort the process on the basis of AT
if(check_ar!=0)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j].AT>a[j+1].AT)
{
swap(&a[j].id,&a[j+1].id);
swap(&a[j].AT,&a[j+1].AT);
swap(&a[j].BT,&a[j+1].BT);
swap(&a[j].PR,&a[j+1].PR);
}
}
}
}
}
a[i].WT=Cmp_time-a[i].AT;
Total_WT=Total_WT+a[i].WT;
// completion time of the process
Cmp_time=Cmp_time+a[i].BT;
// Turn Around Time of the process
// compl-Arival
a[i].TAT=Cmp_time-a[i].AT;
Total_TAT=Total_TAT+a[i].TAT;
}
}
}
a[i].WT=Cmp_time-a[i].AT;
Avg_WT=Total_WT/n;
Avg_TAT=Total_TAT/n;
Avg_TAT=total_tat/n;
Avg_WT=total_wt/n;
Output:
Practical - 3
Write a C programs to simulate the following page replacement
algorithms:
a) FIFO b) LRU c) LFU
#include <stdio.h>
int main()
{
int referenceString[10], pageFaults = 0, m, n, s, pages, frames;
printf("\nEnter the number of Pages:\t");
scanf("%d", &pages);
printf("\nEnter reference string values:\n");
for( m = 0; m < pages; m++)
{
printf("Value No. [%d]:\t", m + 1);
scanf("%d", &referenceString[m]);
}
printf("\n What are the total number of frames:\t");
{
scanf("%d", &frames);
}
int temp[frames];
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(referenceString[m] == temp[n])
{
s++;
pageFaults--;
}
}
pageFaults++;
if((pageFaults <= frames) && (s == 0))
{
temp[m] = referenceString[m];
}
else if(s == 0)
{
temp[(pageFaults - 1) % frames] = referenceString[m];
}
printf("\n");
for(n = 0; n < frames; n++)
{
printf("%d\t", temp[n]);
}
}
printf("\nTotal Page Faults:\t%d\n", pageFaults);
return 0;
}
Output:
(B)Write a Program for LRU Page Replacement Algorithm
#include<stdio.h>
#include<limits.h>
return 0;
}
int main()
{
int n = sizeof(incomingStream)/sizeof(incomingStream[0]);
int frames = 3;
int queue[n];
int distance[n];
int occupied = 0;
int pagefault = 0;
printFrame(queue, occupied);
}
else{
if(queue[j] == incomingStream[k])
break;
}
printf("\n");
}
printf("Page Fault: %d",pagefault);
return 0;
}
Output:
(C)Write a Program for LFU Page Replacement Algorithm
#include<stdio.h>
int main()
{
int f,p;
int pages[50],frame[10],hit=0,count[50],time[50];
int i,j,page,flag,least,minTime,temp;
printf("Enter no of frames : ");
scanf("%d",&f);
printf("Enter no of pages : ");
scanf("%d",&p);
for(i=0;i<f;i++)
{
frame[i]=-1;
}
for(i=0;i<50;i++)
{
count[i]=0;
}
printf("Enter page no : \n");
for(i=0;i<p;i++)
{
scanf("%d",&pages[i]);
}
printf("\n");
for(i=0;i<p;i++)
{
count[pages[i]]++;
time[pages[i]]=i;
flag=1;
least=frame[0];
for(j=0;j<f;j++)
{
if(frame[j]==-1 || frame[j]==pages[i])
{
if(frame[j]!=-1)
{
hit++;
}
flag=0;
frame[j]=pages[i];
break;
}
if(count[least]>count[frame[j]])
{
least=frame[j];
}
}
if(flag)
{
minTime=50;
for(j=0;j<f;j++)
{
if(count[frame[j]]==count[least] && time[frame[j]]<minTime)
{
temp=j;
minTime=time[frame[j]];
}
}
count[frame[temp]]=0;
frame[temp]=pages[i];
}
for(j=0;j<f;j++)
{
printf("%d ",frame[j]);
}
printf("\n");
}
printf("Page hit = %d",hit);
return 0;
}
Output:
Practical - 4
Write a Program for Bankers Algorithm
//C program for Banker's Algorithm
#include <stdio.h>
int main()
{
// P0, P1, P2, P3, P4 are the names of Process
int n, r, i, j, k;
n = 5; // Indicates the Number of processes
r = 3; //Indicates the Number of resources
int alloc[5][3] = { { 0, 0, 1 }, // P0 // This is Allocation Matrix
{ 3, 0, 0 }, // P1
{ 1, 0, 1 }, // P2
{ 2, 3, 2 }, // P3
{ 0, 0, 3 } }; // P4
int flag = 0;
for (j = 0; j < r; j++) {
if (need[i][j] > avail[j]){
flag = 1;
break;
}
}
if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < r; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}
return (0);
}
Output:
Practical - 5
Write a program for Fork() system call
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
printf("Hello world!\n");
return 0;
}
Output:
Hello world!
Hello world!
Practical - 6
Write a Program for changing the file permissions.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char fileName[16] = "file.txt";
char cmd[32];
int ret = 0;
ret = system(cmd);
if (ret == 0)
printf("Permissions of file changed successfully\n");
else
printf("Unable to change the permissions of file\n");
return 0;
}
Output:
Practical - 7
Write a Program for file operations
#include<stdio.h>
int main()
{
FILE* fp;
char ch;
fp-fopen("test1.txt", "r");
if (fp == NULL)
{
printf("Unable to open file ");
system("PAUSE");
return 0;
}
do
{
ch=getc(fp);
printf("%c",ch);
}
while(ch !=EOF);
fclose(fp);
printf(" ");
system("PAUSE");
return 0;
}
Practical - 8
Write a Program for file copy and move
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
fclose(fptr1);
fclose(fptr2);
return 0;
}
Output:
Practical - 9
Write a Program for Dining Philospher’s Problem
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#define N 5
#define THINKING 2
#define HUNGRY 1
#define EATING 0
#define LEFT (phnum + 4) % N
#define RIGHT (phnum + 1) % N
int state[N];
int phil[N] = { 0, 1, 2, 3, 4 };
sem_t mutex;
sem_t S[N];
sleep(2);
// take up chopsticks
void take_fork(int phnum)
{
sem_wait(&mutex);
sem_post(&mutex);
sleep(1);
}
sem_wait(&mutex);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}
while (1) {
int* i = num;
sleep(1);
take_fork(*i);
sleep(0);
put_fork(*i);
}
}
int main()
{
int i;
pthread_t thread_id[N];
sem_init(&S[i], 0, 0);
pthread_join(thread_id[i], NULL);
}
Output:
Practical - 10
Write a Program for 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);
}
Output:
Practical - 11
(A)Write a Program for FCFS disk scheduling
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,TotalHeadMoment=0,initial;
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);
for(i=0;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
Output:
(B)Write a Program for 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);
}
TotalHeadMoment=TotalHeadMoment+min;
initial=RQ[index];
// 1000 is for max
// you can use any number
RQ[index]=1000;
count++;
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
// last movement for min size
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
initial =0;
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
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);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
for( i=0;i<index;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
for(i=n-1;i>=index;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
// last movement for min size
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
/*movement min to max disk */
TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
initial =size-1;
for(i=n-1;i>=index;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
Output:
Enter the number of blocks: 3
Enter the number of files: 2