Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Pokaż rozwi ą zanie problemu czytelników i pisarzy na przyk ł adzie operacji zapisu
- i odczytu do pami ę ci dzielonej. Do synchronizacji komunikacji u ż yj semaforów. Do
- wypisywania na ekran komunikatów zamiast funkcji printf() u ż yj funkcji write()
- */
- #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>
- #include <string.h>
- #define PROCESSES_NUMBER 16
- #define BUFFER_SIZE 128
- struct shmdata {
- char message[BUFFER_SIZE];
- int readers_number;
- };
- void error_handler(const char *msg)
- {
- perror(msg);
- exit(EXIT_FAILURE);
- }
- void writer(int id, int mux, int shmid)
- {
- struct sembuf up_operation = {0, 1, 0},
- down_operation = {0, -1, 0};
- char info[BUFFER_SIZE];
- struct shmdata *shared_memory;
- if((shared_memory = (struct shmdata *)shmat(shmid, NULL, 0)) == (void *)-1)
- error_handler("shmat_writer");
- if(semop(mux, &down_operation, 1) == -1)
- error_handler("semop_down");
- snprintf(info, BUFFER_SIZE, "Pisarz nr: %d\n", id);
- write(STDOUT_FILENO, info, strlen(info));
- snprintf(shared_memory->message, SHMLBA, "Wiadomosc od pisarza nr %d\n", id);
- if(semop(mux, &up_operation, 1) == -1)
- error_handler("semop_up");
- if(shmdt(shared_memory) == -1)
- error_handler("shmdt");
- }
- void reader(int id, int mux, int shmid)
- {
- struct sembuf message_up_operation = {0, 1, 0},
- message_down_operation = {0, -1, 0},
- readers_up_operation = {1, 1, 0},
- readers_down_operation = {1, -1, 0};
- char info[BUFFER_SIZE];
- struct shmdata *shared_memory;
- if((shared_memory = (struct shmdata *)shmat(shmid, NULL, 0)) == (void *)-1)
- error_handler("shmat_reader");
- if(semop(mux, &readers_down_operation, 1) == -1)
- error_handler("semop_down");
- shared_memory->readers_number++;
- if(shared_memory->readers_number == 1)
- if(semop(mux, &message_down_operation, 1) == -1)
- error_handler("semop_down");
- if(semop(mux, &readers_up_operation, 1) == -1)
- error_handler("semop_up");
- snprintf(info, BUFFER_SIZE, "Czytelnik nr: %d\n", id);
- write(STDOUT_FILENO, info, strlen(info));
- write(STDOUT_FILENO, shared_memory->message, sizeof(shared_memory->message));
- if(semop(mux, &readers_down_operation, 1) == -1)
- error_handler("semop_down");
- shared_memory->readers_number--;
- if(!shared_memory->readers_number)
- if(semop(mux, &message_up_operation, 1) == -1)
- error_handler("semop_up");
- if(semop(mux, &readers_up_operation, 1) == -1)
- error_handler("semop_up");
- if(shmdt(shared_memory) == -1)
- error_handler("shmdt");
- }
- int main(void)
- {
- int mux, shmid;
- pid_t pid;
- int writer_id = 0;
- int reader_id = 0;
- int choice_buffer;
- //////////////////////////
- srand(time((time_t *)NULL));
- if((mux = semget(IPC_PRIVATE, 2, 0775 | IPC_CREAT | IPC_EXCL)) == -1)
- error_handler("semget");
- if((shmid = shmget(IPC_PRIVATE, SHMLBA, 0775 | IPC_CREAT | IPC_EXCL)) == -1)
- error_handler("shmget");
- for(int i = 0; i < 2; i++)
- if(semctl(mux, i, SETVAL, 1) == -1)
- error_handler("semctl_setval");
- for(int i = 0; i < PROCESSES_NUMBER; i++){
- choice_buffer = rand() % 2;
- if(choice_buffer)
- reader_id++;
- else
- writer_id++;
- pid = fork();
- if(pid == -1)
- error_handler("fork");
- else if(pid == 0){
- if(choice_buffer)
- reader(reader_id, mux, shmid);
- else
- writer(writer_id, mux, shmid);
- return EXIT_SUCCESS;
- }
- }
- for(int i = 0; i < PROCESSES_NUMBER; i++)
- if(wait(NULL) == -1)
- error_handler("wait");
- if(shmctl(shmid, IPC_RMID, NULL) == -1)
- error_handler("shmctl");
- if(semctl(mux, 0, IPC_RMID, NULL) == -1)
- error_handler("semctl");
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment