進程之間的通信作用
1、數據傳輸 :一個進程需要將他的數據傳到其他進程
2、資源共享
3、進程通知事件
4、進程控制 :有些進程完全控制另一個進程的執行,如調試狀態啊
我們需要完全控制他的每一步操作;
通信發展歷史
Linux進程間的通信IPC由以下幾個部分發展而來:
1、UNIX進程之間的通信
2、基於system v進程間的通信
3、POSIX進程之間的通信(portable operating system interface)
現在Linux使用的進程間的通信方式包括;
1、管道和有名管道
2、信號
3、消息隊列
4、共享內存
5、信號量
6、套接字
分類:無名管道和有名管道:前者用於父進程和子進程間的通信,後者可用於運行於同一系統
int pipe(int filedis[2])函數創建:
當一個管道建立時,他會創建兩個文件描述符:
filedis[0]用於讀管道,filedis[1]用於寫管道。
管道的讀寫
管道用於不同進程間的通信。通常先創建一個管道,在通過fork函數
創建一個子進程,該子進程會繼承父進程所創建的管道。
調用fork()之前要先調用pipe(),否則子進程將不會繼承文件描述符。
/******************無名管道的實現*******************************/
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int pipe_fd[2];
pid_t pid;
char buf_r[100];
char* p_wbuf;
int r_num;
memset(buf_r,0,sizeof(buf_r));
/*創建管道 fork之前先pipl*/
if(pipe(pipe_fd)<0)
{
printf("pipe create error\n");
return -1;
}
/*創建子進程*/
if((pid=fork())==0) //子進程 OR 父進程?
{
printf("\n");
close(pipe_fd[1]);
sleep(2); /*爲什麼要睡眠*/
if((r_num=read(pipe_fd[0],buf_r,100))>0)
{
printf( "%d numbers read from the pipe is %s\n",r_num,buf_r);
}
close(pipe_fd[0]);
exit(0);
}
else if(pid>0)
{
close(pipe_fd[0]);
if(write(pipe_fd[1],"Hefrfro",5)!=-1)
printf("parent write1 Hello!\n");
if(write(pipe_fd[1]," 程度vvfvf",5)!=-1)
printf("parent write2 常潤茶ge!\n");
close(pipe_fd[1]);
sleep(3);
waitpid(pid,NULL,0); /*等待子進程結束*/
exit(0);
}
return 0;
}
命名(FIFO)
#include<sys/types.h>
#include<sys/stat.h>
int mkfifo(const char *pathname,mode_t mod)
pathname:FIFO 文件名
mode :屬性一旦創建FIFO,就可以用open打開他,另外close read write都可以
當打開FIFO時,非阻塞(O_NONBLOCK)
將對以後的讀寫產生如下影響
1、沒有使用O_NONBLOCK:當訪問進程無法滿足時,進程將阻塞,否則就立即報錯。
/******************有名管道讀操作***************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO "/tmp/myfifo"
main(int argc,char** argv)
{
char buf_r[100];
int fd;
int nread;
/* 創建管道 */
if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST)) //FIFO 管道路徑
printf("cannot create fifoserver\n");
printf("Preparing for reading bytes...\n");
memset(buf_r,0,sizeof(buf_r)); // 爲buf_r分配內存
/* 打開管道 */
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,100))==-1) 如果管道數據沒有數據了,就打印no data yet
{
if(errno==EAGAIN)
printf("no data yet\n");
}
printf("read %s from FIFO\n",buf_r);
sleep(1);
}
pause(); /*暫停,等待信號*/
unlink(FIFO); //刪除文件
}
/**************有名管道寫操作*************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO_SERVER "/tmp/myfifo" //注意這裏兩個管道都是同一地址。
main(int argc,char** argv) //if you dont understand this looking before articles
{
int fd;
char w_buf[100]; // define the cpcity of shu zu
int nwrite;
/*打開管道*/
fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);
if(argc==1)
{
printf("Please send something\n");
exit(-1);
}
strcpy(w_buf,argv[1]); // 把輸入的參數放入到buf裏面;
/* 向管道寫入數據 */
//nwrite=write(fd,w_buf,100)) ; 可加可不加!me not using!
if((nwrite=write(fd,w_buf,100))==-1) //在這裏把這個函數綜合寫了,如果不習慣可以獨立一行創建
{
if(errno==EAGAIN)
printf("The FIFO has not been read yet.Please try later\n");
}
else
printf("write %s to the FIFO\n",w_buf);
}
end!