代码是这样的 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #define k 1024 #define WRITELEN (128*k) int main() { int result = -1; int fd[2]; int nbytes; pid_t pid; char string[WRITELEN] = "你好,管道"; char readbuffer[10*k]; result = pipe(fd); if(-1 == result) { printf("建立管道失败 \n"); return -1; } pid = fork(); if(-1 == pid) { printf("fork 进程失败 \n"); return -1; } else if(0 == pid) { int write_size = WRITELEN; close(fd[0]); while(write_size >= 0) { result = write(fd[1],string,write_size); //好像这里出问题了,郁闷啊,谢谢各位大牛 if(result > 0) { write_size -= result; printf("写入%d个字符,剩余%d个字符 \n",result,write_size); } else { sleep(10); } } return 0; } else { close(fd[1]); while(1) { nbytes = (fd[0],readbuffer,sizeof(readbuffer)); if(nbytes <= 0) { printf("没有数据写入咯 \n"); break; } printf("接收到%d个字符,内容是:%s \n",nbytes,readbuffer); } } return 0; } |
|
好像没有写入数据,后面就直接开始读了,然后无线循环读………………
|
|
40分 |
nbytes = (fd[0],readbuffer,sizeof(readbuffer)); //改为 nbytes = read(fd[0],readbuffer,sizeof(readbuffer)); |
果然如此,大牛,我太粗心大意了 |