C
40
childin
Guest on 1st June 2022 01:27:08 AM
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#define BUFSIZE 8192
char buf[BUFSIZE];
int ifd[2] = {0, 0}; /* ifd[0] is read, ifd[1] is write */
int
main(int argc, char *argv[])
{
int status;
int rc;
pipe(ifd);
rc = fork();
if (rc < 0) {
} else if (rc == 0) {
close(0);
close(ifd[1]);
dup2(ifd[0],0);
argv++;
execve(argv[0], argv, 0);
} else {
size_t count = 5;
close(ifd[0]);
count = read(0, buf, count);
if (count == -1) {
perror("parent read from stdin failed");
}
count = write(ifd[1], buf, count);
if (count == -1) {
perror("parent write to child failed");
}
waitpid(rc, &status, 0);
printf("child %d exited with status %d\n", rc
,
WEXITSTATUS(status));
}
}