Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <signal.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <err.h>
- #include <sys/sem.h>
- key_t key1, key2, key3; // klucz dla semaforow
- int semid1, semid2, semid3; // ID semaforow
- union semun ctl; // unia do kontroli semafora
- int pid1, pid2, pid3, ppid;
- FILE* input;
- FILE* temporary;
- int pdes[2];
- 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 charToHex(unsigned char c, char * hex) {
- sprintf(hex, "%x", c);
- }
- void process1Interactive() {
- char data[256] = "\n\0"; char c; int i;
- while(1){
- sleep(1);
- fprintf(stderr, "\nPodaj dane (EXIT, by wyjsc):");
- fgets(data,256,stdin);
- if( strcmp( data , "EXIT\n" ) == 0 ) break;
- i = 0;
- while(data[i]!= '\n'){
- semlock(semid1);
- fputc(data[i], temporary);
- fflush(temporary);
- fseek(temporary, -1, SEEK_CUR);
- i++;
- semunlock(semid2);
- }
- }
- fprintf(stderr,"\nKoniec wczytywania danych");
- kill(getpid(), 1);
- }
- void process1File() {
- char c; int i;
- while((c = fgetc(input))!= EOF) {
- semlock(semid1);
- fputc(c, temporary);
- fflush(temporary);
- fseek(temporary, -1, SEEK_CUR);
- semunlock(semid2);
- }
- fprintf(stderr,"\nKoniec wczytywania danych");
- kill(getpid(), 1);
- }
- void process1Random() {
- unsigned char c; int i;
- while((c = fgetc(input))!= EOF) {
- semlock(semid1);
- fputc(c, temporary);
- fflush(temporary);
- fseek(temporary, -1, SEEK_CUR);
- semunlock(semid2);
- }
- }
- void process2() {
- unsigned char c;
- char hex[3];
- close(pdes[0]);
- while(1) {
- semlock(semid2);
- c = fgetc(temporary);
- charToHex(c, hex);
- write(pdes[1], hex, sizeof(hex));
- semunlock(semid3);
- }
- }
- void process3() {
- unsigned char c; char heks[3]; int k = 0;
- close(pdes[1]);
- while(1) {
- semlock(semid3);
- read(pdes[0], heks, sizeof(heks));
- fprintf(stderr, "%s ", heks);
- k++;
- if( k == 15 ) {
- fprintf(stderr,"\n");
- k = 0;
- }
- semunlock(semid1);
- }
- }
- void signalHandler(int signal) {
- if(signal == 2) {
- fprintf(stderr, "\nZabijam proces o pid %d", getpid());
- exit(0);
- }
- else if(signal == 1) { // getpid() == pid1 &&
- kill(pid2, 2);
- kill(pid3, 2);
- kill(ppid, 2);
- kill(pid1, 2);
- }
- }
- void signalMotherHandler(int signal) {
- if(signal == 2 ) {
- fprintf(stderr, "\nZabijam proces macierzysty o pid: %d", ppid);
- close((int)input);
- close((int)temporary);
- remove("file");
- exit(0);
- }
- }
- void signalInit() {
- signal(1, signalHandler);
- signal(2, signalHandler);
- }
- int main(int argc, char **argv)
- { //////////////
- 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!");
- if ((key3 = ftok(".", 'C')) == -1)
- errx(1, "Blad tworzenia klucza!");
- if ((semid3 = semget(key3, 1, IPC_CREAT | 0600)) == -1)
- errx(2, "Blad tworzenia semafora!");
- ctl.val = 1;
- if (semctl(semid3, 0, SETVAL, ctl) == -1)
- errx(3, "Blad ustawiania semafora!");
- semlock(semid3);
- semlock(semid2);
- pipe(pdes);
- if(argc>1) {
- input = fopen(argv[1], "r");
- if(input == NULL) {
- fprintf(stderr, "Blad przy otwieraniu pliku do odczytu!");
- return -1;
- }
- }
- temporary = fopen("file", "w+");
- if(temporary == NULL) {
- fprintf(stderr, "Blad przy otwieraniu pliku do zapisu!");
- return -2;
- }
- ppid = getpid();
- fprintf(stderr, "Ppid = %d\n", ppid);
- signal(2, signalMotherHandler);
- if( fork() == 0 ) { /////////////////////////////////////////////// proces 1 -> przekazuje dane do procesu 2 poprzez file
- pid1 = getpid();
- fprintf(stderr, "Pid1 = %d\n", pid1);
- signalInit();
- if(argc == 1) { // interaktywny
- process1Interactive();
- }
- else if( strcmp(argv[1], "/dev/urandom") == 0 ){ // random
- process1Random();
- }
- else { // plik
- process1File();
- }
- }
- else if(fork() == 0) { /////////////////////////////////// proces 2 -> pobiera dane z procesu 1 poprzez file i przekazuje do procesu 3 poprzez pipe
- pid2 = getpid();
- fprintf(stderr, "Pid2 = %d\n", pid2);
- signalInit();
- process2();
- }
- else if(fork() == 0) { ////////////////////////////////// proces 3 -> pobiera dane z procesu 3 poprzez pipe
- pid3 = getpid();
- fprintf(stderr, "Pid3 = %d\n", pid3);
- signalInit();
- process3();
- }
- pause();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement