#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
int main (int argc,char* argv[]){
int pid,pid_s,pid_f,r,w;
char r_buf[512],w_buf[512];
int pfd[2];
strcpy(w_buf,"pingpong");
pipe(pfd);
pid = fork();
if(pid ==0){
pid_s = getpid();
close(pfd[1]);
r = read(pfd[0],r_buf,sizeof(r_buf));
printf("This is son %d read %d bytes , %s\n",pid_s,r,r_buf);
close(pfd[0]);
exit(0);
}//子进程
else if (pid > 0) {
pid_f = getpid();
close(pfd[0]);
w = write(pfd[1],w_buf,sizeof(w_buf));
printf("This is father %d write %d bytes \n",pid_f,w);
close(pfd[1]);
exit(0);
}//父进程
else {
printf("fork error\n");
exit(1);
}//创建进程失败
}
看打印似乎是成功了的,但是在父进程和子进程中加的打印出现乱码了,原因应该是子进程和父进程 互相抢占