Solution for Mutual exclusion with busy waiting using locked variable in C++ Object Oriented Approach Note:The output will be displayed in the same folder.
#include <iostream> #include <stdlib.h> #include <fstream> #include <time.h> using namespace std; int lock = 0, check; class process { public : process() { counter = 0; checkEntry = 0; } int print(){ if(counter == 1) { lock = 0; counter = 0; return 1; } if(lock == 0){ lock = 1; if(checkEntry!=1) check += 1; counter++; return 0; } } private: int counter,checkEntry; }; int main() { srand(time(NULL)); ofstream myfile; int i = 0,k = 0,a[5] = {0,1,2,3,4},arrLength = 5; myfile.open ("example.txt"); int n; process p[5]; while(check!=5) { n = rand()%5; if(p[n].print()) myfile << "P" << n << " is in busy wait"<< endl; else myfile << "P" << n << " is in cr"<<endl; k++; } }
No comments:
Post a Comment