step4 . day7 进程间的通信方式
?進程間的通信方式:
無名管道(pipe)
有名管道 (fifo)
信號(signal)
system v5的進程間通信方式
共享內存(share memory)
消息隊列(message queue)
信號燈集(semaphore set)
套接字(socket)
?
?
1.無名管道
只能用于具有親緣關系的進程之間的通信
?
單工的通信模式,具有固定的讀端和寫端
?
無名管道創建時會返回兩個文件描述符,分別用于讀寫管道
創建無名管道
#include <unistd.h>
int pipe(int pfd[2]);
返回值:成功時返回0,失敗時返回EOF
參數:pfd 包含兩個元素的整形數組,用來保存文件描述符
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.有名管道
有名管道具有如下特點:
對應管道文件,可用于任意進程之間進行通信
打開管道時可指定讀寫方式
通過文件IO操作,內容存放在內存中
管道的創建
#include <unistd.h>
#include <fcntl.h>
int mkfifo(const char *path, mode_t mode);
返回值:成功時返回0,失敗時返回EOF
參數: path 創建的管道文件路徑
mode 管道文件的權限,如0666
使用: open()
read()
write()
進程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;
}
進程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.信號通信
信號是在軟件層次上對中斷機制的一種模擬,是一種異步通信方式
linux內核通過信號通知用戶進程,不同的信號類型代表不同的事件
Linux對早期的unix信號機制進行了擴展
進程對信號有不同的響應方式?
缺省方式
忽略信號
捕捉信號
終端命令 kill -l 可以查看信號
signal 函數定義
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)
返回值:成功時返回原先的信號處理函數,失敗時返回SIG_ERR
參數 :signo 要設置的信號類型
handler 指定的信號處理函數: SIG_DFL代表缺省方式; SIG_IGN 代表忽略信號;
signal函數不發信號,不阻塞,它使用回掉函數改變信號的行為。
#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);
}
}
?
轉載于:https://www.cnblogs.com/huiji12321/p/11343161.html
總結
以上是生活随笔為你收集整理的step4 . day7 进程间的通信方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ES简介
- 下一篇: step5 . day1 网络编程基础知