SK/processes/fifo/c2-cfifo.c

68 lines
1.6 KiB
C
Raw Normal View History

2023-02-10 17:00:31 +01:00
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
const char *path = "/tmp/fifo-server-432423";
char path_pid_1[500];
char path_pid_2[500];
const char *suf_1 = "-1-";
const char *suf_2 = "-2-";
void handle_error(char *txt)
{
perror(txt);
exit(1);
}
int main()
{
int m_w, m_r;
int db_w, db_r;
char pid_str[20];
int m_fifo;
int pid = getpid();
strcpy(path_pid_1, path);
strcpy(path_pid_2, path);
strcpy(path_pid_1, strcat(path_pid_1, suf_1));
strcpy(path_pid_2, strcat(path_pid_2, suf_2));
sprintf(pid_str, "%d", pid);
strcpy(path_pid_1, strcat(path_pid_1, pid_str));
strcpy(path_pid_2, strcat(path_pid_2, pid_str));
if (access(path_pid_1, F_OK) == 0)
unlink(path_pid_1);
if (access(path_pid_2, F_OK) == 0)
unlink(path_pid_2);
if ((m_fifo = mkfifo(path_pid_1, 0600)) == -1)
handle_error("pid1 mkfifo");
if ((m_fifo = mkfifo(path_pid_2, 0600)) == -1)
handle_error("pid2 mkfifo");
if ((m_w = open(path, O_WRONLY)) == -1)
handle_error("main open");
printf("writing pid\n");
write(m_w, (void *)&pid, sizeof(pid_t));
if ((db_w = open(path_pid_1, O_WRONLY)) == -1)
handle_error("db_w open");
if ((db_r = open(path_pid_2, O_RDONLY)) == -1)
handle_error("db_r open");
double db = 2.21212;
printf("writing db\n");
write(db_w, (void *)&db, sizeof(double));
close(db_w);
printf("reading db\n");
read(db_r, (void *)&db, sizeof(double));
close(db_r);
printf("%f\n", db);
return 0;
}