
















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

















































Q.1 ) Write a C Menu driven Program to implement following functionality a) Accept Available b) Display Allocation, Max c) Display the contents of need matrix d) Display Available Ans Source Code – #include<stdio.h> int A[10][10], M[10][10], N[10][10]; int AV[10], Safe[10], Finish[10], R[10][10], nor, nop; void AcceptData(intX[][10]) { int i, j; for (i = 0; i < nop; i++) { printf("P%d\n", i); for (j = 0; j < nor; j++) { printf("%c:", 65 + j); scanf("%d", &X[i][j]); } } } void AcceptAvailable() { int i; for (i = 0; i < nor; i++) { printf("%c:", 65 + i); scanf("%d", &AV[i]); } } void AcceptRequest(intR[][10]) { int i; for (i = 0; i < nor; i++) { printf("%c:", 65 + i); scanf("%d", &R[i]); } } void DisplayData() { Process Allocation Max Available A B C A B C A B C P0 2 3 2 9 7 5 3 3 2 P1 4 0 0 5 2 2 P2 5 0 4 1 0 4 P3 4 3 3 4 4 4 P4 2 2 4 6 5 5
int i, j; printf("\n\tAllocation\t\tMax\t\tNeed\n"); printf("\t"); for (i = 0; i < 3; i++) { for (j = 0; j < nor; j++) { printf("%4c", 65 + j); } printf("\t"); } for (i = 0; i < nop; i++)
Enter Number of Process : Enter Number of Resources : 3 Enter Allocation P0 A:0 B:1 C: P1 A:2 B:0 C: P2 A:3 B:0 C: P3 A:2 B:1 C: P4 A:0 B:0 C: Enter MAX Requirement P A:7 B:5 C: P1 A:3 B:2 C: P2 A:9 B:0 C: P3 A:2 B:2 C: P4 A:4 B:3 C: Enter Availability A:2 B:4 C: Allocation Max Need A B C A B C A B C P0 0 1 0 7 5 3 7 4 3 P1 2 0 0 3 2 2 1 2 2 P2 3 0 2 9 0 2 6 0 0 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 Available 2 4 6 Q.2 Write a simulation program for disk scheduling using FCFS algorithm. Accept total number of disk blocks, disk request string, and current head position from the user. Display the list of request in the order in which it is served. Also display the total number of head moments. 55, 58, 39, 18, 90, 160, 150, 38, 184 Start Head Position: 50
#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); // logic for FCFS disk scheduling for(i=0;i<n;i++) { TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i]; } printf("Total head moment is %d",TotalHeadMoment);
return 0; }
Enter the number of Request 8 Enter the Requests Sequence 95 180 34 119 11 123 62 64 Enter initial head position 50 Total head movement is 644
last=temp; } else { last->next=temp; last=temp; } } return(list); } block createalloclist() { block temp,ptr=freelist,prev; int i; f1[count].startblock=ptr->blockno; for(i=0;i<f1[count].filesize && ptr!=NULL;i++) { temp=addblock(ptr); f1[count].endblock=temp->blockno; prev=ptr; freelist=ptr->next; ptr=ptr->next; if(allocate==NULL) { allocate=temp; lasta=temp; } else { lasta->next=temp; lasta=temp; } } return(allocate); } { printf("\nEnter the file name:"); scanf("%s",&f1[count].filename); printf("\nEnter file size in blocks:"); scanf("%d",&f1[count].filesize); } void displayfile() { int i; printf("\nFile name\t\tFile size\t\tstart block\t\tEnd block"); for(i=1;i<=count;i++) { printf("\n%s",f1[i].filename); printf("\t\t\t%d",f1[i].filesize); printf("\t\t\t%d",f1[i].startblock); printf("\t\t\t%d",f1[i].endblock); } } int main() { int ch,result; char fname[20]; printf("\nEnter the size of disk in blocks");
scanf("%d",&d); freelist=create(); while(1) { printf("\n1: Allocate space for newly created file."); printf("\n2: Show used and free space on disk."); printf("\n3: Exit"); printf("\nEnter the choice"); scanf("%d",&ch); switch(ch) { case 1: acceptfile(); countfree(freelist); if(countfblock>=f1[count].filesize) { allocate=createalloclist(); displayfile(); count++; } else printf("\nNo sufficient space to allocate"); break; case 2: printf("\nFree list:"); displaylist(freelist); printf("\nAllocated list: "); displaylist(allocate); break; case 3: exit(0); } }
#include<stdio.h> #include<stdlib.h> int ind,A[10][10],M[10][10],N[10][10],Av[10],Safe[10],Finish[10],nor,nop,work[10],req[10][10]; void AcceptData(int X[][10]) { int i,j; for(i=0;i<nop;i++) { printf("P%d:\n",i); for(j=0;j<nor;j++) { printf("%c:",65+j); scanf("%d",&X[i][j]); } } } void AcceptAvailability() { int i; for(i=0;i<nor;i++) { printf("%c",65+i); scanf("%d",&Av[i]); work[i]=Av[i]; } } void DisplayData() { int i,j; printf("\n\tAllocation\t\tMax\t\tNeed\n"); printf("\t"); for(i=0;i<3;i++) { for(j=0;j<nor;j++) printf("%4c",65+j); printf("\t"); } for(i=0;i<nop;i++) { printf("\nP%d\t",i); for(j=0;j<nor;j++) printf("%4d",A[i][j]); printf("\t"); for(j=0;j<nor;j++) printf("%4d",M[i][j]); printf("\t"); for(j=0;j<nor;j++) printf("%4d",N[i][j]); } printf("\nAvailable\n”); for(j=0;j<nor;j++) printf("%4d",work[j]); } void CalcNeed() { int i,j; for(i=0;i<nop;i++) for(j=0;j<nor;j++) N[i][j]=M[i][j]-A[i][j]; } void Resource_Request(int no) { int i,f11=0,f12=0; for(i=0;i<nor;i++) { if(req[no][i]<=N[no][i]) f11=1; else
f11=0; } if(f11==0) { printf("\n Error!Process has exceeded its maximum claim"); exit(0); } if(f11==1) { for(i=0;i<nor;i++) { if(req[no][i]<=work[i]) f12=1; else f12=0; } if(f12==0) { printf("\n Process has to wait for resources"); exit(0); } } if(f11==1 && f12==1) { for(i=0;i<nor;i++) { work[i]=work[i]-req[no][i]; A[no][i]=A[no][i]+req[no][i]; N[no][i]=N[no][i]-req[no][i]; } } } int checkNeed(int pno) { int i; for(i=0;i<nor;i++) if(N[pno][i]>work[i]) return(0); return(1); } void Banker() { int i=0,j=0,k=0,flag=0; while(flag<2) { if(!Finish[i]) { printf("\nNeed%d(",i); for(j=0;j<nor;j++) printf("%d",N[i][j]); if(!checkNeed(i)) { printf("\b)>Work"); for(j=0;j<nor;j++) printf("%d",work[j]); printf("\b)"); printf("\nNeed Cannot be satisfied,consider next process"); } else { printf("b)<=Work("); for(j=0;j<nor;j++) printf("%d,",work[j]); printf("\b)"); printf("\nNeed can be satisfied,so allocate required resources"); printf("\nWork(%d)=",i); for(j=0;j<nor;j++) { work[j]+=A[i][j]; }
Q.1 Implement the Menu driven Banker's algorithm for accepting Allocation, Max from user. a) Accept Available b) Display Allocation, Max c) Find Need and display It, d) Display Available Consider the system with 3 resources types A,B, and C with 7,2,6 instances respectively. Consider the following snapshot: Process Allocation Request A B C A B C P0 0 1 0 0 0 0 P1 4 0 0 5 2 2
#include<stdio.h> int A[10][10],M[10][10],N[10][10],Av[10],nor,nop; void AcceptData(int X[][10]) { int i,j; for(i=0;i<nop;i++) { printf("P%d\n",i); for(j=0;j<nor;j++) { printf("%c: ",65+j); scanf("%d",&X[i][j]); } } } void AcceptAvailability() { int i; for(i=0;i<nor;i++) { printf("%c: ",65+i); scanf("%d",&Av[i]); } } void DisplayData() { int i,j; printf("\n\tAllocation\t\tMax\t\tNeed\n"); printf("\t"); for(i=0;i<3;i++) { for(j=0;j<nor;j++) printf("%4c",65+j); printf("\t"); } for(i=0;i<nop;i++) { printf("\nP%d\t",i); for(j=0;j<nor;j++) printf("%4d",A[i][j]); printf("\t"); for(j=0;j<nor;j++) printf("%4d",M[i][j]); printf("\t"); for(j=0;j<nor;j++) printf("%4d",N[i][j]); } printf("\nAvailable\n"); for(j=0;j<nor;j++) printf("%4d",Av[j]); } P2 5 0 4 1 0 4 P3 4 3 3 4 4 4 P4 2 2 4 6 5 5
{ 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;
for (i = temp1 + 2, j = 0; j < temp2; i++, j++) queue[i] = queue2[j]; queue[i] = 0; 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; } Slip – 5
f11=1; else f11=0; } if(f11==0) { printf("\n Error!Process has exceeded its maximum claim"); exit(0); } if(f11==1) { for(i=0;i<nor;i++) { if(req[no][i]<=work[i]) f12=1; else f12=0; } if(f12==0) { printf("\n Process has to wait for resources"); exit(0); } } if(f11==1 && f12==1) { for(i=0;i<nor;i++) { work[i]=work[i]-req[no][i]; A[no][i]=A[no][i]+req[no][i]; N[no][i]=N[no][i]-req[no][i]; } } } int checkNeed(int pno) { int i; for(i=0;i<nor;i++) if(N[pno][i]>work[i]) return(0); return(1); } void Banker() { int i=0,j=0,k=0,flag=0; while(flag<2) { if(!Finish[i]) { printf("\nNeed%d(",i); for(j=0;j<nor;j++) printf("%d",N[i][j]); if(!checkNeed(i)) { printf("\b)>Work"); for(j=0;j<nor;j++) printf("%d",work[j]); printf("\b)"); printf("\nNeed Cannot be satisfied,consider next process"); } else { printf("b)<=Work("); for(j=0;j<nor;j++) printf("%d,",work[j]); printf("\b)"); printf("\nNeed can be satisfied,so allocate required resources"); printf("\nWork(%d)=",i); for(j=0;j<nor;j++) {
work[j]+=A[i][j]; } for(j=0;j<nor;j++) printf("%4d",work[j]); printf("\nAfter P%d terminates it will release all its resources\n",i); Safe[k++]=i; Finish[i]=1; } } if((i+1)%nop==0) flag++; i=(i+1)%nop; } if(k==nop) { printf("\nSystem is in safe state..."); printf("\nSafe Sequence:"); for(i=0;i<k;i++) printf("P%d->",Safe[i]); printf("\b\b"); } else { printf("\nSystem is in not safe state..."); } } int main() { int i; printf("\nEnter no of processes & No of Resources:"); scanf("%d%d",&nop,&nor); printf("Enter Allocation\n"); AcceptData(A); printf("Enter Max Requirement\n"); AcceptData(M); printf("Enter Availability\n"); AcceptAvailability(); CalcNeed(); DisplayData(); Banker(); printf("\n Enter Process number from which request arrives:"); scanf("%d",&ind); printf("\nEnter request for process%d\n",ind); for(i=0;i<nor;i++) { printf("%c",65+i); scanf("%d",&req[ind][i]); } for(i=0;i<nop;i++) Finish[i]=0; for(i=0;i<nor;i++) work[i]=Av[i]; Resource_Request(ind); Banker(); return(0); }
#include <stdio.h> #include <stdlib.h> #include <mpi.h>