/* This program shows how a pair of processes can come to an agreement on their incoming-outgoing pipes. * To run, first compile the program using * * g++ -lpthread agreement.cpp -o agreement * * Then run two instances of the program using ./agreement in two separate terminals. * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #define FIRST_TO_SECOND "FIFO1" #define SECOND_TO_FIRST "FIFO2" using namespace std; string outgoing, incoming; int main() { char c; int shmid; key_t key; static int *b; key = 5678; /* * Create one-byte shared memory segment. Each process reads this bit, if it is 1, * changes it to zero and if it is zero changes it to one. Depending on the initial * value, the incoming and outgoing pipes are created, to ensure the two processes use different pipes for incoming/outgoing communication. */ if ((shmid = shmget(key, 1, IPC_CREAT | 0666)) < 0) { cerr<<"shmget"; exit(1); } /* A binary semaphore to be used for mutual exclusion */ sem_t * sem_id; sem_id=sem_open("mysem2", O_CREAT, 0, 1); if(sem_id == SEM_FAILED) { perror("sem_open"); return 1; } /* Semaphore is used for mutual exclusion in access to the shared bit*/ if(sem_wait(sem_id) < 0) { perror("sem_wait"); } /* * Now we attach the segment to our data space. */ if ((b = ( int *)shmat(shmid, 0, 0)) == (int *) -1) { cerr<<"shmat"; exit(1); } cout<