我的测试应用程序是
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
int main(int argc, char *argv[], char *envp[]) {
int fd[2];
if(pipe(fd) < 0) {
printf("Can\'t create pipe\n");
exit(-1);
}
pid_t fpid = fork();
if (fpid == 0) {
close(0);
close(fd[1]);
char *s = (char *) malloc(sizeof(char));
while(1) if (read(fd[0], s, 1)) printf("%i\n", *s);
}
close(fd[0]);
char *c = (char *) malloc(sizeof(char));
while (1) {
if (read(0, c, 1) > 0) write(fd[1], c, 1); …Run Code Online (Sandbox Code Playgroud)