step4 . day7 进程间的通信方式
?進(jìn)程間的通信方式:
無(wú)名管道(pipe)
有名管道 (fifo)
信號(hào)(signal)
system v5的進(jìn)程間通信方式
共享內(nèi)存(share memory)
消息隊(duì)列(message queue)
信號(hào)燈集(semaphore set)
套接字(socket)
?
?
1.無(wú)名管道
只能用于具有親緣關(guān)系的進(jìn)程之間的通信
?
單工的通信模式,具有固定的讀端和寫(xiě)端
?
無(wú)名管道創(chuàng)建時(shí)會(huì)返回兩個(gè)文件描述符,分別用于讀寫(xiě)管道
創(chuàng)建無(wú)名管道
#include <unistd.h>
int pipe(int pfd[2]);
返回值:成功時(shí)返回0,失敗時(shí)返回EOF
參數(shù):pfd 包含兩個(gè)元素的整形數(shù)組,用來(lái)保存文件描述符
pfd[0]用于讀管道;pfd[1]用于寫(xiě)管道
#include<stdio.h>
#include <unistd.h>
#include <string.h>
#define N 32
int main(int argc, const char *argv[])
{
pid_t pid;
int pfd[2];
char buf[N];
if(pipe(pfd)<0){
perror("pipe");
return -1;
}
pid = fork();
if(pid < 0){
perror("fork");
return -1;
}else if(pid == 0){
while(1){
strcpy(buf, "hello world from child process");
write(pfd[1],buf,strlen(buf));
sleep(1);
}
}
else{
while(1){
memset(buf,0,strlen(buf));
read(pfd[0],buf,N);
printf("recevie :%s\n",buf);
}
}
return 0;
}
2.有名管道
有名管道具有如下特點(diǎn):
對(duì)應(yīng)管道文件,可用于任意進(jìn)程之間進(jìn)行通信
打開(kāi)管道時(shí)可指定讀寫(xiě)方式
通過(guò)文件IO操作,內(nèi)容存放在內(nèi)存中
管道的創(chuàng)建
#include <unistd.h>
#include <fcntl.h>
int mkfifo(const char *path, mode_t mode);
返回值:成功時(shí)返回0,失敗時(shí)返回EOF
參數(shù): path 創(chuàng)建的管道文件路徑
mode 管道文件的權(quán)限,如0666
使用: open()
read()
write()
進(jìn)程1
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#define N 32
int main(int argc, const char *argv[])
{
int re;
unlink("myfifo");
re= mkfifo("myfifo",0666);
if(re<0){
perror("makfifo");
return -1;
}
char buf1[N];
char buf2[N];
strcpy(buf1,"hello world");
int fifofd;
fifofd = open("myfifo",O_RDWR);
while(1){
write(fifofd,buf1,N);
sleep(1);
memset(buf2,0,N);
re = read(fifofd,buf2,N);
if(re>0){
printf("%s\n",buf2);
}
}
return 0;
}
進(jìn)程2
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#define N 32
int main(int argc, const char *argv[])
{
int re;
char buf1[N];
char buf2[N];
strcpy(buf2,"how are you");
int fifofd;
fifofd = open("myfifo",O_RDWR);
while(1){
memset(buf1,0,N);
re = read(fifofd,buf1,N);
if(re>0){
printf("%s\n",buf1);
}
write(fifofd,buf1,N);
sleep(1);
}
return 0;
}
3.信號(hào)通信
信號(hào)是在軟件層次上對(duì)中斷機(jī)制的一種模擬,是一種異步通信方式
linux內(nèi)核通過(guò)信號(hào)通知用戶(hù)進(jìn)程,不同的信號(hào)類(lèi)型代表不同的事件
Linux對(duì)早期的unix信號(hào)機(jī)制進(jìn)行了擴(kuò)展
進(jìn)程對(duì)信號(hào)有不同的響應(yīng)方式?
缺省方式
忽略信號(hào)
捕捉信號(hào)
終端命令 kill -l 可以查看信號(hào)
signal 函數(shù)定義
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
或者void (*signal(int signo, void (*handler)(int)))(int);
定義分解
void (* xxxx )(int);
xxxx = signal(int signo, aaa)
aaa = void (*handler)(int)
返回值:成功時(shí)返回原先的信號(hào)處理函數(shù),失敗時(shí)返回SIG_ERR
參數(shù) :signo 要設(shè)置的信號(hào)類(lèi)型
handler 指定的信號(hào)處理函數(shù): SIG_DFL代表缺省方式; SIG_IGN 代表忽略信號(hào);
signal函數(shù)不發(fā)信號(hào),不阻塞,它使用回掉函數(shù)改變信號(hào)的行為。
#include <stdio.h>
#include <signal.h>
void handle(int sig){
if(sig == SIGINT){
printf("I got a ctrl+C signal\n");
}else if(sig == SIGQUIT){
printf("I got a quit signal\n");
}else{
printf("other siganl\n");
}
}
int main(){
signal(SIGINT,handle);
signal(SIGQUIT,handle);
signal(SIGHUP,handle);
while(1){
sleep(1);
}
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/huiji12321/p/11343161.html
總結(jié)
以上是生活随笔為你收集整理的step4 . day7 进程间的通信方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ES简介
- 下一篇: step5 . day1 网络编程基础知