Sie sind auf Seite 1von 9

// WAP TO IMPLEMENT BANKER ALGO #include<stdlib.h> #include<conio.

h> #include<iostream> using namespace std; int main(int argc, char **argv){ int num_resources; int num_customers; int *existing_resources; int *available_resources; int **current_allocation; int **requested_resources; cout<<"Banker's Algorithm"<<endl;

cout<<"Enter number of Customers:"; cin>>num_customers; cout<<"Enter number of Resources:"; cin>>num_resources; if(!(existing_resources=(int*)malloc(sizeof(int)*num_resources))){ cout<<"Failed to allocate existing_resources"<<endl; getch(); exit(1); } if(!(available_resources=(int*)malloc(sizeof(int)*num_resources))){ cout<<"Failed to allocate available_resources"<<endl; getch(); exit(1); } if(!(current_allocation=(int**)malloc(sizeof(int*)*num_customers))){ cout<<"Failed to allocate the current_allocation dope vector"<<endl; getch(); exit(1); } if(!(requested_resources=(int**)malloc(sizeof(int*)*num_customers))){ cout<<"Failed to allocate the requested_resources dope vector"<<endl; getch(); exit(1); } for(int ii=0;ii<num_customers;ii++){ if(!(current_allocation[ii]=(int*)malloc(sizeof(int)*num_resources))){ cout<<"Failed to allocate current_allocation dope vector entry "<<ii<<endl; getch();

exit(1); } if(!(requested_resources[ii]=(int*)malloc(sizeof(int)*num_resources))){ cout<<"Failed to allocate requested_resources dope vector entry "<<ii<<endl; getch(); exit(1); } } for(int ii=0;ii<num_resources;ii++){ cout<<"How many of resource"<<ii<<"exist?"; cin>>existing_resources[ii]; available_resources[ii]=existing_resources[ii]; } for(int ii=0;ii<num_customers;ii++){ cout<<"Tell me about the resources for customer"<<ii<<endl; for(int jj=0;jj<num_resources;jj++){ cout<<"How many of resource "<<jj<<" are currently allocated to customer "<< ii <<"?"; cin>>current_allocation[ii][jj]; available_resources[jj]-=current_allocation[ii][jj]; if(available_resources[jj]<0){ cout<<"Overallocation of resources"<<endl; getch(); exit(1); } cout<<"How many maximum of resource "<<jj<<" would customer "<<ii<<" like to have?"; cin>>requested_resources[ii][jj]; requested_resources[ii][jj]-=current_allocation[ii][jj]; } } int done=0; int failed; int trials; int freed; while(!done){ trials=0; freed=0; for(int ii=0;ii<num_customers;ii++){ if(requested_resources[ii]){ trials++; failed=0; for(int jj=0;jj<num_resources;jj++){ if(requested_resources[ii][jj]>available_resources[jj]){ failed=1;

cout<<"Failed to satisfy customer "<<ii<<endl; break; } } if(!failed){ freed++; for(int jj=0;jj<num_resources;jj++){ available_resources[jj]+=current_allocation[ii][jj]; } free(requested_resources[ii]); requested_resources[ii]=NULL; cout<<"Satisfying Customer "<<ii<<endl; } } } if(trials==0){ cout<<"The state is safe!!!"<<endl; break; } if(freed==0){ cout<<"Unsafe!!!"<<endl; break; } } getch(); } /* Output: Banker's Algorithm Enter number of Customers:2 Enter number of Resources:3 How many of resource0exist?5 How many of resource1exist?5 How many of resource2exist?5 Tell me about the resources for customer0 How many of resource 0 are currently allocated to customer 0?3 How many maximum of resource 0 would customer 0 like to have?2 How many of resource 1 are currently allocated to customer 0?1 How many maximum of resource 1 would customer 0 like to have?4 How many of resource 2 are currently allocated to customer 0?4 How many maximum of resource 2 would customer 0 like to have?1 Tell me about the resources for customer1 How many of resource 0 are currently allocated to customer 1?0 How many maximum of resource 0 would customer 1 like to have?5 How many of resource 1 are currently allocated to customer 1?2 How many maximum of resource 1 would customer 1 like to have?2 How many of resource 2 are currently allocated to customer 1?0

How many maximum of resource 2 would customer 1 like to have?5 Failed to satisfy customer 0 Failed to satisfy customer 1 Unsafe!!! Process returned 1 (0x1) execution time : 44.453 s Press any key to continue. */

