SK/processes/fifo/c-cfifo.c

63 lines
1.5 KiB
C
Raw Normal View History

2023-01-22 22:23:37 +01:00
#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));
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));
2023-01-28 13:34:00 +01:00
printf("d1: %f d2: %f\n", d, d2);
2023-01-22 22:23:37 +01:00
close(f1);
close(f2);
unlink(kat_1_pid);
unlink(kat_2_pid);
printf("%f\n", d2);
2023-01-28 13:34:00 +01:00
close(f);
2023-01-22 22:23:37 +01:00
}