进程间的通信

本文详细介绍了Linux下的进程间通信方式,包括信号、管道、命名管道、消息队列和共享内存。信号如SIGKILL和SIGSTOP无法被程序捕获,而管道是半双工的,适用于父子或兄弟进程。命名管道FIFO则允许任意两个进程通信。消息队列是内核中存储的消息列表,允许自定义读写。共享内存提供了快速的通信机制,无需数据复制。
摘要由CSDN通过智能技术生成

信号

信号是软件中断的模拟,可以在任何时候发给进程,如果进程处于未执行状态,该信号就由内核保存,直到进程恢复执行再传递给它。
SIGKILL和SEGSTOP是应用程序无法捕捉和忽略的。
几个常用的快捷键和信号:
ctrl + C —— SIGINT 中断信号
ctrl + \ —— SIGQUIT 退出信号
ctrl + Z —— SIGTSTP 进程挂起
functions about signals:

function introduction
int kill(pid_t pid, int sig) send signal to a process
int raise(int sig) send a signal to the caller
unsigned int alarm(unsigned int seconds) set an alarm clock for delivery of a signal
int pause(void) wait for signal
typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler); ANSI C signal handling
int sigemptyset(sigset_t *set) initializes the signal set given by set to empty, with all signals excluded from the set.
int sigfillset(sigset_t *set) initializes set to full, including all signals
int sigaddset(sigset_t *set, int signum) add respectively signal signum from set
int sigdelset(sigset_t *set, int signum) delete respectively signal signum from set
int sigismember(const sigset_t *set, int signum) tests whether signum is a member of set
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset) examine and change blocked signals

下面是子进程给自己发送SIGSTOP信号,用于暂停进程。父进程中waitpid(pid,0,WNOHANG) 用于子进程没有退出立即返回(return immediately if no child has exited.)。

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

int main(){
    pid_t pid = fork();
    if(pid == 0){ // child
         printf("child id is %d\n",getpid());
         raise(SIGSTOP);
    }
    else if(pid > 0){ // father
         printf("father pid is %d\n",getpid());
         if(waitpid(pid,0,WNOHANG) == 0){ 
              int ret = kill(pid,SIGKILL);
              if(ret < 0) perror("ending child failed ");
              else printf("father process end child process.\n");
         }   
    }
    else {
         perror("fork ");
         exit(1);
    }
    return 0; 
}
/*
father pid is 6284
father process end child process.
*/

alarm用于设置多少秒后发出SIGALAR信号,时间片函数控制事件发生的时间点。

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

void handler(int sig){
    printf("hello world\n");
}
int main(){
    int i;
    alarm(3);
    signal(SIGALRM,handler);
    for(i=1;i<=6;i++){
        printf("times is %d\n",i);
        sleep(1);
    }   
    return 0; 
}
/*
times is 1
times is 2
times is 3
hello world
times is 4
times is 5
times is 6
*/

信号的阻塞:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h> 
void handler(){
    printf("signal SIGINT has been blocked for 5 seconds, now it works.\n");
}
int main(){
    signal(SIGINT,handler);
    int i;
    sigset_t set;
    if(sigemptyset(&set) == -1){
        perror("sigemptyset ");
    }
    if(sigaddset(&set,SIGINT) == -1){
        perror("sigaddset ");
    }
    if(sigprocmask(SIG_BLOCK,&set,NULL) == -1){ /* make set blocked */
        perror("sigpromask ");
    }
    raise(SIGINT);
    for(i=0;i<5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值