// WAP To implement Master-Schedule Algo. #include<iostream> #include<stdio.h> #include<conio.h> using namespace std; class mps { int job,dead[100],pen[100],job_array[100]; public: void input(); void deadline_time(void); int sort_dead(void); int sort_pen(void); void interchange(int); }; void mps::input(void) { cout<<"***************************************"<<"\n"; cout<<"MASTER PRODUCTION SCHEDULE"<<"\n"; cout<<"***************************************"<<"\n"; cout<<"enter the value of number of the job: "<<"\n"; cin>>job; for(int i=0; i<job; i++) { job_array[i]=i+1; cout<<"enter the value of deadline ["<<i+1<<"] job:"; cin>>dead[i]; cout<<"enter the value of penalty ["<<i+1<<"] job:"; cin>>pen[i]; cout<<"n"; } } void mps::deadline_time(void) { int k,sum_dead=0,sum_pen=0; for(k=0; k<job; k++) { sum_dead += dead[k]; sum_pen += pen[k]; } cout<<"sum of the deadline value is : "<<sum_dead<<"\n"; cout<<"sum of the penalty value is :"<<sum_pen<<"\n"; if(sum_dead>sum_pen) { sort_dead();

} else { sort_pen(); } } void mps::interchange(int j) { int temp; temp=job_array[j]; job_array[j]=job_array[j+1]; job_array[j+1]=temp; } int mps::sort_dead(void) { int temp,i,j; for(i=0; i<job; i++) { for(j=0; j<job-1; j++) { if(dead[job_array[j]]<dead[job_array[j+1]]) { interchange(j); } } } cout<<"\n\n job order according to deadline::\n"; for(i=0; i<job; i++) { cout<<"job order wlll be ["<<i+1<<"]job:"<<job_array[i]<<"\n"; } return dead[i]; } int mps::sort_pen(void) { int temp,i,j,k; for(i=0; i<job; i++) { for(j=0; j<job-1; j++) { if(pen[job_array[j]]<pen[job_array[j+1]]) { interchange(j);

} } cout <<"\n\n JOB ORDER ACCORDING TO PENALTY ::\N"; for(i+0; i<job; i++) { cout<<"job order wll be ["<<i+1<<"] job :"<<job_array[i]<<"\n"; } return pen[i]; } } int main() { mps o; o.input(); o.deadline_time(); getch(); return 0; } /* *************************************** MASTER PRODUCTION SCHEDULE *************************************** enter the value of number of the job: 3 enter the value of deadline [1] job:6 enter the value of penalty [1] job:3 nenter the value of deadline [2] job:9 enter the value of penalty [2] job:2 nenter the value of deadline [3] job:11 enter the value of penalty [3] job:7 nsum of the deadline value is : 26 sum of the penalty value is :12

job order according to deadline:: job order wlll be [1]job:3 job order wlll be [2]job:2 job order wlll be [3]job:1 */

// WAP to implement BOM algo #include<iostream> using namespace std; int main() { int bom[20][20]; int ndept,nitem; cout<<"\n Enter no of departments : "; cin>>ndept; cout<<"\n Enter no of items : "; cin>>nitem; int i,j; for(i=0;i<ndept;i++){ cout<<"\n Enter no of instances for each item for department"<<i<<":"<<endl; for(j=0;j<nitem;j++){ cout<<"\n Enter number of instances for each item"<<j<<":"; cin>>bom[i][j]; } } for(i=0;i<ndept;i++){ for(j=0;j<nitem;j++){ cout<<"\n dept "<<i<<" has "<<" "<<bom[i][j]<<"instances of item"<<j; } cout<<"\n"; } cout<<"\n"; } /* Output Enter no of departments : 2 Enter no of items : 3 Enter no of instances for each item for department0: Enter number of instances for each item0:1 Enter number of instances for each item1:2 Enter number of instances for each item2:4 Enter no of instances for each item for department1: Enter number of instances for each item0:2

Enter number of instances for each item1:3 Enter number of instances for each item2:1 dept 0 has 1instances of item0 dept 0 has 2instances of item1 dept 0 has 4instances of item2 dept 1 has 2instances of item0 dept 1 has 3instances of item1 dept 1 has 1instances of item2

Process returned 0 (0x0) execution time : 5.813 s Press any key to continue. */

Das könnte Ihnen auch gefallen