Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Stwórz trzy procesy, które b ę d ą wymienia ł y mi ę dzy sobą dane poprzez dwa obszary
- pami ę ci dzielonej (np. niech proces drugi po ś redniczy w wymianie danych pomi ę dzy
- pierwszym i drugim obszarem pami ę ci dzielonej).
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/ipc.h>
- #include <sys/types.h>
- #include <sys/shm.h>
- #include <sys/sem.h>
- #include <unistd.h>
- #include <wait.h>
- #include <time.h>
- void error_exit(const char *msg)
- {
- perror(msg);
- exit(EXIT_FAILURE);
- }
- void lock_mutex(int mux)
- {
- struct sembuf lock_mutex = {0, -1, 0};
- if(semop(mux, &lock_mutex, 1) == -1)
- error_exit("lock_mutex");
- }
- void unlock_mutex(int mux)
- {
- struct sembuf unlock_mutex = {0, 1, 0};
- if(semop(mux, &unlock_mutex, 1) == -1)
- error_exit("unlock_mutex");
- }
- void first_process(int shmid, int mux)
- {
- int *shared_memory;
- int random_number = (rand() % 10) + 1;
- if((shared_memory = (int *)shmat(shmid, NULL, 0)) == (void *)-1)
- error_exit("first_shmat");
- lock_mutex(mux);
- (*shared_memory) = random_number;
- unlock_mutex(mux);
- printf("Proces 1: Zapisuje do pamieci dzielonej liczbe: %d\n", *shared_memory);
- if(shmdt((void *)shared_memory) == -1)
- error_exit("first_shmdt");
- }
- void second_process(int shmids[], int mux)
- {
- int *first_shared_memory;
- int *second_shared_memory;
- if((first_shared_memory = shmat(shmids[0], NULL, 0)) == (void *)-1)
- error_exit("second_shmat");
- if((second_shared_memory = shmat(shmids[1], NULL, 0)) == (void *)-1)
- error_exit("second_shmat");
- lock_mutex(mux);
- (*second_shared_memory) = (*first_shared_memory);
- unlock_mutex(mux);
- printf("Proces 2: Przekazuje liczbe %d z pierwszej pamieci dzielonej do drugiej\n",
- *second_shared_memory);
- if(shmdt(first_shared_memory) || shmdt(second_shared_memory) == -1)
- error_exit("second_shmdt");
- }
- void third_process(int shmid, int mux)
- {
- int *shared_memory;
- if((shared_memory = (int *)shmat(shmid, NULL, 0)) == (void *)-1)
- error_exit("third_shmat");
- printf("Proces 3: Otrzymana wartosc: %d\n", *shared_memory);
- if(shmdt(shared_memory) == -1)
- error_exit("third_shmdt");
- }
- int main(void)
- {
- pid_t pids;
- int shmids[2];
- int mux;
- srand(time((time_t *)NULL));
- for(int i = 0; i < 2; i++)
- if((shmids[i] = shmget(IPC_PRIVATE, SHMLBA, 0775 | IPC_CREAT | IPC_EXCL)) == -1)
- error_exit("shmget");
- if((mux = semget(IPC_PRIVATE, 2, 0775 | IPC_CREAT | IPC_EXCL)) == -1)
- error_exit("semget");
- for(int i = 0; i < 2; i++)
- if(semctl(mux, i, SETVAL, 1) == -1)
- error_exit("semctl");
- for(int i = 0; i < 3; i++){
- pids = fork();
- if(pids == -1)
- error_exit("fork");
- else if(pids == 0){
- switch(i){
- case 0:
- first_process(shmids[0], mux);
- break;
- case 1:
- second_process(shmids, mux);
- break;
- case 2:
- third_process(shmids[1], mux);
- break;
- default:
- error_exit("switch");
- }
- return EXIT_SUCCESS;
- }
- }
- for(int i = 0; i < 3; i++)
- if(wait(NULL) == -1)
- error_exit("wait");
- if(semctl(mux, 0, IPC_RMID, NULL) == -1)
- error_exit("semctl");
- for(int i = 0; i < 2; i++)
- if(shmctl(shmids[i], IPC_RMID, NULL) == -1)
- error_exit("shmctl_rmid");
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment