78 lines
1.9 KiB
C
78 lines
1.9 KiB
C
|
#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-";
|
||
|
int m_fifo, m_w, m_r;
|
||
|
|
||
|
void handle_error(char *txt)
|
||
|
{
|
||
|
close(m_w);
|
||
|
close(m_r);
|
||
|
perror(txt);
|
||
|
exit(1);
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
int p;
|
||
|
if (access(path, F_OK) == 0)
|
||
|
unlink(path);
|
||
|
|
||
|
if ((m_fifo = mkfifo(path, 0600)) == -1)
|
||
|
handle_error("main mkfifo");
|
||
|
|
||
|
if ((m_r = open(path, O_RDONLY)) == -1 || (m_w = open(path, O_WRONLY)) == -1)
|
||
|
{
|
||
|
unlink(path);
|
||
|
handle_error("main open");
|
||
|
}
|
||
|
|
||
|
printf("otwieranie kolejki\n");
|
||
|
while (1)
|
||
|
{
|
||
|
pid_t client_pid;
|
||
|
read(m_r, (void *)&client_pid, sizeof(pid_t));
|
||
|
int pid;
|
||
|
int db_w, db_r;
|
||
|
if ((pid = fork()) == -1)
|
||
|
handle_error("fork");
|
||
|
if (pid == 0)
|
||
|
{
|
||
|
char pid_str[20];
|
||
|
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", client_pid);
|
||
|
strcpy(path_pid_1, strcat(path_pid_1, pid_str));
|
||
|
strcpy(path_pid_2, strcat(path_pid_2, pid_str));
|
||
|
|
||
|
if ((db_r = open(path_pid_1, O_RDONLY)) == -1)
|
||
|
handle_error("db_r open");
|
||
|
if ((db_w = open(path_pid_2, O_WRONLY)) == -1)
|
||
|
handle_error("db_w open");
|
||
|
|
||
|
double db;
|
||
|
read(db_r, (void *)&db, sizeof(double));
|
||
|
printf("%f\n", db);
|
||
|
db *= 2;
|
||
|
write(db_w, (void *)&db, sizeof(double));
|
||
|
printf("%f\n", db);
|
||
|
close(db_r);
|
||
|
close(db_w);
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|