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
- 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 dane[256];
- char c;
- while(1){
- sleep(1);
- printf("Podaj dane: ");
- scanf("%s", dane);
- int i = 0;
- while(dane[i]!= '\0'){
- semlock(semid1);
- //printf("Process 1: %c\n", dane[i]);
- //fflush(stdout);
- fputc(dane[i], temporary);
- fflush(temporary);
- fseek(temporary, -1, SEEK_CUR);
- i++;
- semunlock(semid2);
- }
- }
- }
- void process1File() {
- char c; int i;
- while((c = fgetc(input))!= EOF) {
- semlock(semid1);
- //printf("Process 1: %c\n", c);
- //fflush(stdout);
- fputc(c, temporary);
- fflush(temporary);
- fseek(temporary, -1, SEEK_CUR);
- semunlock(semid2);
- }
- }
- void process1Random() {
- unsigned char c; int i;
- while((c = fgetc(input))!= EOF) {
- semlock(semid1);
- //printf("Process 1: %c\n", c);
- //fflush(stdout);
- 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);
- printf("Hex = %s", hex);
- //printf("Process 2: %c\n", c);
- fflush(stdout);
- write(pdes[1], hex, sizeof(hex));//write(pdes[1], &c, 1);
- semunlock(semid3);
- }
- //remove("file");
- }
- void process3() { // mozna zakonczyc ten proces normalnie za pomoca sygnalu, jak cos
- unsigned char c;
- char heks[3];
- close(pdes[1]);
- while(1) {
- semlock(semid3);
- read(pdes[0], heks, sizeof(heks));//read(pdes[0], &c, 1);
- //printf("In process 3: %c\n", c);
- printf(" , %s\n", heks);
- fflush(stdout);
- semunlock(semid1);
- }
- }
- 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);
- ///////////////////files opening
- if(argc>1) {
- input = fopen(argv[1], "r"); //FILE* input = fopen(argv[1], "r"); /dev/urandom
- if(input == NULL) {
- printf("Blad przy otwieraniu pliku do odczytu!");
- return -1;
- }
- }
- temporary = fopen("file", "w+");
- if(temporary == NULL) {
- printf("Blad przy otwieraniu pliku do zapisu!");
- return -2;
- }
- /////////////////// processes
- if( fork() == 0 ) { /////////////////////////////////////////////// proces 1 -> przekazuje dane do procesu 2 poprzez file
- if(argc == 1) { // interaktywny
- printf("Interactive");
- process1Interactive();
- }
- else if( strcmp(argv[1], "/dev/urandom") == 0 ){ // random
- printf("Random");
- process1Random();
- }
- else { // plik
- printf("File");
- process1File();
- }
- }
- else if(fork() == 0) { /////////////////////////////////// proces 2 -> pobiera dane z procesu 1 poprzez file i przekazuje do procesu 3 poprzez pipe
- process2();
- }
- else if(fork() == 0) { ////////////////////////////////// proces 3 -> pobiera dane z procesu 3 poprzez pipe
- process3();
- }
- while(1)
- pause();
- return 0;
- }
- // /dev/urandom
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement