step4 . day7 进程间的通信方式
?進(jìn)程間的通信方式:
無名管道(pipe)
有名管道 (fifo)
信號(signal)
system v5的進(jìn)程間通信方式
共享內(nèi)存(share memory)
消息隊(duì)列(message queue)
信號燈集(semaphore set)
套接字(socket)
?
?
1.無名管道
只能用于具有親緣關(guān)系的進(jìn)程之間的通信
?
單工的通信模式,具有固定的讀端和寫端
?
無名管道創(chuàng)建時(shí)會返回兩個(gè)文件描述符,分別用于讀寫管道
創(chuàng)建無名管道
#include <unistd.h>
int pipe(int pfd[2]);
返回值:成功時(shí)返回0,失敗時(shí)返回EOF
參數(shù):pfd 包含兩個(gè)元素的整形數(shù)組,用來保存文件描述符
pfd[0]用于讀管道;pfd[1]用于寫管道
#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):
對應(yīng)管道文件,可用于任意進(jìn)程之間進(jìn)行通信
打開管道時(shí)可指定讀寫方式
通過文件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.信號通信
信號是在軟件層次上對中斷機(jī)制的一種模擬,是一種異步通信方式
linux內(nèi)核通過信號通知用戶進(jìn)程,不同的信號類型代表不同的事件
Linux對早期的unix信號機(jī)制進(jìn)行了擴(kuò)展
進(jìn)程對信號有不同的響應(yīng)方式?
缺省方式
忽略信號
捕捉信號
終端命令 kill -l 可以查看信號
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í)返回原先的信號處理函數(shù),失敗時(shí)返回SIG_ERR
參數(shù) :signo 要設(shè)置的信號類型
handler 指定的信號處理函數(shù): SIG_DFL代表缺省方式; SIG_IGN 代表忽略信號;
signal函數(shù)不發(fā)信號,不阻塞,它使用回掉函數(shù)改變信號的行為。
#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 进程间的通信方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ES简介
- 下一篇: 关于在vue项目中使用wangEdito