进程通信 --- 信号 -- 练习

进程通信 — 信号 – 练习

使用setitimer每秒打印一次hello, world

#include<stdio.h>
#include<sys/time.h>
#include<signal.h>

void catch_sig(int num)
{
    printf("hello, world\n");
}

int main()
{
    struct sigaction sigAct;
    sigAct.sa_flags = 0;
    sigemptyset(&sigAct.sa_mask);
    sigAct.sa_handler = catch_sig;
    // 捕捉信号
    sigaction(SIGALRM,&sigAct,NULL);
    
    // 设置闹钟
    struct itimerval itm = {{1,0},{1,0}};
    setitimer(ITIMER_REAL,&itm,NULL);
    
    while(1);
    return 0;
}

运行结果 :

[dai@localhost test]$ ./01test
hello, world
hello, world
hello, world
hello, world
^Z
[8]+ 已停止 ./01test

利用SIGUSR1和SIGUSR2在父子进程之间进行消息传递, 实现父子进程交替报数

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


int nums= 0;
int flag = 1;
void catch_sig_usr1(int num)
{
    printf("父进程:%d\n",nums);
    nums = nums + 2;
    flag = 1;
    sleep(1);
}

void catch_sig_usr2(int num)
{
    printf("子进程:%d\n",nums);
    nums = nums + 2;
    flag = 1;
    sleep(1);
}

int main()
{
    pid_t pid = fork();
    if(pid == 0){
        // 子进程
        struct sigaction sigAct;
        sigAct.sa_flags = 0;
        sigemptyset(&sigAct.sa_mask);
        sigAct.sa_handler = catch_sig_usr1;
        sigaction(SIGUSR1,&sigAct,NULL);

        while(1){
            sleep(2);
            if(flag == 1 )
            {
                kill(getppid(),SIGUSR2);
                flag = 0;
            }
        }
    }
    if(pid > 0){
        // 父进程
        usleep(100);
        nums = 1;
        struct sigaction sigAct;
        sigAct.sa_flags = 0;
        sigemptyset(&sigAct.sa_mask);
        sigAct.sa_handler = catch_sig_usr2;
        sigaction(SIGUSR2,&sigAct,NULL);
        while(1){
            sleep(2);
            if(flag == 1 )
            {
                kill(pid,SIGUSR1);
                flag = 0;
            }
        }
    }

    return 0;
}

运行结果 :

[dai@localhost test]$ ./02父子进程交替报数
父进程:0
子进程:1
父进程:2
子进程:3
父进程:4
子进程:5
父进程:6
子进程:7
父进程:8
^C

在父子进程进行管道通信时,如果管道读端都关闭,会收,到SIGPIPE信号,模拟场景,对该信号进行捕捉,并且使用捕捉函数回收子进程

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

void catch_sig_pipe(int num)
{
    printf("捕捉到管道关闭信号\n");
    pid_t pid;
    while( (pid=wait(NULL)) > 0 ){
        printf("回收子进程:%d\n",pid);
    }
    exit(0);
}

int main()
{
    int fd[2];
    pipe(fd);

    pid_t pid = fork();
    if(pid == 0) {
        printf("我是子进程%d\n",getpid());
        sleep(2);
        // 子进程
        close(fd[1]);   // 关闭写端
        char buf[1024];
        while(1){
            sleep(1);
            memset(buf,0x00,sizeof(buf));
            read(fd[0],buf,sizeof(buf));
            if(strcmp(buf,"close") == 0)
            {
                close(fd[0]);   // 接收到"close" 子进程关闭读端
                return 0;   // 或者break;
            }
            printf("子进程接受到:%s\n",buf);
        }
    }
    else
    {
        // 父进程
        struct sigaction sigAct;
        sigAct.sa_flags = 0;
        sigemptyset(&sigAct.sa_mask);
        sigAct.sa_handler = catch_sig_pipe;
        sigaction(SIGPIPE,&sigAct,NULL);

        close(fd[0]);   // 父进程关闭读端
        char buf[1024];
        while(1){
            sleep(1);
            memset(buf,0x00,sizeof(buf));
            printf("请输入发给子进程的内容:");
            scanf("%s",buf);
            write(fd[1],buf,strlen(buf));
        }
    }
    return 0;
}

运行结果 :

[dai@localhost test]$ ./03父子进程管道通讯
我是子进程46440
请输入发给子进程的内容:AAA
子进程接受到:AAA
请输入发给子进程的内容:BBB
子进程接受到:BBB
请输入发给子进程的内容:close
请输入发给子进程的内容:s
捕捉到管道关闭信号
回收子进程:46440

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

去留意

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值