#include <iostream>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
using namespace std;
/*Montelo paragogou katanaloti -Master /slave*/

void execute_task(int p, int &nth) {
     // cout<< "nth="<< nth<<endl;
//      if (p<=nth) {
      //to ekteloumeno task
            cout<< "Child Process ["<<p <<"]started"<<endl;
            usleep(100*1000);
            cout<<"child process ["<< p<< "]ended"<< endl;
  //    } else  {
    //      cout<< "Erroneous PID:"<< p<<endl;
    //  }
       
}
int main(void ) {
       int nth;
       cout << "NP=?";
       cin>> nth;
       int pid[nth]={0};
       int i=0;
       // Polytektopoiisi
       for (i=0;i<nth;i++) {
            pid[i]=fork();
            if (pid[i]==0) break;
      }
      // empodizo ton master na ektelesei task
      if (pid[i]==0) {
             execute_task(getpid()%getppid(),nth);
      }  else {
      
      //master barrier
             for (i=0;i<nth;i++) {
             wait(NULL);
        }
        }
    return 0;
}
