マルチプロセス通信方式一:パイプ(PIPE)
プロセス通信(IPC)のいくつかの方式と比較
執筆:黄顕国080416
せっかくの暇なので、時間を割いてプロセス通信の知識を勉強して、ここ数日の勉強をまとめて、忘れたときに参考にします.
プロセス通信方式:
Linuxシステムは、3つのシステムのプロセス通信モードを継承しています.
1、システムV IPCに基づく
2、UNIX IPCに基づく
3.POSIX IPCに基づく
同時にsocketプロセス間通信も含まれていますが、これは異なるプロセッサシステム間のネットワーク通信方式であり、私の関心ではありません.
方式一:パイプ(PIPE)
パイプは無名パイプと有名パイプの2種類に分かれています
1、無名パイプ.
無名パイプは、親関係を持つ親子プロセス、サブプロセス間の通信に使用されます.その実現関数にはint pipe(int fd[2])がある.
//fd[2]は記述子配列であり、1つのリード記述子と1つのライト記述子を含み、パイプ通信を使用するときに不要なリードまたはライト記述子を閉じ、一方向のリードまたはライトパイプを構築し、readとwriteで操作ファイルのように操作すればよい.
図は、プロセス1からプロセス2までのリードパイプです.
以下は私が書いたpipeの検証プログラムで、それぞれ親プロセスと親子プロセスでパイプにデータを書き、サブプロセスとサブプロセスでデータを読み、各サブプロセスのsleep時間を変更して所望の同期を実現しようとすると、結果が面白いことに気づきます.注:サブプロセスを作成すると、親プロセスのパイプがコピーされます.
執筆:黄顕国080416
せっかくの暇なので、時間を割いてプロセス通信の知識を勉強して、ここ数日の勉強をまとめて、忘れたときに参考にします.
プロセス通信方式:
Linuxシステムは、3つのシステムのプロセス通信モードを継承しています.
1、システムV IPCに基づく
2、UNIX IPCに基づく
3.POSIX IPCに基づく
同時にsocketプロセス間通信も含まれていますが、これは異なるプロセッサシステム間のネットワーク通信方式であり、私の関心ではありません.
方式一:パイプ(PIPE)
パイプは無名パイプと有名パイプの2種類に分かれています
1、無名パイプ.
無名パイプは、親関係を持つ親子プロセス、サブプロセス間の通信に使用されます.その実現関数にはint pipe(int fd[2])がある.
//fd[2]は記述子配列であり、1つのリード記述子と1つのライト記述子を含み、パイプ通信を使用するときに不要なリードまたはライト記述子を閉じ、一方向のリードまたはライトパイプを構築し、readとwriteで操作ファイルのように操作すればよい.
図は、プロセス1からプロセス2までのリードパイプです.
以下は私が書いたpipeの検証プログラムで、それぞれ親プロセスと親子プロセスでパイプにデータを書き、サブプロセスとサブプロセスでデータを読み、各サブプロセスのsleep時間を変更して所望の同期を実現しようとすると、結果が面白いことに気づきます.注:サブプロセスを作成すると、親プロセスのパイプがコピーされます.
/*******************************************************************************************/
//pipe.c
//frome the example, we can see:
// pipe , , , read
// write , ;
// 。
/*****************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main()
{
int pipe_fd[2];
char buf_r[100];
char buf_rr[100];
if(pipe(pipe_fd)<0)
{
printf("pipe create error
");
return -1;
}
else
printf("pipe create success
");
printf("pipe_fd[0]=%d,pipe_fd[1]=%d
",pipe_fd[0],pipe_fd[1]);
if(fork()==0)//
{
close(pipe_fd[1]);//
printf("fork()");
//sleep(2);
if(fork()==0)//
{
if(read(pipe_fd[0],buf_r,5)>0)
printf("BUF_R: in child child process,read from the pipe is %s
",buf_r);
close(pipe_fd[0]);
exit(0);
}
Else //
{
if(read(pipe_fd[0],buf_rr,3)>0)
printf("BUF_RR: in child parent process,read from the pipe is %s
",buf_rr);
close(pipe_fd[0]);
exit(0);
}
}
else //
{
close(pipe_fd[0]);
sleep(5);
if(write(pipe_fd[1],"Hello ",5)!=-1)
printf("write1 parent pipe success
");
if(fork()==0) //
{
if(write(pipe_fd[1],"PIPE",5)!=-1)
printf("write parent child pipe success");
close(pipe_fd[1]);
exit(0);
}
close(pipe_fd[1]);
exit(0);
}
}
2、
。 :
int mkfifo(const char *filename, mode_t mode)
// filename , , 。
mkfifo 。Fifo_read.c ,fifo_write.c 。 sleep , 。
gcc 。
//---------------------------------------------------------------------------------------------------
//fifo_read.c
// ,
//int mkfifo(const char *filename, mode_t mode)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#define FIFO "/home/huang/myfifo"
int main(int argc,char **argv)
{
int fd;
int nread;
char buf_r[100];
if(mkfifo("/home/huang/myfifo",O_CREAT|O_EXCL)<0)//&&(errno!=EEXIST))
{
perror("mkfifo:");
printf("cann't create fifoserver
");
return -1;
}
printf("Preparing for reading bytes...
");
fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);
if(fd==-1)
{
perror("open!
");
exit(1);
}
while(1)
{
memset(buf_r,0,sizeof(buf_r));
if((nread=read(fd,buf_r,sizeof(buf_r)))==-1)
{
if(errno==EAGAIN)
printf("no data yet
");
}
printf("read %s from FIFO
",buf_r);
sleep(1);
}
pause();
unlink(FIFO);
}
//------------------------------------------------------------------------------------------------------------------
//fifo_write.c
// ,
//int mkfifo(const char *filename, mode_t mode)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#define FIFO "/home/huang/myfifo"
int main(int argc, char **argv)
{
int fd;
char w_buf[100];
int nwrite;
if(argc==1)
{
printf("please send some message
");
exit(1);
}
fd=open(FIFO,O_WRONLY|O_NONBLOCK,0);
if(fd==-1)
{
if(errno==ENXIO)
printf("open error;no reading process
");
perror("open:");
return -1;
}
memset(w_buf,'a',sizeof(w_buf));
printf("sizeof(w_buf)=%d
",sizeof(w_buf));
while(1)
{
if((nwrite=write(fd,w_buf,strlen(w_buf)))==-1)
{
if(errno==EAGAIN)
printf("The FIFO has not been write yet.
");
perror("write");
// else
// printf("error in writting!
");
}
else
printf("write %s to the FIFO
",w_buf);
sleep(2);
}
close(fd);
}