父进程接受两个子进程
来源:互联网 发布:网络摄像头改ip软件 编辑:程序博客网 时间:2024/06/06 13:10
#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <sys/wait.h>#include <sys/types.h>int main (){ int pid1,pid2; int fd[2]; char outpipe[100],inpipe[100]; pipe(fd); pid1=fork(); if (pid1==0) { lockf(fd[1],1,0); sprintf(outpipe,"child1 process is sending message"); write (fd[1],outpipe,50); sleep(5); lockf(fd[1],0,0); exit(0); } else { pid2=fork(); if(pid2==0) { lockf(fd[1],1,0); sprintf(outpipe,"child2 process is sending message"); write (fd[1],outpipe,50); sleep(5); lockf(fd[1],0,0); exit(0); } else { wait(NULL); read(fd[0],inpipe,50); printf("%s\n",inpipe); wait(NULL); read(fd[0],inpipe,50); printf("%s\n",inpipe); exit(0); } }}