Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <stdlib.h>
- #include <err.h>
- #include <sys/sem.h>
- #include <err.h>
- #include <string.h>
- #include <signal.h>
- #define SHM_SIZE sizeof(char)
- union semun
- {
- int val;
- struct semid_ds *buf;
- unsigned short int *array;
- struct seminfo *__buf;
- };
- int semlock(int semid)
- {
- struct sembuf opr;
- opr.sem_num = 0;
- opr.sem_op = -1;
- opr.sem_flg = 0;
- if (semop(semid, &opr, 1) == -1){
- warn("Blad blokowania semafora!");
- return 0;
- }else{
- return 1;
- }
- }
- int semunlock(int semid)
- {
- struct sembuf opr;
- opr.sem_num = 0;
- opr.sem_op = 1;
- opr.sem_flg = 0;
- if (semop(semid, &opr, 1) == -1)
- {
- warn("Blad odblokowania semafora!");
- return 0;
- }
- else
- {
- return 1;
- }
- }
- void *przylaczSegment(key_t key)
- {
- int shmid;
- char *shm;
- if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666)) < 0)
- errx(2, "Blad tworzenia segmentu pamieci dzielonej!");
- if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
- errx(3, "Blad przylaczania pamieci dzielonej!");
- return (void*)shm;
- }
- void usunSegment(key_t key)
- {
- int shmid;
- char *shm;
- if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666)) < 0)
- errx(2, "Blad tworzenia segmentu pamieci dzielonej!");
- if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
- errx(3, "Blad przylaczania pamieci dzielonej!");
- shmdt(shm);
- shmctl(shmid, IPC_RMID, NULL);
- }
- int m, p1, p2;
- int main(int argc, char *argv[])
- {
- m = getpid();
- if (argc != 2)
- {
- printf("Zla ilosc argumentow!\n");
- return 1;
- }
- FILE*input = fopen(argv[1], "r");
- if(input == NULL) {
- printf("Blad przy odczytywaniu pliku!\n");
- return 2;
- }
- else if(access ( argv[1], F_OK ) == -1)
- {
- printf("Brak dostepu!\n");
- return 3;
- }
- FILE* output = fopen("out.txt", "w");
- if(output == NULL) {
- printf("Blad przy otwieraniu pliku do zapisu!\n");
- return 4;
- }
- key_t key1, key2; // klucz dla semaforow
- int semid1, semid2; // ID semaforow
- union semun ctl; // unia do kontroli semafora
- if ((key1 = ftok(".", 'A')) == -1)
- errx(1, "Blad tworzenia klucza!");
- if ((semid1 = semget(key1, 1, IPC_CREAT | 0600)) == -1)
- errx(2, "Blad tworzenia semafora!");
- ctl.val = 1;
- if (semctl(semid1, 0, SETVAL, ctl) == -1)
- errx(3, "Blad ustawiania semafora!");
- if ((key2 = ftok(".", 'B')) == -1)
- errx(1, "Blad tworzenia klucza!");
- if ((semid2 = semget(key2, 1, IPC_CREAT | 0600)) == -1)
- errx(2, "Blad tworzenia semafora!");
- ctl.val = 1;
- if (semctl(semid2, 0, SETVAL, ctl) == -1)
- errx(3, "Blad ustawiania semafora!");
- semlock(semid2);
- int datakey;
- if ((datakey = ftok(".", 'A')) == -1)
- errx(1, "Blad tworzenia klucza!");
- przylaczSegment(datakey);
- if(fork())
- {
- p1 = getpid();
- int c;
- do
- {
- c = fgetc(input);
- semlock(semid2);
- char *znak = (char *)przylaczSegment(datakey);
- *znak = c;
- semunlock(semid1);
- } while (c != -1);
- fclose(input);
- return 0;
- }
- else
- {
- p2 = getpid();
- while (1)
- {
- semlock(semid1);
- char *znak = (char *)przylaczSegment(datakey);
- char x = *znak;
- if (x == -123)
- x = 'a';
- else if (x == -121)
- x = 'c';
- else if (x == -103)
- x = 'e';
- else if (x == -124)
- x = 'n';
- else if (x == -126)
- x = 'l';
- else if (x == -77)
- x = 'o';
- else if (x == -101)
- x = 's';
- else if (x == -70)
- x = 'z';
- else if (x == -68)
- x = 'z';
- if (!(x == -59 || x == -60 || x == -61 || x == -1 || x == 0))
- fprintf(output, "%c", x);
- semunlock(semid2);
- if (x == -1) {
- fclose(output);
- usunSegment(datakey);
- kill(p1, SIGKILL);
- kill(p2, SIGKILL);
- kill(m, SIGKILL);
- break;
- }
- }
- }
- for (;;)
- pause();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement