Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Zademonstruj działanie operacji SEM_UNDO.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/ipc.h>
- #include <sys/types.h>
- #include <sys/sem.h>
- #include <sys/wait.h>
- void sem_down(int semid)
- {
- struct sembuf down_operation;
- down_operation.sem_num = 0;
- down_operation.sem_op = -1;
- down_operation.sem_flg = SEM_UNDO;
- if(semop(semid, &down_operation, 1) == -1){
- perror("semop");
- exit(EXIT_FAILURE);
- }
- }
- int main(void)
- {
- int semid;
- pid_t pid;
- if((semid = semget(IPC_PRIVATE, 1, 0775 | IPC_CREAT | IPC_EXCL)) == -1){
- perror("semget");
- return EXIT_FAILURE;
- }
- if(semctl(semid, 0, SETVAL, 1) == -1){
- perror("semctl");
- return EXIT_FAILURE;
- }
- printf("Wartosc semafora ustawiona na 1\n");
- pid = fork();
- if(pid == -1){
- perror("fork");
- return EXIT_FAILURE;
- }
- else if(pid == 0){
- sem_down(semid);
- int buffer;
- if((buffer = semctl(semid, 0, GETVAL)) == -1){
- perror("semctl");
- return EXIT_FAILURE;
- }
- printf("Potomek: wartosc semafora po opuszczeniu semafora: %d\n", buffer);
- }
- else{
- wait(0);
- int buffer;
- if((buffer = semctl(semid, 0, GETVAL)) == -1){
- perror("semctl");
- return EXIT_FAILURE;
- }
- printf("Rodzic: wartosc semafora po wykonaniu kodu potomka: %d\n", buffer);
- printf("Rodzic: Po wykonaniu kodu potomka, automatyczna operacja V - zwiekszenie wartosci semafora, dzieki fladze SEM_UNDO\n");
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement