最近在学操作系统,新学到了一些知识,记在下面。
以下是相关源代码:
#include"sys/types.h"
#include"sys/file.h"
#include"unistd.h"
#include"string.h"
#include"stdlib.h"
#include"stdio.h"
#include"wait.h"
char r_buf[4]; //read buffer
char w_buf[4]; //write buffer
int pipe_fd[2];
pid_t pid1,pid2,pid3,pid4;
int producer(int id);
int consumer(int id);
int main(int argc,int**argv){
if(pipe(pipe_fd)<0){
printf("pipe create error\n");
exit(-1);
}
else{
printf("pipe is created successfully\n");
if((pid1=fork())==0){
producer(1);
printf("child pid%d\n",getpid());
}
else if(pid1>0){
printf("parent pid%d,children pid%d\n",getpid(),pid1);
}
printf("after fork() 1\n");
if((pid2=fork())==0){
producer(2