From cf8f5b74222e30b1d5c3a3b5e77b9bc95c951c3a Mon Sep 17 00:00:00 2001 From: Dawid Pietrykowski Date: Sun, 22 Jan 2023 22:23:37 +0100 Subject: [PATCH] Fifo, pipes practice --- processes/c-cfifo.c | 62 ++++++++++++++++++++++++++ processes/c-sfifo.c | 75 ++++++++++++++++++++++++++++++++ processes/cfifo.c | 84 ++++++++++++++++++++++++++++++++++++ processes/fork.c | 25 +++++++++++ processes/pipe.c | 53 +++++++++++++++++++++++ processes/popen.c | 14 ++++++ processes/sfifo.c | 103 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 416 insertions(+) create mode 100644 processes/c-cfifo.c create mode 100644 processes/c-sfifo.c create mode 100644 processes/cfifo.c create mode 100644 processes/fork.c create mode 100644 processes/pipe.c create mode 100644 processes/popen.c create mode 100644 processes/sfifo.c diff --git a/processes/c-cfifo.c b/processes/c-cfifo.c new file mode 100644 index 0000000..744b451 --- /dev/null +++ b/processes/c-cfifo.c @@ -0,0 +1,62 @@ +#include +#include +#include /* Definition of AT_* constants */ +#include +#include +#include + +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 main() +{ + int f, f1, f2; + pid_t pid = getpid(); + printf("pid: %d\n", pid); + char kat_1_pid[100]; + char kat_2_pid[100]; + 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 ((f = open(kat, O_WRONLY)) == -1) + { + printf("kat open error\n"); + return; + } + if (mkfifo(kat_1_pid, 0600) == -1) + { + printf("mkfifo1 open error\n"); + return; + } + if (mkfifo(kat_2_pid, 0600) == -1) + { + printf("mkfifo2 open error\n"); + return; + } + write(f, &pid, sizeof(pid_t)); + close(f); + if ((f2 = open(kat_2_pid, O_WRONLY)) == -1) + { + printf("kat2 open error\n"); + return; + } + printf("f2 open\n"); + if ((f1 = open(kat_1_pid, O_RDONLY)) == -1) + { + printf("kat1 open error\n"); + return; + } + printf("f1 open\n"); + double d2, d = 10.0; + write(f2, &d, sizeof(double)); + read(f1, &d2, sizeof(double)); + close(f1); + close(f2); + unlink(kat_1_pid); + unlink(kat_2_pid); + printf("%f\n", d2); +} \ No newline at end of file diff --git a/processes/c-sfifo.c b/processes/c-sfifo.c new file mode 100644 index 0000000..32d0895 --- /dev/null +++ b/processes/c-sfifo.c @@ -0,0 +1,75 @@ +#include +#include +#include /* Definition of AT_* constants */ +#include +#include +#include +#include + +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; +void end() +{ + close(f); + close(f1); + close(f2); + unlink(kat); + exit(0); +} + +int main() +{ + pid_t pid; + char kat_1_pid[100]; + char kat_2_pid[100]; + signal(SIGTERM, end); + signal(SIGINT, end); + if (mkfifo(kat, 0600) == -1) + { + printf("mkfifo open error\n"); + return; + } + if ((f = open(kat, O_RDONLY)) == -1) + { + unlink(kat); + printf("kat open error\n"); + return; + } + 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; + } + 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; + } + 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/cfifo.c b/processes/cfifo.c new file mode 100644 index 0000000..4fca13e --- /dev/null +++ b/processes/cfifo.c @@ -0,0 +1,84 @@ +/* +Klient oparty na kolejkach FIFO +*/ + +#include +#include +#include +#include +#include + +static const char *katalog = "/tmp/FIFO-TMP-SERWER"; + +int main() +{ + pid_t numer_procesu; + int f, f1, f2; + char bufor[1000], *pom = bufor; + + numer_procesu = getpid(); + if ((f = open(katalog, 1)) == -1) + { + perror("Klient: Nie moge ustanowic polaczenia z serwerem. [1]\n"); + return -1; + } + else + { + strcpy(pom, "/tmp/FIFO-TMP-1-"); + pom += strlen(pom); + sprintf(pom, "%d", (int)numer_procesu); + if (mknod(bufor, S_IFIFO | 0600, 0) == -1) /* kolejka do czytania */ + { + perror("Klient: Blad tworzenia kolejki FIFO. [2]\n"); + close(f); + return -1; + } + else + { + *(pom - 2) = '2'; + if (mknod(bufor, S_IFIFO | 0600, 0) == -1) /* kolajka do pisania */ + { + perror("Klient: Blad tworzenia kolejki FIFO. [3]\n"); + close(f2); + close(f); + return -1; + } + write(f, &numer_procesu, sizeof(pid_t)); + close(f); + if ((f1 = open(bufor, O_WRONLY)) == -1) /* kolejka do pisania */ + { + perror("Klient: Blad otwarcia kolejki FIFO. [4]\n"); + close(f); + return -1; + } + else + { + *(pom - 2) = '1'; + if ((f2 = open(bufor, O_RDONLY)) == -1) /* kolajka do czytania */ + { + perror("Klient: Blad otwarcia kolejki FIFO. [5]\n"); + close(f1); + close(f); + return -1; + } + else + { + double a, b; + + printf("Podaj liczbe: "); + scanf("%lf", &a); + write(f1, &a, sizeof(double)); + read(f2, &b, sizeof(double)); + printf("Wynik obliczen %lf^2 = %lf.\n", a, b); + close(f1); + close(f2); + close(f); + unlink(bufor); + *(pom - 2) = '2'; + unlink(bufor); + return 0; + } + } + } + } +} \ No newline at end of file diff --git a/processes/fork.c b/processes/fork.c new file mode 100644 index 0000000..067cfd6 --- /dev/null +++ b/processes/fork.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + pid_t pid; + if ((pid = fork()) != -1) + { + if (pid == 0) + { + printf("dziecko\n"); + char const *arg[2]; + arg[0] = "touch"; + arg[1] = "tt"; + execvp(arg[0], arg); + } + else + { + waitpid(pid); + printf("rodzic\n"); + } + } +} \ No newline at end of file diff --git a/processes/pipe.c b/processes/pipe.c new file mode 100644 index 0000000..449559a --- /dev/null +++ b/processes/pipe.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + int fcp[2]; + int fpc[2]; + pid_t pid; + if (pipe(fcp) == -1) + { + printf("pipe failed\n"); + return -1; + } + if (pipe(fpc) == -1) + { + printf("pipe failed\n"); + return -1; + } + if ((pid = fork()) != -1) + { + if (pid == 0) + { + // child + // char const *arg[3]; + // arg[0] = "bc"; + // arg[1] = "-l"; + // arg[2] = NULL; + close(fpc[1]); + close(fcp[0]); + char *buf = malloc(sizeof(char) * 1000); + const char *prefix = "INPUT: "; + memcpy(buf, prefix, strlen(prefix) + 1); + read(fpc[0], buf + (strlen(prefix)), 1000 - strlen(prefix) - 1); + write(fcp[1], buf, 1000); + // execvp(arg[0], arg); + } + else + { + // parent + close(fpc[0]); + close(fcp[1]); + const char *data = "testdata"; + char *buf = malloc(sizeof(char) * 1000); + // write(fpc[1], data, strlen(data) + 1); + FILE *fd = fopen(fpc[1], "w"); + fprintf(fd, "ddd"); + read(fcp[0], buf, 1000); + printf("%s\n", buf); + } + } +} \ No newline at end of file diff --git a/processes/popen.c b/processes/popen.c new file mode 100644 index 0000000..98ab7cf --- /dev/null +++ b/processes/popen.c @@ -0,0 +1,14 @@ +#include +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + FILE *f; + if ((f = popen("cut -f2 -d ' '", "w")) != -1) + { + fprintf(f, "%s", "vv aaa"); + pclose(f); + } +} \ No newline at end of file diff --git a/processes/sfifo.c b/processes/sfifo.c new file mode 100644 index 0000000..576c1e2 --- /dev/null +++ b/processes/sfifo.c @@ -0,0 +1,103 @@ +/* +Serwer oparty na kolejkach FIFO +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static const char *katalog = "/tmp/FIFO-TMP-SERWER"; +int f, f3; + +void ala(int i) +{ + close(f); + close(f3); + unlink(katalog); + exit(0); +} + +int main() +{ + int f1, f2; + char bufor[1000], *pom = bufor; + pid_t nr_proc, pid; + + signal(SIGTERM, ala); + signal(SIGINT, ala); + if (mknod(katalog, S_IFIFO | 0600, 0) == -1) + { + perror("Serwer: Blad tworzenia kolejki FIFO. [1]\n"); + return -1; + } + else + { + if ((f = open(katalog, O_RDONLY)) == -1 || (f3 = open(katalog, O_WRONLY)) == -1) + { + perror("Serwer: Blad otwarcia kolejki FIFO. [2]\n"); + return -1; + } + else + { + for (;;) + { + read(f, &nr_proc, sizeof(pid_t)); + if ((pid = fork()) == -1) + { + perror("Wywolanie funkcji fork nie powiodlo sie.\n"); + return -1; + } + else + { + if (pid == 0) + { + strcpy(pom, "/tmp/FIFO-TMP-2-"); + pom += strlen(pom); + sprintf(pom, "%d", (int)nr_proc); + printf("%s\n", bufor); + printf("%d\n", nr_proc); + if ((f1 = open(bufor, O_RDONLY)) == -1) /* kolejka do czytania */ + { + perror("Serwer: Blad otwarcia kolejki FIFO. [3]\n"); + close(f); + return -1; + } + else + { + *(pom - 2) = '1'; + if ((f2 = open(bufor, O_WRONLY)) == -1) /* kolejka do pisania */ + { + perror("Serwer: Blad otwarcia kolejki FIFO. [4]\n"); + close(f1); + close(f); + return -1; + } + else + { + double a, b; + + read(f1, &a, sizeof(double)); + b = a * a; + write(f2, &b, sizeof(double)); + } + } + close(f1); + close(f2); + close(f); + return 0; + } + else + { + wait(NULL); + } + } + } + } + } +} \ No newline at end of file