SK/processes/pipe.c
2023-02-10 17:00:31 +01:00

54 lines
1.3 KiB
C

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