diff --git a/processes/c-cfifo.c b/processes/c-cfifo.c index 744b451..5e6011b 100644 --- a/processes/c-cfifo.c +++ b/processes/c-cfifo.c @@ -38,7 +38,6 @@ int main() return; } write(f, &pid, sizeof(pid_t)); - close(f); if ((f2 = open(kat_2_pid, O_WRONLY)) == -1) { printf("kat2 open error\n"); @@ -54,9 +53,11 @@ int main() double d2, d = 10.0; write(f2, &d, sizeof(double)); read(f1, &d2, sizeof(double)); + printf("d1: %f d2: %f\n", d, d2); close(f1); close(f2); unlink(kat_1_pid); unlink(kat_2_pid); printf("%f\n", d2); + close(f); } \ No newline at end of file diff --git a/processes/c-cipc.c b/processes/c-cipc.c new file mode 100644 index 0000000..cac0ec2 --- /dev/null +++ b/processes/c-cipc.c @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +const char *main_path = "/tmp/serweripcmain"; + +typedef struct DanePid +{ + long mtype; /* Message type. */ + pid_t pid; /* Message text. */ +} dane_pid; + +typedef struct DaneDouble +{ + long mtype; /* Message type. */ + double n; /* Message text. */ +} dane_double; + +int main(int argc, char *argv[]) +{ + key_t key; + ssize_t bytes; + if (access(main_path, F_OK) == -1) + { + perror("file inaccessible"); + return -1; + } + if ((key = ftok(main_path, 1)) == -1) + { + perror("main ftok error"); + return -1; + } + int msg_id; + if ((msg_id = msgget(key, 0600)) == -1) + { + perror("msgget error"); + return -1; + } + dane_pid pid; + pid.pid = getpid(); + pid.mtype = 1; + dane_double test_dane; + test_dane.n = 12; + test_dane.mtype = 2; + if (msgsnd(msg_id, (const void *)&pid, sizeof(pid_t), 0) == -1) + { + perror("pid error"); + return -1; + } + printf("Sent %d bytes pid: %d\n", sizeof(pid_t), pid.pid); + key_t key2; + if ((key2 = ftok(main_path, pid.pid)) == -1) + { + perror("data ftok error"); + return -1; + } + int msg_id_2; + if ((msg_id_2 = msgget(key2, 0600 | IPC_CREAT | IPC_EXCL)) == -1) + { + perror("data msgget error"); + return -1; + } + printf("Opened queue with id %x\n", msg_id_2); + if (msgsnd(msg_id_2, (const void *)&test_dane, sizeof(double), 0) == -1) + { + perror("data send error"); + return -1; + } + printf("Sent %d bytes data: %f\n", sizeof(double), test_dane.n); + if ((bytes = msgrcv(msg_id_2, (void *)&test_dane, sizeof(double), 3, 0)) == -1) + { + perror("msgrcv error"); + msgctl(msg_id, IPC_RMID, NULL); + return -1; + } + printf("Child received %d bytes and value %f\n", bytes, test_dane.n); +} \ No newline at end of file diff --git a/processes/c-sfifo.c b/processes/c-sfifo.c index 32d0895..08160fa 100644 --- a/processes/c-sfifo.c +++ b/processes/c-sfifo.c @@ -10,7 +10,7 @@ static const char *kat = "/tmp/FIFO-TMP-SERWER"; static const char *kat_1 = "/tmp/FIFO-TMP-1-"; static const char *kat_2 = "/tmp/FIFO-TMP-2-"; -int f, f1, f2; +int f, f1, f2, f_temp; void end() { close(f); @@ -27,12 +27,14 @@ int main() char kat_2_pid[100]; signal(SIGTERM, end); signal(SIGINT, end); + strcpy(kat_1_pid, kat_1); + strcpy(kat_2_pid, kat_2); if (mkfifo(kat, 0600) == -1) { printf("mkfifo open error\n"); return; } - if ((f = open(kat, O_RDONLY)) == -1) + if ((f = open(kat, O_RDONLY)) == -1)// || (f_temp = open(kat, O_WRONLY)) == -1) { unlink(kat); printf("kat open error\n"); @@ -40,36 +42,48 @@ int main() } for (;;) { - read(f, &pid, sizeof(pid_t)); - printf("pid: %d\n", pid); - strcpy(kat_1_pid, kat_1); - strcpy(kat_2_pid, kat_2); - sprintf(kat_1_pid + strlen(kat_1), "%d", pid); - sprintf(kat_2_pid + strlen(kat_2), "%d", pid); - // printf(kat_1_pid); - // printf(kat_2_pid); - if ((f2 = open(kat_2_pid, O_RDONLY)) == -1) - { - unlink(kat); - close(f); - printf("kat2 open error\n"); - return; + int readres = read(f, &pid, sizeof(pid_t)); + if(readres == -1){ + printf("read error\n"); + continue; + }else if(readres == 0){ + continue; } - printf("f2 open\n"); - if ((f1 = open(kat_1_pid, O_WRONLY)) == -1) - { - unlink(kat); - close(f2); - close(f); - printf("kat1 open error\n"); - return; + pid_t pid_c; + if((pid_c = fork()) != -1){ + if(pid_c == 0){ + printf("pid: %d\n", pid); + sprintf(kat_1_pid + strlen(kat_1), "%d", pid); + sprintf(kat_2_pid + strlen(kat_2), "%d", pid); + if ((f2 = open(kat_2_pid, O_RDONLY)) == -1) + { + // unlink(kat); + close(f); + printf("kat2 open error\n"); + return -1; + } + printf("f2 open\n"); + if ((f1 = open(kat_1_pid, O_WRONLY)) == -1) + { + // unlink(kat); + close(f2); + close(f); + printf("kat1 open error\n"); + return -1; + } + printf("f1 open\n"); + double d2, d; + read(f2, &d, sizeof(double)); + d2 = d * d; + printf("d1: %f d2: %f\n", d, d2); + write(f1, &d2, sizeof(double)); + close(f1); + close(f2); + close(f); + return 0; + }else{ + wait(pid_c); + } } - printf("f1 open\n"); - double d2, d; - read(f2, &d, sizeof(double)); - d2 = d * d; - write(f1, &d2, sizeof(double)); - close(f1); - close(f2); } } \ No newline at end of file diff --git a/processes/c-sipc.c b/processes/c-sipc.c new file mode 100644 index 0000000..9935054 --- /dev/null +++ b/processes/c-sipc.c @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +const char *main_path = "/tmp/serweripcmain"; + +typedef struct DanePid +{ + long mtype; /* Message type. */ + pid_t pid; /* Message text. */ +} dane_pid; + +typedef struct DaneDouble +{ + long mtype; /* Message type. */ + double n; /* Message text. */ +} dane_double; + +int f; +int msg_id; + +void end() +{ + close(f); + msgctl(msg_id, IPC_RMID, NULL); + exit(0); +} + +int main(int argc, char *argv[]) +{ + signal(SIGTERM, end); + signal(SIGINT, end); + key_t key; + if ((f = open(main_path, O_CREAT | O_RDWR, 0600)) == -1) + { + perror("open main file error"); + return -1; + } + else + { + close(f); + } + if ((key = ftok(main_path, 1)) == -1) + { + perror("main ftok error"); + return -1; + } + if ((msg_id = msgget(key, 0600 | IPC_CREAT)) == -1) + { + perror("msgget error"); + return -1; + } + printf("Created queue with id %d\n", msg_id); + dane_pid pid_dane; + dane_double dane_double; + ssize_t bytes; + for (;;) + { + if ((bytes = msgrcv(msg_id, (void *)&pid_dane, sizeof(pid_t), 1, 0)) == -1) + { + perror("msgrcv error"); + msgctl(msg_id, IPC_RMID, NULL); + return -1; + } + printf("Received %d bytes\n", bytes); + printf("Dane: %d\n", pid_dane.pid); + pid_t pid; + if ((pid = fork()) == -1) + { + msgctl(msg_id, IPC_RMID, NULL); + return -1; + } + if (pid == 0) + { + key_t key; + int f; + if ((key = ftok(main_path, pid_dane.pid)) == -1) + { + perror("child ftok error"); + return -1; + } + int child_msg_id; + if ((child_msg_id = msgget(key, 0600 | IPC_CREAT)) == -1) + { + perror("child msgget error"); + return -1; + } + printf("Created queue with id %d\n", child_msg_id); + if ((bytes = msgrcv(child_msg_id, (void *)&dane_double, sizeof(double), 2, 0)) == -1) + { + perror("msgrcv error"); + msgctl(msg_id, IPC_RMID, NULL); + return -1; + } + printf("Child received %d bytes and value %f\n", bytes, dane_double.n); + dane_double.n = dane_double.n * dane_double.n; + dane_double.mtype = 3; + if (msgsnd(child_msg_id, (const void *)&dane_double, sizeof(double), 0) == -1) + { + perror("data send error"); + return -1; + } + printf("Sent %d bytes data: %f\n", sizeof(double), dane_double.n); + if (msgctl(child_msg_id, IPC_RMID, NULL) == -1) + { + perror("child msgctl IPC_RMID error"); + return -1; + } + } + } + if (msgctl(msg_id, IPC_RMID, NULL) == -1) + { + perror("msgctl IPC_RMID error"); + return -1; + } +} \ No newline at end of file diff --git a/processes/cipc.c b/processes/cipc.c new file mode 100644 index 0000000..d1bff22 --- /dev/null +++ b/processes/cipc.c @@ -0,0 +1,133 @@ +/* +Klient oparty na kolejkach komunikatow dla serwera wspolbieznego. +*/ + +#include +#include +#include +#include +#include +#include +#include + +typedef struct mymesg1 +{ + long mtype; + pid_t pid; +} paczka1; +typedef struct mymesg2 +{ + long mtype; + double melement; +} paczka2; + +int main(int argc, char *argv[]) +{ + int f, f1; + key_t klucz_kolejka; + char *sciezka; + int czy_czekac = 0, liczba_sekund; + pid_t pid = getpid(); + const size_t wielkosc1 = sizeof(paczka1) - sizeof(long); + + if (argc > 1) + { + czy_czekac = 1; + liczba_sekund = strtol(argv[1], NULL, 10); + } + if (argc > 2) + { + sciezka = strndup(argv[2], strlen(argv[2])); + } + else + { + sciezka = strndup("/tmp/roboczy.kolejka_serwer", strlen("/tmp/roboczy.kolejka_serwer")); + } + if ((klucz_kolejka = ftok(sciezka, 1)) == -1) + { + perror("Klient: Blad funkcji ftok.\n"); + free(sciezka); + return -1; + } + else + { + if ((f = msgget(klucz_kolejka, 0600)) == -1) + { + perror("Klient: Blad funkcji msgget.\n"); + return -1; + } + else + { + paczka1 *wiad = (paczka1 *)malloc(sizeof(paczka1)); + ssize_t liczba_bajtow; + + wiad->mtype = 1; + wiad->pid = pid; + if (msgsnd(f, wiad, wielkosc1, 0) == -1) + { + perror("Klient: Blad funkcji msgsnd.\n"); + free(wiad); + return -1; + } + else + { + free(wiad); + if ((klucz_kolejka = ftok(sciezka, pid)) == -1) + { + perror("Klient: Blad funkcji ftok.\n"); + free(sciezka); + return -1; + } + else + { + free(sciezka); + if ((f1 = msgget(klucz_kolejka, 0600 | IPC_CREAT | IPC_EXCL)) == -1) + { + perror("Klient: Blad funkcji msgget.\n"); + return -1; + } + else + { + paczka2 *praca; + const size_t wielkosc2 = sizeof(paczka2) - sizeof(long); + double a = 0; + + printf("Klient: Podaj liczbe: "); + scanf("%lf", &a); + praca = (paczka2 *)malloc(sizeof(paczka2)); + praca->mtype = 2; + praca->melement = a; + if (czy_czekac) + { + printf("Klient: Oczekuje %d sekund na wyslanie wiadomosci do serwera.\n", liczba_sekund); + sleep(liczba_sekund); + } + if (msgsnd(f1, praca, wielkosc2, 0) == -1) + { + free(praca); + msgctl(f1, IPC_RMID, NULL); + return -1; + } + else + { + if ((liczba_bajtow = msgrcv(f1, praca, wielkosc2, 3, 0)) == -1) + { + free(praca); + msgctl(f1, IPC_RMID, NULL); + return -1; + } + else + { + printf("Klient: Odebralem %ld bajtow od serwera.\n", liczba_bajtow); + printf("Klient: Wynik: %lf^2 = %lf.\n", a, praca->melement); + free(praca); + msgctl(f1, IPC_RMID, NULL); + return 0; + } + } + } + } + } + } + } +} \ No newline at end of file diff --git a/processes/sipc.c b/processes/sipc.c new file mode 100644 index 0000000..a5952c4 --- /dev/null +++ b/processes/sipc.c @@ -0,0 +1,182 @@ +/* +Serwer wspolbiezny oparty na kolejekach komunikatow. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct mymesg1 +{ + long mtype; + pid_t pid; +} paczka1; +typedef struct mymesg2 +{ + long mtype; + double melement; +} paczka2; +int f; +paczka1 *wiad; +char *sciezka; +int czy_skasowac = 1; + +void ala(int i) +{ + if (i == SIGTERM || i == SIGINT) + { + printf("Serwer: Koniec pracy serwera.\n"); + free(wiad); + msgctl(f, IPC_RMID, NULL); + if (czy_skasowac) + { + unlink(sciezka); + } + free(sciezka); + exit(0); + } +} + +int main(int argc, char *argv[]) +{ + pid_t pid; + key_t klucz_kolejka; + const size_t wielkosc1 = sizeof(paczka1) - sizeof(long); + ssize_t liczba_bajtow; + int plik = 0; + + signal(SIGTERM, ala); + signal(SIGINT, ala); + sigignore(SIGCHLD); + if (argc > 1) + { + sciezka = strndup(argv[1], strlen(argv[1])); + } + else + { + sciezka = strndup("/tmp/roboczy.kolejka_serwer", strlen("/tmp/roboczy.kolejka_serwer")); + } + if (access(sciezka, F_OK) == 0) + { + czy_skasowac = 0; + } + if (czy_skasowac && ((plik = open(sciezka, O_CREAT | O_EXCL, 0600)) == -1)) + { + fprintf(stderr, "Serwer: Blad utworzenia pliku %s: %s.\n", sciezka, strerror(errno)); + free(sciezka); + return -1; + } + else + { + close(plik); + if ((klucz_kolejka = ftok(sciezka, 1)) == -1) + { + perror("Serwer: Blad funkcji ftok.\n"); + if (czy_skasowac) + { + unlink(sciezka); + } + free(sciezka); + return -1; + } + else + { + if ((f = msgget(klucz_kolejka, 0600 | IPC_CREAT | IPC_EXCL)) == -1) + { + perror("Serwer: Blad funkcji msgget.\n"); + if (czy_skasowac) + { + unlink(sciezka); + } + free(sciezka); + return -1; + } + else + { + for (;;) + { + wiad = (paczka1 *)malloc(sizeof(paczka1)); + if ((liczba_bajtow = msgrcv(f, wiad, wielkosc1, 1, 0)) == -1) + { + perror("Serwer: Blad funkcji msgrcv.\n"); + if (czy_skasowac) + { + unlink(sciezka); + } + free(sciezka); + return -1; + } + else + { + printf("Serwer: Przeczytalem %ld bajtow od klienta na glownej kolejce.\n", liczba_bajtow); + if ((pid = fork()) == -1) + { + perror("Serwer: Wywolanie funkcji fork nie powiodlo sie.\n"); + return -1; + } + else + { + if (pid == 0) + { + int f1; + + if ((klucz_kolejka = ftok(sciezka, wiad->pid)) == -1) + { + free(sciezka); + perror("Serwer, proces potomny: Blad funkcji ftok.\n"); + return -1; + } + else + { + if ((f1 = msgget(klucz_kolejka, 0600)) == -1) + { + perror("Serwer, proces potomny : Nie moge utworzyc kolejki komunikatow dla klienta.\n"); + return -1; + } + else + { + paczka2 *praca = (paczka2 *)malloc(sizeof(paczka2)); + const size_t wielkosc2 = sizeof(paczka2) - sizeof(long); + + if ((liczba_bajtow = msgrcv(f1, praca, wielkosc2, 2, 0)) == -1) + { + perror("Serwer, proces potomny: Blad funkcji msgrcv.\n"); + return -1; + } + else + { + printf("Serwer, proces potomny: Przeczytalem %ld bajtow od klienta.\n", liczba_bajtow); + praca->melement *= praca->melement; + praca->mtype = 3; + if ((liczba_bajtow = msgsnd(f1, praca, wielkosc2, 0)) == -1) + { + perror("Serwer, proces potomny: Blad funkcji msgsnd.\n"); + return -1; + } + else + { + free(praca); + return 0; + } + } + } + } + } + else + { + sigignore(SIGCHLD); + } + } + } + } + } + } + } +} \ No newline at end of file