很难将Sig更改为管道

时间:2019-12-01 16:12:51

标签: c

家长程序

#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------------\n");
    printf("Child Processor Shutdown\n");
    exit(0);
}

void sigusr1_handler(int signo) {

    struct sigaction sact;

    printf("(Food Served.)\n");

    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("-------------------\n");
            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)\n");
                    }

            }
    }

 return 0;
 }

----子进程-----

 #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("\n");
    printf("(Arrival order. Food production.)\n");

    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.)\n");
    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);
     }

 }

上面的代码是父进程。在此之下是一个子进程。我不确定要在此处添加什么内容以通过管道发送和接收。

烹饪完成了吗? 有必要用管道替换此部分中的sigusr1。

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

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

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

0 个答案:

没有答案