c++ - how to synchronize N processes -
q1. (see code below) if comment while loop. can communicate through ipc. why while loop, i'm getting no output?
q2. there better way communicate between processes rather using pipe, (through signals or semaphores if yes kindly provids examples.
#include <iostream> #include <unistd.h> #include <sys/wait.h> #include <stdlib.h> using namespace std; int main(){ int person_from_whom_token_starts =1; bool started = false ; int tok_value = 3; int ivetoken = 4; int tok_value_const = 5; int x; bool y; int fd[2]; int status = 0; pipe(fd); switch(fork()){ case -1: cout << "errr" << endl; break; case 0: close(fd[0]); dup2(fd[1],1); while(1){ cout << person_from_whom_token_starts << started << tok_value << ivetoken << tok_value_const; sleep(3); } exit(0); break; default: sleep(1); dup2(fd[0],0); close(fd[1]); cin >> x >> y; cout << "x: " << x << "\ny: " << y << endl; //wait(&status); break; } return 0; }
Comments
Post a Comment