coder_jacky 2015-04-25 16:29 采纳率: 100%
浏览 1771
已采纳

一个小的Linux程序,一运行电脑就自动关机。(要求两个进程轮流打印1.2.3.4....)

#include
#include
#include
#include
#include

void f_sigact(int num)
{
static int f = 2;
// sleep(1);
printf("I am father.This is %d\n",f);
f += 2;
}

void c_sigact(int num)
{
int static c = 1;
// sleep(1);
printf("I am child.This is %d\n",c);
c+=2;
}

int main()
{
pid_t fpid,cpid;
struct sigaction fact,cact;
fact.sa_handler = f_sigact;
cact.sa_handler = c_sigact;
// sigemptyset(&fact.sa_mask);
// sigaddset(&fact.sa_mask,SIGUSR2);
fact.sa_flags = 0;
// sigaction(SIGUSR1,&fact,NULL);
// sigemptyset(&cact.sa_mask);
sigaddset(&cact.sa_mask,SIGUSR1);

cact.sa_flags = 0;
sigaction(SIGUSR2,&cact,NULL);

fpid = fork();
if(fpid < 0){
    perror("fork");
    exit(1);
}
int m = 5;
while(1){
    if(fpid > 0){
        kill(fpid,SIGUSR2);
//      sleep(1);
        }
    else{
        cpid = getppid();
        kill(cpid,SIGUSR1);
//      sleep(1);
    }
    sleep(3);
}

return 0;

}

  • 写回答

2条回答

  • 知常曰明 2015-04-26 00:36
    关注
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    void f_sigact(int num)
    {
            static int f = 2;
            // sleep(1);
            printf("I am father.This is %d\n",f);
            f += 2;
    }
    void c_sigact(int num)
    {
            int static c = 1;
            // sleep(1);
            printf("I am child .This is %d\n",c);
            c+=2;
    }
    int main()
    {
            pid_t fpid,cpid;
            struct sigaction fact,cact;
            struct sigaction ofact,ocact;
            fact.sa_handler = f_sigact;
            cact.sa_handler = c_sigact;
            // sigemptyset(&fact.sa_mask);
            // sigaddset(&fact.sa_mask,SIGUSR2);
            fact.sa_flags = 0;
            cact.sa_flags = 0;
            // sigaction(SIGUSR1,&fact,NULL);
            // sigemptyset(&cact.sa_mask);
            sigaddset(&cact.sa_mask,SIGUSR1);
            sigaddset(&fact.sa_mask,SIGUSR2);
            //sigaction(SIGUSR2,&cact,NULL);
            cpid = fork();
            if(cpid < 0){
                    perror("fork");
                            exit(1);
            }
            else if (cpid==0){
                    //it's a child
                    sigaction(SIGUSR1,&cact,&ocact);
            }
            else{
                    //it;s the parent
                    sigaction(SIGUSR2,&fact,&ofact);
            }
    
            int m = 5;
            sleep(1);
            while(1){
                    if (cpid>0)
                    {
                            fpid = getpid();
                            kill(cpid,SIGUSR1);
                            sleep(1);
                            kill(fpid,SIGUSR2);
                            sleep(1);
                    }
                            sleep(3);
            }
    
            return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档