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;
- FILE* pids;
- 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 == 1) { //konczenie procesow
- if(getpid() == pid1 ){
- kill(pid2, 2);
- kill(pid3, 2);
- kill(ppid, 2);
- kill(pid1, 2);
- }
- else kill(pid1, 1);
- }
- else if(signal == 2) {
- fprintf(stderr, "\nZabijam proces o pid %d", getpid());
- exit(0);
- }
- else if(getpid() != pid1) {
- kill(pid1, signal);
- }
- else if(signal == SIGUSR1) {
- kill(pid3, SIGSTOP);
- kill(pid2, SIGSTOP);
- kill(pid1, SIGSTOP);
- }
- else if(signal == SIGUSR2) {
- kill(pid3, SIGCONT);
- kill(pid2, SIGCONT);
- kill(pid1, SIGCONT);
- }
- }
- // sigusr1 -> stopuje wszystkie
- //sigusr2 -> wznawia wszystkie
- // 1 -> zabija wszystkie
- // 2 -> zabija pojedynczo (nie używać!)
- void signalCleanUp(int signal) {
- if(signal == 2 ) {
- close((int)input);
- close((int)temporary);
- close((int)pids);
- remove("file");
- remove("pids.txt");
- exit(0);
- }
- }
- void signalInit() {
- signal(SIGUSR1, signalHandler);
- signal(SIGUSR2, signalHandler);
- signal(1, 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;
- }
- }
- pids = fopen("pids.txt", "w+");
- if(pids == NULL){
- fprintf(stderr, "Blad przy otwieraniu pliku do zapisu pidow!");
- 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); fprintf(pids, "Ppid = %d\n", ppid); fflush(pids);
- signal(2, signalCleanUp);
- if( fork() == 0 ) { /////////////////////////////////////////////// proces 1 -> przekazuje dane do procesu 2 poprzez file
- pid1 = getpid();
- fprintf(stderr, "Pid1 = %d\n", pid1); fprintf(pids, "Pid1 = %d\n", pid1); fflush(pids);
- 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); fprintf(pids, "Pid2 = %d\n", pid2); fflush(pids);
- signalInit();
- process2();
- }
- else if(fork() == 0) { ////////////////////////////////// proces 3 -> pobiera dane z procesu 3 poprzez pipe
- pid3 = getpid();
- fprintf(stderr, "Pid3 = %d\n", pid3); fprintf(pids, "Pid3 = %d\n", pid3); fflush(pids);
- signalInit();
- process3();
- }
- while(1)
- pause();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement