0% found this document useful (0 votes)
4 views14 pages

OS Program: File Allocation Methods

The document contains multiple C programs demonstrating various file allocation methods including First Come First Serve Scheduling, Sequential File Allocation, Indexed File Allocation, Linked File Allocation, and the Banker's Algorithm. Each program is accompanied by example outputs illustrating how they function with user inputs. Additionally, an MPI program is included to calculate the sum of randomly generated numbers across a cluster.
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)
4 views14 pages

OS Program: File Allocation Methods

The document contains multiple C programs demonstrating various file allocation methods including First Come First Serve Scheduling, Sequential File Allocation, Indexed File Allocation, Linked File Allocation, and the Banker's Algorithm. Each program is accompanied by example outputs illustrating how they function with user inputs. Additionally, an MPI program is included to calculate the sum of randomly generated numbers across a cluster.
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

1) FIRST COME FIRST SERVE SCHEDULING

#include<stdio.h>

struct process

int pid; int bt; int wt,tt;

}p[10];

int main()

int i,n

float totwt,tottt,avg1,avg2;

clrscr();

printf("enter the no of process \n");

scanf("%d",&n);

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

p[i].pid=i;

printf("enter the burst time n");

scanf("%d",&p[i].bt);

p[1].wt=0;

p[1].tt=p[1].bt+p[1].wt;

i=2;

while(i<=n)

p[i].wt=p[i-1].bt+p[i-1].wt;

p[i].tt=p[i].bt+p[i].wt;

i ++;

i=1;

p[i].wt=p[i-1].bt+p[i-1].wt;
p[i].tt=p[i].bt+p[i].wt;

totwt=tottt=0;

printf("\n processid \t bt\t wt\t tt\n");

while(i<=n)

printf("\n\t%d \t%d \t%d \t%d",p[i].pid,p[i].bt,p[i].wt,p[i].tt); totwt=p[i].wt+totwt;

tottt=p[i].tt+tottt;

i++;

avg1=totwt/n; avg2=tottt/n; printf("\navg1=%f \t avg2=%f\t",avg1,avg2);

getch();

return 0;

2) Program: Write a C Program to implement Sequential File Allocation method

#include<stdio.h>

#include<conio.h>

main()

int n,i,j,b[20],sb[20],t[20],x,c[20][20];

clrscr();

printf("Enter [Link] files:");

scanf("%d",&n);

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

printf("Enter no. of blocks occupied by file%d",i+1);

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

printf("Enter the starting block of file%d",i+1);

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

t[i]=sb[i];
for(j=0;j<b[i];j++)

c[i][j]=sb[i]++;

printf("Filename\tStart block\tlength\n");

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

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

printf("Enter file name:");

scanf("%d",&x);

printf("File name is:%d",x);

printf("length is:%d",b[x-1]);

printf("blocks occupied:");

for(i=0;i<b[x-1];i++)

printf("%4d",c[x-1][i]);

getch();

OUTPUT:
Enter [Link] files: 2

Enter no. of blocks occupied by file1 4

Enter the starting block of file1 2

Enter no. of blocks occupied by file2 10

Enter the starting block of file2 5

Filename Start block length

124

2 5 10

Enter file name: rajesh

File name is:12803 length is:0blocks occupied

3) Program : : Write a C Program to Allocation implement Indexed File method.

#include<stdio.h>

#include<conio.h>
main()

int n,m[20],i,j,sb[20],s[20],b[20][20],x;

clrscr();

printf("Enter no. of files:");

scanf("%d",&n);

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

{ printf("Enter starting block and size of file%d:",i+1);

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

printf("Enter blocks occupied by file%d:",i+1);

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

printf("enter blocks of file%d:",i+1);

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

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

} printf("\nFile\t index\tlength\n");

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

printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);

}printf("\nEnter file name:");

scanf("%d",&x);

printf("file name is:%d\n",x);

i=x-1;

printf("Index is:%d",sb[i]);

printf("Block occupied are:");

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

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

getch();

OUTPUT:
Enter no. of files:2

Enter starting block and size of file1: 2 5


Enter blocks occupied by file1:10

enter blocks of file1:3

254672647

Enter starting block and size of file2: 3 4

Enter blocks occupied by file2:5

enter blocks of file2: 2 3 4 5 6

File index length

1 2 10

235

