Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Operating System Important Questions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

1.

First come first serve:

#include <stdio.h>

int waitingtime(int proc[],int n,int burst_time[], int wait_time[])

{ wait_time[0] = 0;

for (int i = 1; i < n ; i++ ) {

wait_time[i] = burst_time[i-1] + wait_time[i-1];

return 0;

int turnaroundtime( int proc[], int n, int burst_time[], int wait_time[], int tat[])

int i;

for ( i = 0; i < n ; i++){

tat[i] = burst_time[i] + wait_time[i];

return 0;

int avgtime( int proc[], int n, int burst_time[])

{ int wait_time[n], tat[n], total_wt = 0, total_tat = 0;

int i;

waitingtime(proc, n, burst_time, wait_time);

turnaroundtime(proc, n, burst_time, wait_time, tat);

printf("Processes Burst Waiting Turn around \n");

for ( i=0; i<n; i++) {

total_wt = total_wt + wait_time[i];

total_tat = total_tat + tat[i];

printf(" %d\t %d\t\t %d \t%d\n", i+1, burst_time[i], wait_time[i], tat[i]);


}

printf("Average waiting time = %f\n", (float)total_wt / (float)n);

printf("Average turn around time = %f\n", (float)total_tat / (float)n);

return 0;

int main()

{ int proc[] = { 1, 2, 3};

int n = sizeof proc /sizeof proc[0];

int burst_time[] = {5, 8, 12};

avgtime(proc, n, burst_me);

return 0;

2.Round robin

#include<stdio.h>

int main()

{ int i, limit, total = 0, x, counter = 0, time_quantum;

int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];

float average_wait_time, average_turnaround_time;

printf("\nEnter Total Number of Processes:\t");

scanf("%d", &limit);

x = limit;

for(i = 0; i < limit; i++)

printf("\nEnter Details of Process[%d]\n", i + 1);

printf("Arrival Time:\t");

scanf("%d", &arrival_time[i]);
printf("Burst Time:\t");

scanf("%d", &burst_time[i]);

temp[i] = burst_time[i];

printf("\nEnter Time Quantum:\t");

scanf("%d", &time_quantum);

printf("\nProcess ID\t\tBurst Time\t Turnaround Time\t Waiting Time\n");

for(total = 0, i = 0; x != 0;) {

if(temp[i] <= time_quantum && temp[i] > 0) {

total = total + temp[i];

temp[i] = 0;

counter = 1;

else if(temp[i] > 0)

temp[i] = temp[i] - time_quantum;

total = total + time_quantum;

if(temp[i] == 0 && counter == 1) {

x--;

printf("\nProcess[%d]\t\t%d\t\t %d\t\t\t %d", i + 1, burst_time[i], total -

arrival_time[i], total - arrival_time[i] - burst_time[i]);

wait_time = wait_time + total - arrival_time[i] - burst_time[i];

turnaround_time = turnaround_time + total - arrival_time[i];

counter = 0;

if(i == limit - 1) {

i = 0;

}
else if(arrival_time[i + 1] <= total) {

i++;

else {

i = 0;

average_wait_time = wait_time * 1.0 / limit;

average_turnaround_time = turnaround_time * 1.0 / limit;

printf("\n\nAverage Waiting Time:\t%f", average_wait_time);

printf("\nAvg Turnaround Time:\t%f\n", average_turnaround_time);

return 0;

3.Shortest job first

#include<stdio.h>

int main()

{ int n,j,temp,temp1,temp2,pr[10],b[10],t[10],w[10],p[10], i ;

float att= 0, awt= 0 ;

for (i=0 ; i<10 ; i++) {

b[i]= 0 ;

w[i]= 0 ;

printf("enter the number of process:");

scanf("%d",&n);

printf("enter the burst times:");


for(i=0 ; i<n ; i++) {

scanf("%d", &b[i]);

p[i]=i;

for (i=0 ; i<n ; i++)

for(j=i ; j<n ; j++) {

if(b[i]>b[j])

{ temp=b[i];

temp1=p[i];

b[i]=b[j];

p[i]=p[j];

b[j]=temp;

p[j]=temp1;

w[0]=0;

for(i=0 ; i<n ; i++)

w[i+1] = w[i]+b[i];

for(i=0 ; i<n ; i++) {

t[i] = w[i]+b[i];

awt = awt+w[i];

att = att+t[i];

awt=awt/n;

att=att/n;

printf("\n\t process \t waiting time \t turn around time \n");

for(i=0 ; i<n ; i++)

printf("\t p[%d] \t %d \t\t %d \n",p[i],w[i],t[i]);


printf("the average waitingtimeis %f\n",awt);

printf("the average turn around time is %f\n",att);

return 1;

4.First in first out

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

}
5.LRU

#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 no of pages:");

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

6.Bankers algorithm

#include<stdio.h>

#include<conio.h>

int max[100][100];

int alloc[100][100];

int need[100][100];

int avail[100];

int n,r;

void input();

void show();

void cal();

int main()

int i,j;

printf("********** Banker's Algo ************\n");

input();
show();

cal();

getch();

return 0;

void input()

int i,j;

printf("Enter the no of Processes\t");

scanf("%d",&n);

printf("Enter the no of resources instances\t");

scanf("%d",&r);

printf("Enter the Max Matrix\n");

for(i=0 ; i<n ; i++)

for(j=0 ; j<r ; j++)

scanf("%d",&max[i][j]);

printf("Enter the Allocation Matrix\n");

for(i=0 ; i<n ; i++)

for(j=0 ; j<r ; j++)

scanf("%d",&alloc[i][j]);

printf("Enter the available Resources\n");

for(j=0 ; j<r ; j++)

scanf("%d",&avail[j]);

void show()

int i,j;

printf("Process\t Allocation\t Max\t Available\t");


for(i=0 ; i<n ; i++)

printf("\nP%d\t ",i+1);

for(j=0 ; j<r ; j++)

printf("%d ",alloc[i][j]);

printf("\t");

for(j=0 ; j<r ; j++)

printf("%d ",max[i][j]);

printf("\t");

if(i==0)

for(j=0;j<r;j++)

printf("%d ",avail[j]);

void cal()

int finish[100],temp,need[100][100],flag=1,k,c1=0;

int safe[100];

int i,j;

for(i=0 ; i<n ; i++)

finish[i]=0;

for(i=0; i<n; i++)

for(j=0 ; j<r ; j++)

need[i][j]=max[i][j]-alloc[i][j];

printf("\n");

while(flag)

flag=0;

for(i=0 ; i<n ; i++)


{

int c=0;

for(j=0 ; j<r ; j++)

if((finish[i]==0)&&(need[i][j]<=avail[j])) {

c++;

if(c==r)

for(k=0 ; k<r ; k++) {

avail[k]+=alloc[i][j];

finish[i]=1;

flag=1;

printf("P%d->",i);

if(finish[i]==1)

i=n;

for(i=0 ; i<n ; i++) {

if(finish[i]==1)

c1++;

else

printf("P%d->",i);

if(c1==n)

printf("\n The system is in safe state");

else

printf("\n Process are in dead lock");


printf("\n System is in unsafe state");

7.Sequential file allocation Code

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

void recurse(int files[])

{ int flag = 0, startBlock, len, j, k, ch;

printf("Enter the starting block and the length of the files: ");

scanf("%d%d", &startBlock, &len);

for (j=startBlock ; j<(startBlock+len) ; j++) {

if (files[j] == 0)

flag++;

if(len == flag) {

for ( k=startBlock ; k<(startBlock+len) ; k++) {

if (files[k] == 0) {

files[k] = 1;

printf("%d\t%d\n", k, files[k]);

if (k != (startBlock+len-1))

printf("The file is allocated to the disk\n");

else
printf("The file is not allocated to the disk\n");

printf("Do you want to enter more files?\n");

printf("Press 1 for YES, 0 for NO: ");

scanf("%d", &ch);

if (ch == 1)

recurse(files);

else

exit(0);

return;

int main()

{ int files[50];

for(int i=0 ; i<50 ; i++)

files[i]=0;

printf("Files Allocated are :\n");

recurse(files);

getch();

return 0;

}
8.Index File allocation

#include<stdlib.h>

main()

{ int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;

for(i=0;i<50;i++)

f[i]=0;

x: printf("Enter the index block: ");

scanf("%d",&ind);

if(f[ind]!=1) {

printf("Enter no of blocks needed and no of files for the index

%d on the disk : \n", ind);

scanf("%d",&n);

else

printf("%d index is already allocated \n",ind);

goto x;

y: count=0;

for(i=0 ; i<n ; i++) {

scanf("%d", &index[i]);

if(f[index[i]]==0)

count++;

if(count==n)
{

for(j=0 ; j<n ; j++)

f[index[j]]=1;

printf("Allocated\n");

printf("File Indexed\n");

for(k=0 ; k<n ; k++)

printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);

else

printf("File in the index is already allocated \n");

printf("Enter another file indexed");

goto y;

printf("Do you want to enter more file(Yes - 1/No - 0)");

scanf("%d", &c);

if(c==1)

goto x;

else

exit(0);

getch();

}
9.Linked File Allocation

#include<stdlib.h>

main()

{ int f[50], p,i, st, len, j, c, k, a;

for(i=0 ; i<50 ; i++)

f[i]=0;

printf("Enter how many blocks already allocated: ");

scanf("%d",&p);

printf("Enter blocks already allocated: ");

for(i=0 ; i<p ; i++) {

scanf("%d",&a);

f[a]=1;

x: printf("Enter index starting block and length: ");

scanf("%d%d", &st,&len);

k=len;

if(f[st]==0) {

for(j=st;j<(st+k);j++) {

if(f[j]==0)

f[j]=1;

printf("%d-------->%d\n",j,f[j]);

else {

printf("%d Block is already allocated \n",j);

k++; }

}
}

else

printf("%d starting block is already allocated \n",st);

printf("Do you want to enter more file(Yes - 1/No - 0)");

scanf("%d", &c);

if(c==1)

gotx;

else

exit(0);

getch();

10.Paging Memory Allocation Technique

#include<stdio.h>

main()

int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;

int s[10], fno[10][20];

printf("\nEnter the memory size -- ");

scanf("%d",&ms);

printf("\nEnter the page size -- ");

scanf("%d",&ps);

nop = ms/ps;

printf("\nThe no. of pages available in memory are -- %d ",nop);

printf("\nEnter number of processes -- ");


scanf("%d",&np);

rempages = nop;

for(i=1 ; i<=np ; i++)

printf("\nEnter no. of pages required for p[%d]-- ",i);

scanf("%d",&s[i]); if(s[i] >rempages)

printf("\nMemory is Full");

break;

rempages = rempages - s[i];

printf("\nEnter pagetable for p[%d] --- ",i);

for(j=0 ; j<s[i] ; j++)

scanf("%d",&fno[i][j]);

printf("\nEnter Logical Address to find Physical Address ");

printf("\nEnter process no. and pagenumber and offset -- ");

scanf("%d %d %d",&x,&y, &offset);

if(x>np || y>=s[i] || offset>=ps)

printf("\nInvalid Process or Page Number or offset");

else

pa=fno[x][y]*ps+offset;

printf("\nThe Physical Address is -- %d",pa);

getch();

You might also like