Question

I'm trying to simulate that my two processes who executes in two different cpu enter the critical section. When both processes' instruction become "CS", I would like to send one of them to the blocked queue. However, even I give the sequence of instructions of two processes same and while they are working in different CPUs in parallel, they both enter the critical section in my simulation code. Can you please analyze my code and tell me what I am doing wrong?

void execute(CPU *cpu)
{
fetch(cpu);
while(cpu->instruction=="NI") {
    //printf("instruction : %s, instructionNo: %d, totalExecution in cpu:%d \n", cpu->instruction, cpu->instructionNo, cpu->totalExecuted);
    Sleep(1000);
    fetch(cpu);
}

if(cpu->instruction=="CS"){
    printf("process %d instruction no: %d",((PCB*) ((cpu->runningProcess)->dataPtr))->processID, cpu->instructionNo);
    WaitForSingleObject(csMutex, INFINITE);
        if(csBoolean==FALSE) {
            csBoolean==TRUE;
        }
        else if(csBoolean==TRUE) {
            printf("process %d suspended.",((PCB*) ((cpu->runningProcess)->dataPtr))->processID);
            ReleaseMutex(csMutex);
            contextSwitch(cpu);
            return;
        }
    ReleaseMutex(csMutex);

    printf("process %d entered its critical section.\n",((PCB*) ((cpu->runningProcess)->dataPtr))->processID);
    Sleep(3000);

    WaitForSingleObject(csMutex, INFINITE);
        csBoolean=FALSE;            
    ReleaseMutex(csMutex);

    ((PCB*)((cpu->runningProcess)->dataPtr))->instructionNo = cpu->instructionNo;
}

else if(cpu->instruction!="NI"){
    contextSwitch(cpu);
}
}
Was it helpful?

Solution

Yep, stupid Visual Studio didn't want to bother me about this annoying syntax error:

 if(csBoolean==FALSE) 
  {
          csBoolean==TRUE;
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top