Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

c - 很难将信号更改为管道(It's hard to change the Sig to pipe)

Parent process

(父进程)

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

#define LINE_SIZE 50

void sigint_handler(int signo);
void sigusr1_handler(int signo);


int cooking_pid = -1;

void sigint_handler(int signo) {

    kill(cooking_pid, SIGINT);
    wait();
    printf("---------exit------------
");
    printf("Child Processor Shutdown
");
    exit(0);
}

void sigusr1_handler(int signo) {

    struct sigaction sact;

    printf("(Food Served.)
");

    sigemptyset(&sact.sa_mask);
    sact.sa_flags = 0;
    sact.sa_handler = sigint_handler;
    sigaction(SIGINT,&sact,NULL);
 }

 int main(void) {

    char ch;

    struct sigaction sact;

    int c2k_fd[2];
    int k2c_fd[2];

    if(pipe(c2k_fd) == -1) {
            perror("pipe");
            exit(1);
    }
    if(pipe(k2c_fd) == -1) {
            perror("pipe");
            exit(1);
    }

    cooking_pid = fork();

    if (cooking_pid < 0) {
            perror("fork");
            exit(2);
    }

    else if (cooking_pid == 0) {
            close(c2k_fd[1]);
            close(k2c_fd[0]);

            dup2(c2k_fd[0], 0);
            dup2(k2c_fd[1], 1);
            if (execl("./cooking.out", "cooking.out", 
NULL) < 0) {
            perror ("execl");
            exit(2);
            }
    }

    sigemptyset(&sact.sa_mask);
    sact.sa_flags = 0;
    sact.sa_handler = sigint_handler;
    sigaction(SIGINT,&sact,NULL);



    while (1) {
            printf("-------------------
");
            printf("order? (y or n) :");

            if (scanf(" %c", &ch) == 1) {
            if (ch == 'y') {

    sigemptyset(&sact.sa_mask);
    sact.sa_flags = 0;
    sact.sa_handler = sigusr1_handler;
    sigaction(SIGUSR1,&sact,NULL);

    sigemptyset(&sact.sa_mask);
    sact.sa_flags = 0;
    sact.sa_handler = SIG_IGN;
    sigaction(SIGINT,&sact,NULL);


    kill (cooking_pid, SIGUSR1);
    printf("(Order shipping)
");
                    }

            }
    }

 return 0;
 }

----Child process-----

(----子进程-----)

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

 #define COOK_TIME 5

void sigusr1_handler(int signo);
void sigalrm_handler(int signo);


void sigusr1_handler(int signo) {

    struct sigaction sact;



    printf("
");
    printf("(Arrival order. Food production.)
");

    sigemptyset(&sact.sa_mask);
    sact.sa_flags=0;
    sact.sa_handler=SIG_IGN;
    sigaction(SIGINT,&sact,NULL);

    sigemptyset(&sact.sa_mask);
    sact.sa_flags=0;
    sact.sa_handler=sigalrm_handler;
    sigaction(SIGALRM,&sact,NULL);
    alarm(COOK_TIME);}





void sigalrm_handler(int signo) {

    struct sigaction sact;

    printf("(Cooking complete.)
");
    kill (getppid(), SIGUSR1);


    sigemptyset(&sact.sa_mask);
            sact.sa_flags=0;
            sact.sa_handler=SIG_DFL;
            sigaction(SIGINT,&sact,NULL);
            sigaction(SIGALRM,&sact,NULL);

            sigemptyset(&sact.sa_mask);
            sact.sa_flags= 0;
            sact.sa_handler=sigusr1_handler;
            sigaction(SIGUSR1,&sact,NULL);
}




 int main(void) {

     struct sigaction sact;

    sigemptyset(&sact.sa_mask);
    sact.sa_flags=0;
    sact.sa_handler=sigusr1_handler;
    sigaction(SIGUSR1,&sact,NULL);

     while (1) {
             sleep(60);
     }

 }

The above code is the parent process.

(上面的代码是父进程。)

Below that is a child process.

(在此之下是一个子进程。)

I'm not sure what to add here to send and receive by pipe.

(我不确定要在此处添加什么内容以通过管道发送和接收。)

Cooking complete , order?

(烹饪完成,点菜?)

It is necessary to replace sigusr1 in this part with a pipe.

(这部分需要用管道替换sigusr1。)

order?

(订购?)

(Parent process) Cooking complete(Child process) Here you need to replace sigusr1 with a pipe.

((父进程)烹调完成(子进程)在这里,您需要用管道替换sigusr1。)

I don't know how to change part of sigusr1 to pipe.

(我不知道如何将sigusr1的一部分更改为管道。)

This is complicated because I have a parent process and a child process.

(这很复杂,因为我有一个父进程和一个子进程。)

Help me..

(帮我..)

  ask by hachiman123 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...