46 lines
764 B
C++
46 lines
764 B
C++
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
#include <sys/wait.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
int pfds[2];
|
||
|
|
||
|
if (pipe(pfds) < 0)
|
||
|
{
|
||
|
perror("pipe");
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
pid_t child = fork();
|
||
|
if (child == 0)
|
||
|
{
|
||
|
close(pfds[1]);
|
||
|
|
||
|
char buffer[4096];
|
||
|
size_t nread = read(pfds[0], buffer, sizeof(buffer) - 1);
|
||
|
buffer[nread] = 0;
|
||
|
close(pfds[0]);
|
||
|
|
||
|
puts(buffer);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
else if (child == -1)
|
||
|
{
|
||
|
perror("fork");
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
close(pfds[0]);
|
||
|
|
||
|
const char* string = "Hello from a child process who just received this message from its parent!";
|
||
|
write(pfds[1], string, strlen(string));
|
||
|
close(pfds[1]);
|
||
|
|
||
|
wait(NULL);
|
||
|
|
||
|
return 0;
|
||
|
}
|