Fifo, pipes practice

This commit is contained in:
Dawid Pietrykowski 2023-01-22 22:23:37 +01:00
parent 4d1e8ea79b
commit cf8f5b7422
7 changed files with 416 additions and 0 deletions

62
processes/c-cfifo.c Normal file
View File

@ -0,0 +1,62 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> /* Definition of AT_* constants */
#include <unistd.h>
#include <string.h>
#include <stdio.h>
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);
}

75
processes/c-sfifo.c Normal file
View File

@ -0,0 +1,75 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> /* Definition of AT_* constants */
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
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);
}
}

84
processes/cfifo.c Normal file
View File

@ -0,0 +1,84 @@
/*
Klient oparty na kolejkach FIFO
*/
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
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;
}
}
}
}
}

25
processes/fork.c Normal file
View File

@ -0,0 +1,25 @@
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
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");
}
}
}

53
processes/pipe.c Normal file
View File

@ -0,0 +1,53 @@
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
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);
}
}
}

14
processes/popen.c Normal file
View File

@ -0,0 +1,14 @@
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *f;
if ((f = popen("cut -f2 -d ' '", "w")) != -1)
{
fprintf(f, "%s", "vv aaa");
pclose(f);
}
}

103
processes/sfifo.c Normal file
View File

@ -0,0 +1,103 @@
/*
Serwer oparty na kolejkach FIFO
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/uio.h>
#include <signal.h>
#include <string.h>
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);
}
}
}
}
}
}