Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Napisz dwa programy. Pierwszy stworzy semafor i zainicjuje go war- tością dodatnią, a następnie poczeka, aż drugi program ustawi wartość tego semafor na zero i dopiero wtedy zakończy się.
- */
- // Program pierwszy
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/ipc.h>
- #include <sys/types.h>
- #include <sys/sem.h>
- #include <unistd.h>
- #define FTOK_PATH "/tmp"
- #define FTOK_ID 712
- #define ERROR_MESSAGE_SIZE 20
- void sem_up_and_wait(int semset_id)
- {
- struct sembuf up_operation = {0, 1, 0};
- struct sembuf wait_operation = {0, 0, 0};
- printf("Program 1: Semafor ustawiony, czekanie na wyzerowanie...\n");
- if(semop(semset_id, &up_operation, 1) == -1){
- perror("up_semop");
- exit(EXIT_FAILURE);
- }
- if(semop(semset_id, &wait_operation, 1) == -1){
- perror("wait_semop");
- exit(EXIT_FAILURE);
- }
- printf("Program 1: Semafor zostal wyzerowany!\n");
- }
- int main(void)
- {
- key_t key;
- int semset_id;
- if((key = ftok(FTOK_PATH, FTOK_ID)) == -1){
- perror("ftok");
- return EXIT_FAILURE;
- }
- if((semset_id = semget(key, 1, 0775|IPC_CREAT|IPC_EXCL)) == -1){
- perror("semget");
- return EXIT_FAILURE;
- }
- sem_up_and_wait(semset_id);
- if(semctl(semset_id, 0, IPC_RMID) == -1){
- perror("semctl");
- return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment