Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <sys/shm.h>
- #include <sys/types.h>
- #include <err.h>
- #include <stdbool.h>
- #include <pthread.h>
- #include <time.h>
- #include <semaphore.h>
- #include <string.h>
- #define BUF_SIZE 256
- #define SEM_NAME_czyt "/semafor_nazwany_czytanie"
- #define SEM_NAME_pis "/semafor_nazwany_pisanie"
- #define SEM_NAME "/semafor_nazwany"
- #define INIT_VAL 1
- typedef struct mutex_funkcja{
- pthread_mutex_t mutex_write;
- pthread_mutex_t mutex_read;
- } mutex;
- static mutex *data = NULL;
- void initialise_shared() {
- pthread_mutexattr_t attr_read;
- pthread_mutexattr_t attr_write;
- pthread_mutexattr_init(&attr_read);
- pthread_mutexattr_init(&attr_write);
- pthread_mutexattr_setpshared(&attr_read, PTHREAD_PROCESS_SHARED);
- pthread_mutexattr_setpshared(&attr_write, PTHREAD_PROCESS_SHARED);
- pthread_mutex_init(&data->mutex_read, &attr_read);
- pthread_mutex_init(&data->mutex_write, &attr_write);
- pthread_mutex_lock(&data->mutex_read);
- }
- int main(int argc, char ** argv)
- {
- /////////////////////// DEKLARACJE /////////////////////////
- pid_t pidm, pid1, pid2, pid3;
- int i;
- char *sciezka;
- char bufor1[BUF_SIZE];
- char bufor2[BUF_SIZE];
- char bufor3[BUF_SIZE];
- /* mutex */
- FILE *plik2;
- /* shared memory */
- int shmid;
- key_t key;
- char *shm, *s;
- /* semafory */
- sem_t *semafor_czyt;
- sem_t *semafor_pis;
- semafor_czyt = sem_open(SEM_NAME_czyt, O_CREAT | O_EXCL, 0644, INIT_VAL);
- if (semafor_czyt == SEM_FAILED) {
- perror("sem_czyt_open error");
- exit(EXIT_FAILURE);
- }
- semafor_pis = sem_open(SEM_NAME_pis, O_CREAT | O_EXCL, 0644, INIT_VAL);
- if (semafor_pis == SEM_FAILED) {
- perror("sem_pis_open error");
- exit(EXIT_FAILURE);
- }
- /////////////////////// PROCES MACIERZYSTY //////////////////////////
- /* printf("Flaga -p [sciezka do pliku] -> pobranie z pliku.\nFlaga -k -> pobranie z klawiatury");
- printf("\nargc = %d\n", argc);
- printf("\nargv[0] = %d\n", argv[0]);
- printf("\nargv[1] = %d\n", argv[1]);
- if(argc == 2 && argv[1] == "-k")
- scanf("%s", bufor1);
- else if(argc == 3 && argv[1] == "-p")
- strcpy(sciezka, argv[2]);
- else
- {
- errx(-1, "Blad parametrow!");
- exit(EXIT_FAILURE);
- }*/
- pidm = getpid();
- printf("PID Procesu Macierzystego : %d\n", pidm);
- /////////////////////////////////////////////////////////////////////
- if(!fork())
- {
- // PID procesu 1
- pid1 = getpid();
- printf("PID Procesu 1: %d\n", pid1);
- FILE *plik = fopen("dane.txt", "r");
- bool fd1 = true;
- sem_wait(semafor_pis); // zablokowanie pisania
- sem_wait(semafor_czyt); // zablokowanie czytania
- /* Tworzymy klucz dla pamieci dzielonej - dla takich samych argumentow wynik zawsze bedzie taki sam. */
- if ((key = ftok(".", 'A')) == -1)
- errx(1, "Blad tworzenia klucza!");
- /* Tworzymy segment pamieci dzielonej */
- if ((shmid = shmget(key, BUF_SIZE, IPC_CREAT | 0666)) < 0)
- errx(2, "Blad tworzenia segmentu pamieci dzielonej!");
- /* Dolaczamy pamiec dzielona do naszej przestrzeni danych. */
- if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
- errx(3, "Blad przylaczania pamieci dzielonej!");
- if(plik == NULL){
- puts("Blad przy otwieraniu pliku z danymi. Koncze proces.");
- return -1;
- }
- s = shm;
- //bufor1[0] = fgetc(plik);
- i=0;
- while(true)
- {
- while(bufor1[i]!=EOF)
- {
- bufor1[i] = fgetc(plik);
- *s++ = bufor1[i];
- i++;
- }
- break;
- }
- fclose(plik);
- sem_post(semafor_czyt); // odblokowanie czytania
- sem_post(semafor_pis); // odblokowanie pisania
- /* Odlaczamy sie od pamieci dzielonej. */
- shmdt(shm);
- /* Kasujemy segment pamieci dzielonej. */
- if(semafor_czyt == 1 && semafor_pis == 1)
- shmctl(shmid, IPC_RMID, NULL);
- sem_unlink(semafor_czyt);
- sem_unlink(semafor_pis);
- }
- else
- {
- if(!fork())
- {
- // PID procesu 2
- pid2 = getpid();
- printf("PID Procesu 2: %d\n", pid2);
- int bajty = 0;
- bool fd2 = true;
- /////////////////////////// WCZYTYWANIE DANYCH Z PROCESU 1 ////////////////////////////////
- sem_wait(semafor_czyt); // zablokowanie czytania
- if ((key = ftok(".", 'A')) == -1)
- errx(1, "Blad tworzenia klucza!");
- /* Tworzymy (podłączamy się ) do pamieci dzielonej. */
- if ((shmid = shmget(key, BUF_SIZE, IPC_CREAT | 0666)) < 0)
- errx(2, "Blad tworzenia segmentu pamieci dzielonej!");
- /* Dolaczamy pamiec dzielona do naszej przestrzeni danych - funkcja shmat zwraca wskaźnik na adres podpietego segmentu pamieci dzielonej. */
- if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
- errx(3, "Blad przylaczania pamieci dzielonej!");
- plik2 = fopen("mutex.txt", "w");
- if (plik2 == NULL) {
- fprintf(stderr, "Blad otwarcia pliku mutex!\n");
- return -1;
- }
- pthread_mutex_lock(&data->mutex_write);
- s = shm;
- while(true)
- {
- for(i = 0; i < BUF_SIZE; i++)
- {
- // pobranie danych z pamieci wspoldzielonej do bufora - bufor2[i] = fgetc(); - chyba
- bufor2[i] = *s;
- fputc(bufor2[i], plik2);
- if(bufor2[i] == EOF)
- {
- fd2 = false;
- break;
- }
- s++;
- bajty++;
- }
- if(fd2 == false)
- break;
- }
- pthread_mutex_unlock(&data->mutex_read);
- fprintf(stderr, "Proces Drugi Odebral %d bajtow.\n", bajty);
- sem_post(semafor_czyt); // odblokowanie czytania
- /* Odlaczamy sie od pamieci dzielonej. */
- shmdt(shm);
- }
- else
- {
- if(!fork())
- {
- // PID procesu 3
- bool fd3 = true;
- pid3 = getpid();
- printf("PID Procesu 3: %d\n", pid3);
- plik2 = fopen("mutex.txt", "r");
- if (plik2 == NULL) {
- fprintf(stderr, "Blad otwarcia pliku mutex!\n");
- return -1;
- }
- pthread_mutex_lock(&data->mutex_read);
- while(true)
- {
- for(i = 0; i < BUF_SIZE; i++){
- bufor3[i] = fgetc(plik2);
- printf("%c", bufor3[i]);
- if(bufor3[i] == EOF){
- fd3 = false;
- break;
- }
- }
- if(fd3 == false)
- break;
- }
- pthread_mutex_unlock(&data->mutex_write);
- }
- }
- }
- ///////////// UTRZYMANIE MACIERZYSTEGO //////////////
- while(true)
- sleep(1);
- /////////////////////////////////////////////////////
- pthread_mutex_destroy(&data->mutex_write);
- pthread_mutex_destroy(&data->mutex_read);
- fclose(plik2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement