I have an integer called:

int pID = 0;

Inside my while loop I'm using pID++; to increment it. Sometimes the pID doesn't get incremented and sometimes it will. My sample output would be.

0
1
2
2
2
3
4
5
5
6
7

Does anyone have an idea to why it would be doing this?


Here is the code.

int main(int argv, char *argc[])
{
  srand(time(NULL));
  totalRunningTime = clock();
  int randomP;
  int pID = 0;
  int ran = 0;
  int tempA;
  FILE * filePt;


  while(true)
  {
    randomP = rand() % 2;
    pID++;
    printf("%i\n",pID);
    if(randomP == 0)// Creates a process
    {
      queue(pID,0,0);
    }

    tempA = q[head].getAffinity();
    if (q[head].getPID() > -1)// give cores a process
    {
  
      switch(tempA)
      {
        case 0:CPU(tempA);
        case 1:CPU(tempA);
        case 2:CPU(tempA);
        case 3:CPU(tempA);
      }
    }
    //S; //sleep
    // Determine process fate
    ran = rand() % 3; 
    switch(ran)
    {
      case 0: killProcess(); // process terminates and is removed from the system.
      case 1:
    {
      if(isQueueEmpty() == 0)// Make sure queue isn't empty
        queue(pop());//1 – process returns to the ready queue to wait its turn
    }
    case 2:
    {
      if(isQueueEmpty() == 0)// Make sure queue isn't empty
        io(pop());//2 – process requires I/O and goes into the I/O queue.
    }
   }

  }
  return 0;
}

In my opinion "++" in C/C++ is just terrible. I don't even know why they allow it to be part of the language. Try

pID = pID + 1;

"++" increment its value by 1 and assigns it to the variable. However, if the operator is inserted inside a conditional statement "as the condition to be evaluated", the value will be assigned to the variable, but the conditional statement will treat it as if such an operation never happened. Just terrible, but that's just me, many programmers use "++" as "the best way".

I have tried this to and no success :(

I have tried

pID = pID + 1
pID = (pID + 1)
pID += 1
++pID

Each has resulted in the same problem as before. I am completely confused to what is going on.

are you sure there is no any other print in the other functions that prints the 2 or 5??
please debude your code to figure out what prints the 2's
or it could be that queue function takes pId by ref and changes its value
to make sure that ++ works try to comment all your code and leave the while and pId++ and you will now if this is the problem

In my opinion "++" in C/C++ is just terrible. I don't even know why they allow it to be part of the language. Try

pID = pID + 1;

"++" increment its value by 1 and assigns it to the variable. However, if the operator is inserted inside a conditional statement "as the condition to be evaluated", the value will be assigned to the variable, but the conditional statement will treat it as if such an operation never happened. Just terrible, but that's just me, many programmers use "++" as "the best way".

I disagree. That is all.

queue(pID,0,0);

this this function.,this can decrements the PID.

other thing.

are did you mean to not break out the switch case staement?

switch(tempA)
      {
        case 0:CPU(tempA); break; //like after each case 
        case 1:CPU(tempA);
        case 2:CPU(tempA);
        case 3:CPU(tempA);
      }

similar to here

switch(ran)
    {
      case 0: killProcess(); // process terminates and is removed from the system.
          break;
      case 1:
    {
      if(isQueueEmpty() == 0)// Make sure queue isn't empty
        queue(pop());//1 – process returns to the ready queue to wait its turn
    }
    case 2:
    {
      if(isQueueEmpty() == 0)// Make sure queue isn't empty
        io(pop());//2 – process requires I/O and goes into the I/O queue.
          break;
    }

so you best shot, If I were you,

comment these statement and see if the increment is fixed or not

// if(randomP == 0)// Creates a process
   // {
   //   queue(pID,0,0);
   // }

The problem was somewhere else in my code. I was forking off parents instead of children, and this was causing multiple parents to run in the background. I fixed the code and now it's working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.