Untitled
unknown
plain_text
a year ago
9.2 kB
5
Indexable
#include<stdio.h> #include<conio.h> int main() { int clm[7][5],req[7][5],alloc[7][5],rsrc[5],avail[5],comp[7]; int first,p,r,i,j,prc,count,t; count=0; for(i=1;i<=7;i++) comp[i]=0; printf("\t BANKERS ALGORITHM IN C \n\n"); printf("Enter the no of processes : "); scanf("%d",&p); printf("\n\nEnter the no of resources : "); scanf("%d",&r); printf("\n\nEnter the claim for each process : "); for(i=1;i<=p;i++) { printf("\nFor process %d : ",i); for(j=1;j<=r;j++) { scanf("%d",&clm[i][j]); } } printf("\n\nEnter the allocation for each process : "); for(i=1;i<=p;i++) { printf("\nFor process %d : ",i); for(j=1;j<=r;j++) { scanf("%d",&alloc[i][j]); } } printf("\n\nEnter total no of each resource : "); for(j=1;j<=r;j++) scanf("%d",&rsrc[j]); for(j=1;j<=r;j++) { int total=0; avail[j]=0; for(i=1;i<=p;i++) {total+=alloc[i][j];} avail[j]=rsrc[j]-total; } do { for(i=1;i<=p;i++) { for(j=1;j<=r;j++) { req[i][j]=clm[i][j]-alloc[i][j]; } } printf("\n\nAvailable resources are : "); for(j=1;j<=r;j++) { printf("%d ",avail[j]); } printf("\nClaim matrix:\tAllocation matrix:\n"); for(i=1;i<=p;i++) { for(j=1;j<=r;j++) { printf("%d\t",clm[i][j]); } printf("\t\t\t"); for(j=1;j<=r;j++) { printf("%d\t",alloc[i][j]); } printf("\n"); } prc=0; for(i=1;i<=p;i++) { if(comp[i]==0)//if not completed { prc=i; for(j=1;j<=r;j++) { if(avail[j]==0) { prc=0; break; } } } if(prc!=0) break; } if(prc!=0) { printf("\nProcess ",prc,"runs to completion!"); count++; for(j=1;j<=r;j++) { avail[j]+=alloc[prc][j]; alloc[prc][j]=0; clm[prc][j]=0; comp[prc]=1; } } } while(count!=p&&prc!=0); if(count==p) printf("\nThe system is in a safe state!!"); else printf("\nThe system is in an unsafe state!!"); getch(); return 0; } #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> int main() { // Open a source file for reading int source_fd = open("source.txt", O_RDONLY); if (source_fd == -1) { perror("Failed to open source.txt"); exit(1); } // Create or open a destination file for writing int dest_fd = open("destination.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644); if (dest_fd == -1) { perror("Failed to open destination.txt"); close(source_fd); // Close the source file exit(1); } // Read from the source file and write to the destination file char buffer[4096]; // A buffer to hold data ssize_t nread; while ((nread = read(source_fd, buffer, sizeof(buffer))) > 0) { if (write(dest_fd, buffer, nread) != nread) { perror("Write error"); break; } } // Check if there was an error during reading if (nread < 0) { perror("Read error"); } // Close both files close(source_fd); close(dest_fd); return 0; } #include<stdio.h> int main() { int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,totalT=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; //finding the waiting time of all the processes for(i=1;i<n;i++) { wt[i]=0; for(j=0;j<i;j++) //individual WT by adding BT of all previous completed processes wt[i]+=bt[j]; //total waiting time total+=wt[i]; } //average waiting time avg_wt=(float)total/n; printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time"); for(i=0;i<n;i++) { //turnaround time of individual processes tat[i]=bt[i]+wt[i]; //total turnaround time totalT+=tat[i]; printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]); } //average turnaround time avg_tat=(float)totalT/n; printf("\n\nAverage Waiting Time=%f",avg_wt); printf("\nAverage Turnaround Time=%f",avg_tat); } #include<stdio.h> int main() { //Input no of processed int n; printf("Enter Total Number of Processes:"); scanf("%d", &n); int wait_time = 0, ta_time = 0, arr_time[n], burst_time[n], temp_burst_time[n]; int x = n; //Input details of processes for(int i = 0; i < n; i++) { printf("Enter Details of Process %d \n", i + 1); printf("Arrival Time: "); scanf("%d", &arr_time[i]); printf("Burst Time: "); scanf("%d", &burst_time[i]); temp_burst_time[i] = burst_time[i]; } //Input time slot int time_slot; printf("Enter Time Slot:"); scanf("%d", &time_slot); //Total indicates total time //counter indicates which process is executed int total = 0, counter = 0,i; printf("Process ID Burst Time Turnaround Time Waiting Time\n"); for(total=0, i = 0; x!=0; ) { // define the conditions if(temp_burst_time[i] <= time_slot && temp_burst_time[i] > 0) { total = total + temp_burst_time[i]; temp_burst_time[i] = 0; counter=1; } else if(temp_burst_time[i] > 0) { temp_burst_time[i] = temp_burst_time[i] - time_slot; total += time_slot; } if(temp_burst_time[i]==0 && counter==1) { x--; //decrement the process no. printf("\nProcess No %d \t\t %d\t\t\t\t %d\t\t\t %d", i+1, burst_time[i], total-arr_time[i], total-arr_time[i]-burst_time[i]); wait_time = wait_time+total-arr_time[i]-burst_time[i]; ta_time += total -arr_time[i]; counter =0; } if(i==n-1) { i=0; } else if(arr_time[i+1]<=total) { i++; } else { i=0; } } float average_wait_time = wait_time * 1.0 / n; float average_turnaround_time = ta_time * 1.0 / n; printf("\nAverage Waiting Time:%f", average_wait_time); printf("\nAvg Turnaround Time:%f", average_turnaround_time); return 0; } #include <stdio.h> //Function to swap two variables void swap(int *a,int *b) { int temp=*a; *a=*b; *b=temp; } int main() { int n; printf("Enter Number of Processes: "); scanf("%d",&n); // b is array for burst time, p for priority and index for process id int b[n],p[n],index[n]; for(int i=0;i<n;i++) { printf("Enter Burst Time and Priority Value for Process %d: ",i+1); scanf("%d %d",&b[i],&p[i]); index[i]=i+1; } for(int i=0;i<n;i++) { int a=p[i],m=i; //Finding out highest priority element and placing it at its desired position for(int j=i;j<n;j++) { if(p[j] > a) { a=p[j]; m=j; } } //Swapping processes swap(&p[i], &p[m]); swap(&b[i], &b[m]); swap(&index[i],&index[m]); } // T stores the starting time of process int t=0; //Printing scheduled process printf("Order of process Execution is\n"); for(int i=0;i<n;i++) { printf("P%d is executed from %d to %d\n",index[i],t,t+b[i]); t+=b[i]; } printf("\n"); printf("Process Id Burst Time Wait Time TurnAround Time\n"); int wait_time=0; for(int i=0;i<n;i++) { printf("P%d %d %d %d\n",index[i],b[i],wait_time,wait_time + b[i]); wait_time += b[i]; } return 0; } #include<stdio.h> #include<conio.h> main() { int bt[20], wt[20], tat[20], i, n; float wtavg, tatavg; clrscr(); printf("\\nEnter the number of processes -- "); scanf("%d", &n); for(i=0;i<n;i++) { printf("\\nEnter Burst Time for Process %d -- ", i); scanf("%d", &bt[i]); } wt[0] = wtavg = 0; tat[0] = tatavg = bt[0]; for(i=1;i<n;i++) { wt[i] = wt[i-1] + bt[i-1]; tat[i] = tat[i-1] + bt[i]; wtavg = wtavg + wt[i]; tatavg = tatavg + tat[i]; } printf("\\n\\t PROCESS \\tBURST TIME \\t WAITING TIME\\t TURNAROUND TIME\\n"); for(i=0;i<n;i++) printf("\\n\\t P%d \\t\\t %d \\t\\t %d \\t\\t %d", i, bt[i], wt[i], tat[i]); printf("\\nAverage Waiting Time -- %f", wtavg/n); printf("\\nAverage Turnaround Time -- %f", tatavg/n); getch(); }
Editor is loading...
Leave a Comment