最近学了进程同步,一些东西很容易忽视,故写出以下内容加强记忆,欢迎指正!
首先谈一下管道(即无名管道),它具有以下特点:
1-1 管道特点:
1-2 管道的创建:
#include
int pipe(int pipe_fd[2]);
该函数创建的管道的两端处于一个进程中间,在实际应用中没有太大意义,因此,一个进程在由pipe()创建管道后,一般再fork一个子进程,然后通过管道实现父子进程间的通信(因此也不难推出,只要两个进程中存在亲缘关系,这里的亲缘关系指的是具有共同的祖先,都可以采用管道方式来进行通信)。
1-3 管道读写规则总结
管道两端可分别用描述字fd[0]以及fd[1]来描述,需要注意的是,管道的两端是固定了任务的。即一端只能用于读,由描述字fd[0]表示,称其为管道读端;另一端则只能用于写,由描述字fd[1]来表示,称其为管道写端。如果试图从管道写端读取数据,或者向管道读端写入数据都将导致错误发生。一般文件的I/O函数都可以用于管道,如close、read、write等等。
1-3-1 从管道中读取数据:
如果管道的写端不存在,则认为已经读到了数据的末尾,读函数返回的读出字节数为0; 当管道的写端存在时,如果请求的字节数目大于PIPE_BUF,则返回管道中现有的数据字节数,如果请求的字节数目不大于PIPE_BUF,则返回管道中现有数据字节数(此时,管道中数据量小于请求的数据量);或者返回请求的字节数(此时,管道中数据量不小于请求的数据量
验证如下
#include
#include
#include
main()
{ int pipe_fd[2];
pid_t pid; char r_buf[100];
char w_buf[4]; char* p_wbuf;
int r_num; int cmd;
memset(r_buf,0,sizeof(r_buf));
memset(w_buf,0,sizeof(r_buf));
p_wbuf=w_buf;
if(pipe(pipe_fd)<0)
{ printf("pipe create error\n");
return -1;
}
if((pid=fork())==0)
{ printf("\n");
close(pipe_fd[1]);
sleep(3);//确保父进程关闭写端
r_num=read(pipe_fd[0],r_buf,100);
printf( "read num is %d the data read from the pipe is %d\n",r_num,atoi(r_buf));
close(pipe_fd[0]);
exit();
}
else if(pid>0)
{ close(pipe_fd[0]);//read
strcpy(w_buf,"1234");
if(write(pipe_fd[1],w_buf,4)!=-1)
printf("parent write over\n");
close(pipe_fd[1]);//write
printf("parent close fd[1] over\n");
sleep(10);
}
}
运行结果如下:
parent write over
parent close fd[1] over
read num is 4 the data read from the pipe is 1234
注: 管道写端关闭后,写入的数据将一直存在,直到读出为止.
1-3-2 向管道中写数据:
**只有在管道的读端存在时,向管道中写入数据才有意义。否则,向管道中写入数据的进程将收到内核传来的SIFPIPE信号,应用程序可以处理该信号,也可以忽略(默认动作则是应用程序终止)。
#include
#include
main() { int pipe_fd[2];
pid_t pid;
char r_buf[4];
char* w_buf;
int writenum;
int cmd;
memset(r_buf,0,sizeof(r_buf));
if(pipe(pipe_fd)<0)
{ printf("pipe create error\n"); return -1;
}
if((pid=fork())==0)
{ close(pipe_fd[0]);
close(pipe_fd[1]);
sleep(10);
exit();
}
else if(pid>0)
{
sleep(1); //等待子进程完成关闭读端的操作
close(pipe_fd[0]);//一处
w_buf="111";
if((writenum=write(pipe_fd[1],w_buf,4))==-1)
printf("write to pipe error\n");
else
printf("the bytes write to pipe is %d \n", writenum);
//close(pipe_fd[0]);//二处
close(pipe_fd[1]);
}
}
运行结果会出现段错误
如果把下面的二处打开,关闭一处,则会成功,可以试一下
** 向管道中写入数据时,linux将不保证写入的原子性,管道缓冲区一有空闲区域,写进程就会试图向管道写入数据。如果读进程不读走管道缓冲区中的数据,那么写操作将一直阻塞
#include
#include
#include
main(int argc,char**argv)
{ int pipe_fd[2];
pid_t pid;
char r_buf[4096];
char w_buf[4096*2];
int writenum;
int rnum;
memset(r_buf,0,sizeof(r_buf));
if(pipe(pipe_fd)<0)
{ printf("pipe create error\n");
return -1;
}
if((pid=fork())==0)
{
close(pipe_fd[1]);
while(1)
{
sleep(1);
rnum=read(pipe_fd[0],r_buf,2048);
printf("child: readnum is %d\n",rnum);
memset(r_buf,0,sizeof(r_buf));
} close(pipe_fd[0]);
exit();
}
else if(pid>0)
{
close(pipe_fd[0]);//write
if((writenum=write(pipe_fd[1],w_buf,1024))==-1)
printf("write to pipe error\n");
else
printf("the bytes write to pipe is %d \n", writenum);
if((writenum=write(pipe_fd[1],w_buf,4096))==-1)
printf("write to pipe error\n");
else
printf("the bytes write to pipe is %d \n", writenum);
close(pipe_fd[1]);
}
}
输出结果:the bytes write to pipe is 1024
child: readnum is 2048//此句话可以说明读入数据非原子性
child: readnum is 2048
child: readnum is 1024
child: readnum is 0
child: readnum is 0
~
~