Enter file name: venkat

file name is:12803

Index is:0Block occupied are:

4) Program: Program to implement Linked File Allocation method.

#include<stdio.h>

#include<conio.h>

struct file

char fname[10];

int start,size,block[10];

}f[10];

main()

int i,j,n;

clrscr();

printf("Enter no. of files:");

scanf("%d",&n);

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

printf("Enter file name:");

scanf("%s",&f[i].fname);
printf("Enter starting block:");

scanf("%d",&f[i].start);

f[i].block[0]=f[i].start;

printf("Enter [Link] blocks:");

scanf("%d",&f[i].size);

printf("Enter block numbers:");

for(j=1;j<=f[i].size;j++)

scanf("%d",&f[i].block[j]);

printf("File\tstart\tsize\tblock\n");

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

printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);

for(j=1;j<=f[i].size-1;j++)

printf("%d--->",f[i].block[j]);

printf("%d",f[i].block[j]);

printf("\n");

getch();

OUTPUT:
Enter no. of files:2

Enter file name:venkat

Enter starting block:20

Enter [Link] blocks:6

Enter block numbers: 4

12

15

45
32

25

Enter file name:rajesh

Enter starting block:12

Enter [Link] blocks:5

Enter block numbers:6

File start size block

venkat 20 6 4--->12--->15--->45--->32--->25

rajesh 12 5 6--->5--->4--->3--->2

5) Program:- Bankers Algorithm


#include<stdio.h>

#include<conio.h>

struct da

int max[10],a1[10],need[10],before[10],after[10];

}p[10];

void main()

int i,j,k,l,r,n,tot[10],av[10],cn=0,cz=0,temp=0,c=0;

clrscr();

printf("\n ENTER THE NO. OF PROCESSES:");

scanf("%d",&n);

printf("\n ENTER THE NO. OF RESOURCES:");

scanf("%d",&r);

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

{
printf("PROCESS %d \n",i+1);

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

printf("MAXIMUM VALUE FOR RESOURCE %d:",j+1);

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

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

printf("ALLOCATED FROM RESOURCE %d:",j+1);

scanf("%d",&p[i].a1[j]);

p[i].need[j]=p[i].max[j]-p[i].a1[j];

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

printf("ENTER TOTAL VALUE OF RESOURCE %d:",i+1);

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

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

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

temp=temp+p[j].a1[i];

av[i]=tot[i]-temp;

temp=0;

printf("\n\t RESOURCES ALLOCATED NEEDED TOTAL AVAIL");

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

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

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

printf("%d",p[i].max[j]);
printf("\t");

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

printf("%d",p[i].a1[j]);

printf("\t");

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

printf("%d",p[i].need[j]);

printf("\t");

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

if(i==0)

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

printf(" ");

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

if(i==0)

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

printf("\n\n\t AVAIL BEFORE\T AVAIL AFTER ");

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

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

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

if(p[i].need[j] >av[j])

cn++;

if(p[i].max[j]==0)

cz++;

}
if(cn==0 && cz!=r)

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

p[i].before[j]=av[j]-p[i].need[j];

p[i].after[j]=p[i].before[j]+p[i].max[j];

av[j]=p[i].after[j];

p[i].max[j]=0;

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

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

printf("%d",p[i].before[j]);

printf("\t");

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

printf("%d",p[i].after[j]);

cn=0;

cz=0;

c++;

break;

else

cn=0;cz=0;

if(c==n)

printf("\n THE ABOVE SEQUENCE IS A SAFE SEQUENCE");

else

printf("\n DEADLOCK OCCURED");

getch();
}

OUTPUT:

//TEST CASE 1:

ENTER THE NO. OF PROCESSES:4

ENTER THE NO. OF RESOURCES:3

PROCESS 1

MAXIMUM VALUE FOR RESOURCE 1:3

MAXIMUM VALUE FOR RESOURCE 2:2

MAXIMUM VALUE FOR RESOURCE 3:2

ALLOCATED FROM RESOURCE 1:1

ALLOCATED FROM RESOURCE 2:0

ALLOCATED FROM RESOURCE 3:0

PROCESS 2

MAXIMUM VALUE FOR RESOURCE 1:6

MAXIMUM VALUE FOR RESOURCE 2:1

MAXIMUM VALUE FOR RESOURCE 3:3

