int main(void) { int freie_ressourcen; /* Anlegen des Semaphors */ sem_anfordid = sem_create(SEM_READKEY); /* Setzen des Sempahors auf 1 */ sem_set(sem_anfordid, 1); /* Beginn Serverschleife */ while(1) { /* Abfragen wieviel Ressourcen frei sind, falls 0 Schleife verlassen */ freie_ressourcen = sem_get(sem_anfordid); if (freie_ressourcen == 0) break; /* eine Sekunde warten */ sleep(1); /* Ende Serverschleife */ } /* Semaphor entfernen */ sem_rm(sem_anfordid); printf("Server terminiert.\n"); exit(0); }
void sem_close(int id) { register int semval; /* * The following semop() first gets a lock on the semaphore, * then increments [1] - the process counter. */ if (semop(id, &op_close[0], 3) < 0) err_sys("can't semop"); /* * Now that we have a lock, read the value of the process * counter to see if this is the last reference to the * semaphore. * There is a race condition here - see the comments in * sem_create(). */ if ((semval = semctl(id, 1, GETVAL, 0)) < 0) err_sys("can't GETVAL"); if (semval > BIGCOUNT) err_dump("sem[1] > BIGCOUNT"); else if (semval == BIGCOUNT) sem_rm(id); else if (semop(id, &op_unlock[0], 1) < 0) err_sys("can't unlock"); /* unlock */ }
void sem_close(int id) { register int semval; if (semop(id, &op_close[0], 3) < 0) perror("can't semop"); if ( (semval = semctl(id, 1, GETVAL, 0)) < 0) perror("can't GETVAL"); if (semval > BIGCOUNT) perror("sem[1] > BIGCOUNT"); else if (semval == BIGCOUNT) sem_rm(id); else if (semop(id, &op_unlock[0], 1) < 0) perror("can't unlock"); /* unlock */ }
int main() { int bufferFilledSpaces, bufferEmptySpaces, semM, semN; char *buffer; int temp; int shmid; char c1, c2; int input = 0; int counter; char *arg[1] = {0}; int N; // Create duplicate process and give it code from ProcN N = fork(); if (N == 0) // process is a child { execv("ProcN", arg); } // Create semaphore bufferFilledSpaces bufferFilledSpaces = sem_create(155555, 0); if (bufferFilledSpaces < 0) { Error(1); // Error has occurred } // Create semaphore bufferEmptySpaces bufferEmptySpaces = sem_create(255555, 1); if (bufferEmptySpaces < 0) { Error(1); // Error has occurred } // Creates synchronization semaphores semM = sem_create(355555, 0); if (semM < 0) { Error(1); // Error has occurred } // Creates synchronization semaphores semN = sem_create(455555, 0); if (semN < 0) { Error(1); // Error has occurred } // Opens shared memory between two processes shmid = shm_get(123123, (void*) &buffer, 90); // Synchronization with Process N temp = sem_signal(semN); temp = sem_wait(semM); // Part 1 - Fill in initial values c1 = 'a'; c2 = '0'; counter = 1; while (counter <= 260) { temp = sem_wait(bufferEmptySpaces); buffer[input] = c1; buffer[input+1] = c2; buffer[input+2] = c1; //Debug //printf("# %c%c%c #", c1, c2, c1); if ((counter % 26) == 0) // Increment number if needed { c2++; c1 = 'a'; } else // Otherwise, move to next letter in alphabet { c1++; } counter++; temp = sem_signal(bufferFilledSpaces); } // Part 2 - Fill in initial values c1 = 'A'; c2 = '0'; counter = 1; while (counter <= 260) { temp = sem_wait(bufferEmptySpaces); buffer[input] = c1; buffer[input+1] = c2; buffer[input+2] = c1; //Debug //printf("# %c%c%c #", c1, c2, c1); if ((counter % 26) == 0) // Increment number if needed { c2++; c1 = 'A'; } else // Otherwise, move to next letter in alphabet { c1++; } counter++; temp = sem_signal(bufferFilledSpaces); } // Synchronization with Process N temp = sem_signal(semN); temp = sem_wait(semM); // Remove semaphores temp = sem_rm(bufferEmptySpaces); temp = sem_rm(bufferFilledSpaces); temp = sem_rm(semM); temp = sem_rm(semN); // Remove shared memory temp = shm_rm(shmid); printf("\n"); return 1; }
int main() { int *BufferA; int *BufferB; char c1, c2, c3; int bufAFilled, bufAEmpty, bufBFilled, bufBEmpty; int semX, semY, semZ, CSX, CSY, CSZ; int pX, pY; char *arg[1] = {0}; int shmid1, shmid2; int temp, loop_finished, counter, input; // Create semaphore bufAFilled bufAFilled = sem_create(500001, 0); if (bufAFilled < 0) { Error(1); // Error has occurred } // Create semaphore bufAEmpty bufAEmpty = sem_create(500002, 11); if (bufAEmpty < 0) { Error(1); // Error has occurred } // Create semaphore bufBFilled bufBFilled = sem_create(500003, 0); if (bufBFilled < 0) { Error(1); // Error has occurred } // Create semaphore bufBEmpty bufBEmpty = sem_create(500004, 23); if (bufBEmpty < 0) { Error(1); // Error has occurred } // Create semaphore semX semX = sem_create(500005, 0); if (semX < 0) { Error(1); // Error has occurred } // Create semaphore semY semY = sem_create(500006, 0); if (semY < 0) { Error(1); // Error has occurred } // Create semaphore semZ semZ = sem_create(500007, 0); if (semZ < 0) { Error(1); // Error has occurred } // Create semaphore CSX CSX = sem_create(500008, 0); if (CSX < 0) { Error(1); // Error has occurred } // Create semaphore CSY CSY = sem_create(500009, 0); if (CSY < 0) { Error(1); // Error has occurred } // Create semaphore CSZ CSZ = sem_create(500010, 0); if (CSZ < 0) { Error(1); // Error has occurred } // Opens shared memory between two processes shmid1 = shm_get(900001, (void*) &BufferA, 22); // Opens shared memory between two processes shmid2 = shm_get(900002, (void*) &BufferB, 69); // Create duplicate processes and give them code for processes X and Y pX = fork(); if (pX == 0) // process is a child { execv("ProcX", arg); } pY = fork(); if (pY == 0) // process is a child { execv("ProcY", arg); } // Synchronize with ProcX and ProcY temp = sem_signal(semX); temp = sem_signal(semY); temp = sem_wait(semZ); temp = sem_wait(semZ); // Take out items from buffers when elements exist counter = 1; input = 0; while (counter <= 260) { // Wait until both of the buffers have been filled temp = sem_wait(CSZ); temp = sem_wait(CSZ); // Remove one item from BufferA when one exists temp = sem_wait(bufAFilled); c1 = BufferA[input*2]; c2 = BufferA[input*2+1]; printf("+ %c%c +", c1, c2); temp = sem_signal(bufAEmpty); // Remove one item from BufferB when one exists temp = sem_wait(bufBFilled); c1 = BufferB[input*3]; c2 = BufferB[input*3+1]; c3 = BufferB[input*3+2]; printf("+ %c%c%c +", c1, c2, c3); temp = sem_signal(bufBEmpty); if ((counter % 160) == 0) // Sleep after taking every 160 items { // printf("-!Sleeping 3 seconds!-"); // sleep(3); } counter++; // Signal the commencement of the filling both the buffers temp = sem_signal(CSX); temp = sem_signal(CSY); } // Synchronize with ProcX and ProcY temp = sem_signal(semX); temp = sem_signal(semY); temp = sem_wait(semZ); temp = sem_wait(semZ); // sleep(7); printf("\n"); // Remove semaphores temp = sem_rm(bufAEmpty); temp = sem_rm(bufAFilled); temp = sem_rm(bufBEmpty); temp = sem_rm(bufBFilled); temp = sem_rm(semX); temp = sem_rm(semY); temp = sem_rm(semZ); temp = sem_rm(CSX); temp = sem_rm(CSY); temp = sem_rm(CSZ); // Remove shared memory temp = shm_rm(shmid1); temp = shm_rm(shmid2); printf("\nProcZ: Exiting...\n"); return 1; }