Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c - 如何切换到管道?(How do I switch to a 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?

(烹饪完成,点菜?)

Only this part should be changed to pipe.

(仅此部分应更改为管道。)

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.

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

  ask by hachiman123 translate from so

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

1 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...