ALLOCATED FROM RESOURCE 1:5

ALLOCATED FROM RESOURCE 2:1

ALLOCATED FROM RESOURCE 3:1

PROCESS 3

MAXIMUM VALUE FOR RESOURCE 1:3

MAXIMUM VALUE FOR RESOURCE 2:1

MAXIMUM VALUE FOR RESOURCE 3:4

ALLOCATED FROM RESOURCE 1:2

ALLOCATED FROM RESOURCE 2:1

ALLOCATED FROM RESOURCE 3:1

PROCESS 4

MAXIMUM VALUE FOR RESOURCE 1:4

MAXIMUM VALUE FOR RESOURCE 2:2

MAXIMUM VALUE FOR RESOURCE 3:2

ALLOCATED FROM RESOURCE 1:0


ALLOCATED FROM RESOURCE 2:0

ALLOCATED FROM RESOURCE 3:2

ENTER TOTAL VALUE OF RESOURCE 1:9

ENTER TOTAL VALUE OF RESOURCE 2:3

ENTER TOTAL VALUE OF RESOURCE 3:6

RESOURCES ALLOCATED NEEDED TOTAL AVAIL

P1 322 100 222 936 112

P2 613 511 102

P3 314 211 103

P4 422 002 420

AVAIL BEFORE AVAIL AFTER

P 2 010 623

P 1 401 723

P 3 620 934

P 4 514 936

THE ABOVE SEQUENCE IS A SAFE SEQUENCE

//TEST CASE:2

ENTER THE NO. OF PROCESSES:4

ENTER THE NO. OF RESOURCES:3

PROCESS 1

MAXIMUM VALUE FOR RESOURCE 1:3

MAXIMUM VALUE FOR RESOURCE 2:2

MAXIMUM VALUE FOR RESOURCE 3:2

ALLOCATED FROM RESOURCE 1:1

ALLOCATED FROM RESOURCE 2:0

ALLOCATED FROM RESOURCE 3:1

PROCESS 2

MAXIMUM VALUE FOR RESOURCE 1:6

MAXIMUM VALUE FOR RESOURCE 2:1

MAXIMUM VALUE FOR RESOURCE 3:3


ALLOCATED FROM RESOURCE 1:5

ALLOCATED FROM RESOURCE 2:1

ALLOCATED FROM RESOURCE 3:1

PROCESS 3

MAXIMUM VALUE FOR RESOURCE 1:3

MAXIMUM VALUE FOR RESOURCE 2:1

MAXIMUM VALUE FOR RESOURCE 3:4

ALLOCATED FROM RESOURCE 1:2

ALLOCATED FROM RESOURCE 2:1

ALLOCATED FROM RESOURCE 3:2

PROCESS 4

MAXIMUM VALUE FOR RESOURCE 1:4

MAXIMUM VALUE FOR RESOURCE 2:2

MAXIMUM VALUE FOR RESOURCE 3:2

ALLOCATED FROM RESOURCE 1:0

ALLOCATED FROM RESOURCE 2:0

ALLOCATED FROM RESOURCE 3:2

ENTER TOTAL VALUE OF RESOURCE 1:9

ENTER TOTAL VALUE OF RESOURCE 2:3

ENTER TOTAL VALUE OF RESOURCE 3:6

RESOURCES ALLOCATED NEEDED TOTAL AVAIL

P1 322 101 221 936 110

P2 613 511 102

P3 314 212 102

P4 422 002 420

AVAIL BEFORE AVAIL AFTER

DEADLOCK OCCURRED

6) program: MPI program to calculate sum of randomly generated 1000 no on


cluster
#include <cstdio>
#include <cstdlib>
#include <mpi.h>
static int rank, nodes;

int main()
{
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &nodes);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Status status;

int ans = 0;
int total = 0;

int start = rank * 1000;


int end = start + 999;

for(int i = start; i <= end; i++) {


ans = ans + i;
}

if(rank != 0) {
MPI_Ssend(&ans, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
} else {
total = ans;
for(int j = 1; j < 10; j++) {
MPI_Recv(&ans, 1, MPI_INT, j, 0, MPI_COMM_WORLD, &status);
total += ans;
}
printf("Total is %d\n", total);
printf("Total Nodes is %d\n", nodes);
}

MPI_Finalize();
return 0;
}

You might also like