static bool checkDeadlock(tlTask* task, tlHandle on) { tlTask* other = taskForLocked(on); if (other == task) { trace("DEADLOCK: %s", tl_str(other)); return true; } if (!other) return false; if (other->state != TL_STATE_WAIT) return false; if (other->waitFor == on) return false; // if a task waits on an object or such ... return checkDeadlock(task, other->waitFor); }
void Tracker::printDeadlock(ostream & xout){ vector<vector<string> > rings = checkDeadlock(); if(rings.size()==0) cout << "Cong! No Deadlock exsit" << endl; else{ for(int i =0; i<rings.size(); i++){ vector<string> ring = rings.at(i); for(int j=0; j<ring.size(); j++){ xout << ring.at(j) << "\t"; } xout << endl; } } }
tlArray* tlTaskWaitFor(tlTask* task, tlHandle on) { assert(tlTaskIs(task)); assert(task->state == TL_STATE_RUN); assert(task->value); trace("%s.value: %s", tl_str(task), tl_str(task->value)); tlVm* vm = tlTaskGetVm(task); task->state = TL_STATE_WAIT; task->waitFor = on; if (checkDeadlock(task, on)) { task->state = TL_STATE_RUN; return deadlocked(task, on); } if (!tlWorkerIsBound(task->worker)) task->worker = vm->waiter; a_dec(&vm->runnable); return null; }
int main(int argc, char *argv[]) { if(argc < 2) { perror("Run executable as <executable-path> <probability>\n"); exit(1); } srand(time(NULL)); key_t key; if ((key = ftok("manager.c", 'J')) == -1) { perror("ftok"); exit(1); } if ((sid = initsem(key, 6)) == -1) { perror("initsem"); exit(1); } int i, n, j; float cur, p; p = atof(argv[1]); fp1 = fopen("sequence.txt", "r"); fp2 = fopen("matrix.txt", "w"); fgets(trains, MAX_TRAINS, fp1); n = strlen(trains); fprintf(fp2, "\t\t\t\t\tSemaphores\n\t\tNorth\tWest\tSouth\tEast\n"); for(i=0;i<n;i++) fprintf(fp2, "\t\t0\t0\t0\t0\n\n"); fclose(fp1); fclose(fp2); A = (int **)malloc(n*sizeof(int *)); for(i=0;i<n;i++) A[i] = (int *)malloc(4*sizeof(int)); for(i=0;i<n;) { cur = (rand()%101)/100; if(cur > p) { createTrain(i, n); i++; } else { if(checkDeadlock(n)==1) { printf("............................\nSystem Deadlocked\n"); for(j=0;j<i;j++) kill(tr_pr[j], SIGKILL); exit(1); } } } while(1) { sleep(1); if(checkDeadlock(n)==1) { printf("............................\nSystem Deadlocked\n"); for(j=0;j<i;j++) kill(tr_pr[j], SIGKILL); exit(1); } } return 0; }
/* 0 = OK 1 = transaction node was not found on wake -1 = deadlock detected, transaction node destroyed and resources released mutex should be slavelock */ int LBIDResourceGraph::reserveRange(LBID_t start, LBID_t end, VER_t txn, boost::mutex &mutex) { TransactionNode *txnNode; map<VER_t, TransactionNode *>::iterator it; /* look for existing transaction node T - make one if necessary connectResources(); checkDeadlock(); while (txnNode.out.size() > 0) block on T's condvar connectResources(); checkDeadlock(); } */ it = txns.find(txn); if (it == txns.end()) { txnNode = new TransactionNode(txn); txns[txn] = txnNode; } else txnNode = (*it).second; connectResources(start, end, txnNode); // "If txnNode is waiting on at least one LBID range..." while (txnNode->out.size() > 0) { // make sure there's no deadlock before blocking if (checkDeadlock(*txnNode)) { // releaseResources(txn); return ERR_DEADLOCK; } #ifdef BRM_VERBOSE cerr << " RG: sleeping transaction " << txn << endl; set<RGNode *>::iterator sit; cerr << " waiting on: " << endl; for (sit = txnNode->out.begin(); sit != txnNode->out.end(); sit++) { ResourceNode *rn = dynamic_cast<ResourceNode *>(*sit); cerr << hex << rn << dec << " " << rn->lbid() << endl; } #endif txnNode->sleep(mutex); #ifdef BRM_VERBOSE cerr << " RG: txn " << txn << " is awake" << endl; #endif if (txnNode->dead()) { txns.erase(txn); delete txnNode; return ERR_KILLED; } // attempt to grab remaining resources connectResources(start, end, txnNode); } // txn has all requested LBID ranges return ERR_OK; }
int main(int argc,char **argv) { key_t keysem,keyque1,keyque2,keyque3; struct sembuf wait1,signal1; pid_t managerPID = getpid(); MESSAGE msg; pid_t allPID[10]; for(i=0;i<2*COUNT;++i) { for(j=0;j<2;++j) { matrix_array[i][j] = '0'; } matrix_array[i][j] = '\0'; } FILE * f; f = fopen("./matrix.txt","w"); for(i=0;i<2*COUNT;++i) { fprintf(f, "%s\n",matrix_array[i]); } fclose(f); signal(SIGUSR1,produce); signal(SIGUSR2,consume); signal(SIGINT,delete_all); if(argc < 2) { printf("Incorrect Arguments\n"); printf("Enter the type of process\n"); } int type = atoi(argv[1]); wait1.sem_num = 0; wait1.sem_op = -1; wait1.sem_flg = 0; signal1.sem_num = 0; signal1.sem_op = 1; signal1.sem_flg = 0; keysem = ftok(".", 'M'); int nsem=9; semID=semget(keysem, nsem, IPC_CREAT|0666); ushort val[11] = {1, 1, 1, 1, 1, 10, 0, 10, 0}; // 0 mutex file 1,2 mutex producer q1,q2 3,4 mutex consumer q1,q2 5,6 full,empty q1 7,8 full,empty q2 semctl(semID, 0, SETALL, val); keysem = ftok(".", '1'); if((mID1 = msgget(keysem, IPC_CREAT | 0660))<0){ printf("Error Creating Message Queue1\n"); exit(-1); } keysem = ftok(".", '2'); if((mID2 = msgget(keysem, IPC_CREAT | 0660))<0){ printf("Error Creating Message Queue2\n"); exit(-1); } //Creating the producers and consumers i=0,j=0; while(i<COUNT) { char parameter1[10],parameter2[10]; sprintf(parameter1,"%d",managerPID); sprintf(parameter2,"%d",i); if((allPID[j++] = fork()) == 0) { int execpro = execl("./producer","./producer",parameter1,parameter2,(const char*) NULL); if(execpro <0 ) perror("Error in making producer"); exit(0); } i++; } i=0; while(i<COUNT) { char parameter1[10],parameter2[10]; sprintf(parameter1,"%d",managerPID); sprintf(parameter2,"%d",i); if((allPID[j++] = fork()) == 0) { int execcon = execl("./consumer","./consumer",parameter1,parameter2,argv[1],(const char*) NULL); if(execcon < 0) perror("Error in making consumer"); exit(0); } i++; } i=0,j=0; while(1) { sleep(2); semop(semID,&wait1,1); fp = fopen("matrix.txt", "r"); if(fp == NULL){ perror("fopen"); } ssize_t read; char *line = NULL; size_t len = 0; i=0,j=0; while ((read=getline(&line, &len, fp)) != -1) { // printf("line in manager= %s\n",line); for(i=0;i<2;++i) { matrix_array[j][i] = line[i]; } j++; } fclose(fp); semop(semID,&signal1,1); // for(i=0;i<10;++i) // { // for(j=0;j<2;++j) // { // printf("%c",matrix_array[i][j]); // } // printf("\n"); // } makeResourceGraph(); // for(i=0;i<12;++i) // { // for(j=0;j<12;++j) // { // printf("%d",graph[i][j]); // } // printf("\n"); // } int flag = checkDeadlock(); semctl(semID, 0, GETALL, val); if(flag == 1){ // printf("Deadlock Detected\n"); for(i=0;i<2*COUNT;++i) { kill(allPID[i],SIGKILL); } break; } } fp = fopen("result.txt", "a"); if(fp == NULL){ perror("fopen"); } fprintf(fp,"produce_count = %d consume_count = %d \n",produce_count,consume_count); fclose(fp); delete_all(); return 0; }