I need to get the outputs of each process to strictly alternate. however after i select which process to out put first my program gets stuck. Any help would be much appreciated i have been stuck for awhile. code follows.

#include  <stdio.h>
#include  <iostream>
#include  <stdlib.h>
#include  <sys/types.h>
#include  <fcntl.h>
#include  <unistd.h>
#include <time.h>
#include <string>
#include <ctime>

 using namespace std;

int main()
{
 bool flag [2];
 flag [0] = true;
 flag [1] = true;
 int pid;
 int randomnums;
 string whofirst;// input for first process to cout
 int turn = 0;
 time_t Btime1, Btime2, Etime1, Etime2; // time pointers
 srand(time(NULL));// random number seed
 cout << "How many numbers should i generate?";
 cin >> randomnums;
 cout << "Who should I let output first? parent or child? " ;
 cin >> whofirst;
 if(whofirst == "child")
 {
 turn = 0;
 }
 else
 {
 turn = 1;
 }
 

 

 pid = fork();// process forking in two
 if (pid == 0)
    {
      // Child  process
      time( &Btime1);// process start time
      for(int index = 0; index < randomnums; index++) // random number loop
      {
        flag[0] = true;
        
        while(flag[1] == true && turn == 1)
        {
        }
        cout << "\n I am the child; my ID = " << getpid() << " Random Number: " << rand() % 100 << endl;
        flag[0] = false;
        turn = 1;
      }
      time( &Etime1);// process end time
      cout << "\n I am the child; my ID = " << getpid() << " Begin Time: " << ctime (&Btime1)<< " End Time: " << ctime (&Etime1) << endl;
      exit(0);

    }

 else
    { // Parent process
      time( &Btime2); // process start time
      srand(time(NULL)); //random seeding based on current time
      for(int count = 0; count < randomnums; count++)// random number loop
      {
        flag[1] = true;
        
        while(flag[0] == true && turn == 0)
        {
        }
        cout << "\n I am the parent; my ID = " << getpid() << " Random Number: " << rand() % 100 << endl;
        flag[1] = false;
        turn = 0;
        
      }
      time( &Etime2);// process end time
      cout << "\n I am the parent; my ID = " << getpid() << " Begin Time: " << ctime (&Btime2)<< " End Time: " << ctime (&Etime2) << endl;
      exit(0);
    }
}

What's happening here? Will this loop ever exit?

while(flag[1] == true && turn == 1)
{        
}

It is supposed to keep the process busy waiting if the other one is currently couting. the other process is supposed to stop that loop. Im pretty sure the problem is i get stuck in one of the busy waiting loops i just cant figure out why or how.

It is supposed to keep the process busy waiting if the other one is currently couting. the other process is supposed to stop that loop. Im pretty sure the problem is i get stuck in one of the busy waiting loops i just cant figure out why or how.

When you fork, you are no longer modifying the same variables, you make a complete copy of the process and therefore you cannot use one process to alter another without sending some communication to the other process.

This poses the problem of how to make one process fully execute before the other. And fully is the key word... processes have the ability to run simultaneously and frequently shall. Meaning that if you have two processes from fork, and they are both using "cout" to output data, the data will become garbled because the operating system will give them each a turn at the CPU. So if pid1 outputs 123 and pid2 outputs 321, you could end up with 13 22 31.

To fix this you can use waitpid to cause the kernel(?) to block until the child process fully executes or until a signal is received. I'm sorry if this isn't much help I meant to add more in the form of an example but I ran out of time for now. bbl.

Ahh i see I had the understanding that when i used fork() the child was a thread of the parent. im not sure i understand waitpid. however do you think have pipes would work? if i use read to block the process?

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.