/**************************************************** * This program takes two arguments, creates * * a named FIFO, a reader process (r) and a writer * * process (w): * * **************************************************/ #include using namespace std; #include #include #include #include #include #include #include #define MAX 100 // namedPipe1.c++ // $ ./a.out r & // $ ./a.out w & int main(int count, char *vect[]) { int err, pd ; char wBuff[] = "This text will be written in pipe", rBuff[MAX] = {0}; if(count < 3) { cerr << "Less number of arguments\n" ; exit(1) ; } err = mkfifo(vect[2], 0666) ; if(err == -1 && errno != EEXIST){ cerr << "errno: " << errno << "\n"; exit(1); } if(strcmp(vect[1], "r") == 0) { // Reader process pd = open(vect[2], O_RDONLY) ; read(pd, rBuff, MAX); cout << "OutData: " << rBuff << "\n" ; close(pd); } else if(strcmp(vect[1], "w") == 0) { // Writer process pd = open(vect[2], O_WRONLY) ; write(pd, wBuff, strlen(wBuff)) ; close(pd); } else { cerr << "Wrong 2nd argument\n" ; exit(1) ; } return 0; }