int client(void) { int s; /* spingrab in order to avoid sleep, testcase has a timeout */ while ( (s = msemgrab(SEM0, 0)) == -1 ); int cnt = 0; for (int i = 0; i < 5; ++i) { if (msemdown(s, 1, 1) != 0) { if (errno == EINTR) { /* interrupted by syscall, try again */ continue; } perror("mP(1)"); break; } printf("c%d", cnt); cnt += 2; fflush(stdout); if (msemup(s, 1, 0) != 0) { perror("mV(0)"); break; } } return 0; }
void cleanupsems(void) { int s; s = msemgrab(SEM0, 0); if (s != -1) { msemrm(s); } }
/** Initialize all variables needed for ipc */ static void ipc_init(void) { /* All communicating processes need to be started from within the ssame working dir in order to make key have the same value */ key_t key = ftok(".", 'x'); if(key == -1) { bail_out(EXIT_FAILURE, "Couldn't create ipc key via ftok"); } /* cnd is a two field semaphore (0 = write, 1 = read)*/ if((cnd = mseminit(key, (0600), 2, 1, 0)) == -1) { if((cnd = msemgrab(key, 2)) == -1) { bail_out(EXIT_FAILURE, "Error aquiring conditional semephores"); } } /* mtx is a mutex semaphore to protect access to shm */ if((mtx = seminit(key + 1, (0600), 0)) == -1) { if((mtx = semgrab(key + 1)) == -1) { bail_out(EXIT_FAILURE, "Error aquiring mutual exclusive semephore"); } } /* Create or fetch shm segment */ if((shm = shmget(key, sizeof(ipc_data_t), IPC_CREAT | IPC_EXCL | (0600))) == -1) { if((shm = shmget(key, sizeof(ipc_data_t), 0)) == -1) { bail_out(EXIT_FAILURE, "Error aquiring shared mem"); } /* The process that creates the shm segment will up the mtx, after it's initialized */ if(semdown(mtx) == -1) { bail_out(EXIT_FAILURE, "Error downing mutex"); } /* Mount shm */ if((shared = shmat(shm, NULL, 0)) == (void *) -1) { bail_out(EXIT_FAILURE, "Error attaching shm segment"); } #ifndef _BUILD_READIN /* Signal listening reader */ shared->flag |= READER_F; #endif /* Let other processes proceed */ if(semup(mtx) == -1) { bail_out(EXIT_FAILURE, "Error upping mutex"); } } else { /* Mount shm */ if((shared = shmat(shm, NULL, 0)) == (void *) -1) { bail_out(EXIT_FAILURE, "Error attaching shm segment (creator)"); } /* If we created the segment, we'll initialize it */ (void) memset(shared, 0, sizeof(ipc_data_t)); #ifndef _BUILD_READIN /* Signal listening reader */ shared->flag |= READER_F; #endif /* And signal other processes to proceed */ if(semup(mtx) == -1) { bail_out(EXIT_FAILURE, "Error opening mutex"); } } }