1.参照课本例题【5-20】,利用匿名管道实现父子进程间通信,发送的消息为i am your name。注意:重点是对管道通信给出各种同步、阻塞情况的分析说明。
#include < unistd.h>
#include <error.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int pipe_fd[2];
if(pipe(pipe_fd)<0)
{
printf("pipe create error\n");
return -1;//if ?
}
printf("pipe create success\n");
if(fork()>0)
{
//father
int r;
char buf[15+1];
printf("* * * Want to read from son\n");
r=read(pipe_fd[0],buf,15); //从管道读端数据到buf
buf[r]=0;
printf("* * * FATHER Got strings: %s\n",buf);
}else
{
//son
const char * test="i am zby";
printf("Son sleep:\n");
sleep(5);//人为加入sleep控制使子进程比父进程后执行
printf("SonWrite after sleep:%s\n",test);//2
write(pipe_fd[1],test,strlen(test));//从管道写入端写入test
}
close(pipe_fd[0]);
close(pipe_fd[1]);
}
父进程通过pipe()调用申请到了第三方缓冲区该调用必须是在fork之前,才能保证fork通过克隆把读写端地址复制给子进程。子进程通过write端写入,父进程通过read 端从管道读出。Sleep(5)使得父进程比子进程先执行,但是父进程的read需要等待子进程的write执行后才能读出数据,这样才是完成一次单向通信。
2.参照课本例题【5-22】,利用命名管道(文件命名为自己名字全称)实现非亲缘关系的两个进程通信,发送的消息为i am your name。注意:重点是对管道通信给出各种同步、阻塞情况的分析说明。
1)读管道
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc,char * argv[])
{
if(argc!=2){
printf("not enough params,give pipefile name\n");
exit(1);
}
char *pipefile;
pipefile=argv[1];
char buf[100];
int fd,i;
printf("read open zbypipe!!\n");
fd=open(pipefile,O_RDONLY,0);
if(fd<0){
printf("no such zbypipe file!!\n");
exit(-1);}
printf("OK!zbypipe opened for read!\n");
i=read(fd,buf,100);
buf[i]=0;
printf("OK!readed from zbypipe :%s!\n",buf);
return 0;
}
2)写管道
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc,char *argv[])
{
if(argc!=2){
printf("not enough params,give pipefile name\n");
exit(1);}
char *pipefile;
pipefile=argv[1];
int fd;
char *teststr="i am zby";
printf("OPEN zbypipe--% for write! \n",pipefile);
fd=open(pipefile,O_WRONLY,0);
printf("OK! zbypipe--% opened for write! \n",pipefile);
sleep(5);
write(fd,teststr,strlen(teststr));
printf("OK! zbypipe write successfully!\n");
exit(0);
}
先执行Rfifo读方,执行第一句话会堵塞,因为管道默认要求两端同时存在时才可以执行,现在写端没有执行open,因此读端会堵塞。
接着执行Wfifo,先输出第一句,执行open唤醒之前open堵塞的读端,输出后两句。写端Sleep(5)休息后才可以执行的。这影响读端的执行,因此读端也停留5秒
3.试对课本【例5-24】改写,编程实现3个进程通信,一个负责写入,另外两个负责读出,体验3方的通信过程及效果。
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
int r,i,p1,p2,fd[2];
char buf[10],s[50];
pipe(fd);
while((p1=fork())==-1);
if(p1==0)
{
lockf(fd[0],1,0);
printf("child_1 process p1 Read!\n");
for(i=1; i<=10; i++)
{
if((r=read(fd[0],s,10))==-1)
printf("can’t read pipe\n");
else
printf("%s",s);
}
printf("child_1 Read end.\n");
exit(0);
}
else
{
while((p2=fork())==-1);
if(p2==0)
{
printf("child_2 process p1 Read!\n");
for(i=1; i<=10; i++)
{
if((r=read(fd[0],s,10))==-1)
printf("can't read pipe\n");
else
printf("%s",s);
}
printf("child_2 Read end.\n");
exit(0);
}
lockf(fd[1],1,0);
sprintf(buf, "AAAAAAAAAA\n");
printf("father write(AAAAAAAAAA):\n");
for(i=1; i<=10; i++)
{
write(fd[1],buf,10);
sleep(1);
}
sprintf(buf, "BBBBBBBBBB\n");
printf("father write(BBBBBBBBBB):\n");
for(i=1; i<=10; i++)
{
write(fd[1],buf,10);
sleep(1);
}
printf("father write end.\n");
lockf(fd[1],0,0);
wait(0);
wait(0);
exit(0);
}
}