SK/processes/c-sfifo.c
2023-01-22 22:23:37 +01:00

75 lines
1.7 KiB
C

#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);
}
}