C使用信号同步进程 [英] C synchronize processes using signal

查看:78
本文介绍了C使用信号同步进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我试图教自己如何发信号,但遇到了麻烦,我无法弄清楚自己在做什么错.现在发生的是:它正在执行父级,然后转到子级,然后返回父级..它没有执行我想要执行的执行父级(用户定义运行时间的操作)然后杀死它,然后交给孩子并在相同的时间运行自己.

Okay so I am trying to teach myself on how to do signalling, and I came across a hiccup and I can't figure out what I'm doing wrong. What is going on right now is: it is executing the parent then goes to child and then back to parent.. It's not doing what I want it to do which is execute the parent (which the user defines the amount of time it runs) then kills it then go to child and run itself at the same amount of time.

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

void action(int);
void action(int dummy){
    sleep(1);
    printf("Switching\n");
}

int main(int argc, char *argv[]){
    pid_t pid;
    int m = atoi(argv[1]), i = 0, x = 0;
    if((pid=fork())>0){//parent
        sleep(1);
        while(i < m){
            printf("hello %d\n", x);
            x++;
            kill(pid, SIGUSR1);
            signal(SIGUSR1, action);
            pause();
            i++;
        }
    }

    else
        while(i < m){//child
            //waitpid(getppid(), &status, 0); // wait for parent
            signal(SIGUSR1, action);
            pause();
            printf("hi%d\n", x);
            x++;
            kill(getppid(), SIGUSR1);
            i++;
        }
}

我想要它做的是:

hello 0
hello 1
hello 2
hello 3
hello 4
Switching
hi 0
hi 1
hi 2
hi 3
hi 4

非常感谢您的帮助!

推荐答案

您已经掌握了大部分内容,只需要对它们重新排序即可.

You've got most of the pieces, they just need to be reordered a little bit.

  • 在使用kill
  • 之前,在两个进程中都安装信号处理程序
  • 父母应在通知孩子之前完成打印
  • 孩子可以在完成打印后发信号
  • install the signal handler in both processes before using kill
  • the parent should finish printing before signaling the child
  • the child can signal back after its done printing
void action(int dummy)
{
    sleep(1);
    printf("Switching\n");
}

int main(int argc, char *argv[])
{
    int m = 3;
    if (argc == 2)
        m = atoi(argv[1]);

    pid_t pid = fork();         // create the child process
    signal(SIGUSR1, action);    // set up the signal handler for both parent and child

    if ( pid > 0 )              // the parent
    {
        for ( int i = 0; i < m; i++ )
        {
            sleep(1);
            printf("hello %d\n", i);
        }
        kill( pid, SIGUSR1 );   // signal the child
        pause();                // wait for the child to signal back
        printf("All done\n");
    }
    else                        // the child
    {
        pause();                // wait for the signal from the parent
        for ( int i = 0; i < m; i++ )
        {
            sleep(1);
            printf("hi %d\n", i);
        }
        kill(getppid(), SIGUSR1);   // signal the parent
    }
}

这篇关于C使用信号同步进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