父母和孩子之间的沟通

时间:2010-05-20 12:36:23

标签: c process operating-system fork parent-child

我必须编写一个简单的C应用程序来创建一个进程和一个子进程(fork()),我必须做一个操作。父初始化值和子计算。我写这个:

#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

typedef struct {
    int op1;
    char op;
    int op2;
} Operation;

Operation *varOP;

void finalResult()
{
    float result = 0;
    if(varOP->op == '+') result = (varOP->op1 + varOP->op2);
    if(varOP->op == '-') result = (varOP->op1 - varOP->op2);
    if(varOP->op == '*') result = (varOP->op1 * varOP->op2);
    if(varOP->op == '+') result = (varOP->op1 / varOP->op2)
    printf("%f",result);
}

int main () {
    int p;
    varOP  = (Operation *)malloc(sizeof(Operation));
    p = fork();
    if(p == 0) // If child
    {
        signal(SIGUSR1, finalResult );
        pause();
    }

    if(p > 0) // If parent
    {
        varOP->op = '+';
        varOP->op1 = 2;
        varOP->op2 = 3;
        kill(p, SIGUSR1);
        wait(NULL);
    }
    return 0;
}

但我的孩子从未被召唤过。我的代码有问题吗? 谢谢你的帮助!

3 个答案:

答案 0 :(得分:2)

您的示例代码也有一个更基本的问题:每个进程都有自己的数据空间,因此您通过堆向子进程发送信息的技术将无法正常工作。一种解决方案是使用管道。这只会为您的代码添加四行:

typedef struct {
    int op1;
    char op;
    int op2;
}Operation;

Operation *varOP;

static int pipe_fds[2]; /* <-- added */

static void finalResult(void)
{
    float result = 0;
    read(pipe_fds[0], varOP, sizeof(Operation)); /* <-- added */
    if(varOP->op == '+') result = (varOP->op1 + varOP->op2);
    if(varOP->op == '-') result = (varOP->op1 - varOP->op2);
    if(varOP->op == '*') result = (varOP->op1 * varOP->op2);
    if(varOP->op == '/') result = (varOP->op1 / varOP->op2); /* <-- typo */

    printf("%f\n",result);
}

int main (void) 
{
    int p;
    pipe(pipe_fds); /* <-- added */
    varOP = (Operation *)malloc(sizeof(Operation)); 
    p = fork();

    if(p == 0) // If child
    {
        signal(SIGUSR1, finalResult );
        pause();
    }

    if(p > 0) // If parent
    {
        varOP->op = '+';
        varOP->op1 = 2;
        varOP->op2 = 3;
        write(pipe_fds[1], varOP, sizeof(Operation)); /* <-- added */
        kill(p, SIGUSR1);
        wait(NULL);
    }

    return 0;
}

答案 1 :(得分:1)

感谢约瑟夫,它运作良好! 我试着用内存分段来做这件事,我有同样的问题-_-


#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 

typedef struct {
    int op1;
    char op;
    int op2;
}Operation;


int id;

void hand()
{
    Operation *varOP  = (Operation*) shmat(id, NULL, SHM_R); 
    shmdt((char *) varOP):
    float result = 0;

    switch (varOP->op) {
        case '+':
            result = (varOP->op1 + varOP->op2);
            break;
        case '-':
            result = (varOP->op1 - varOP->op2);
            break;
        case '*':
            result = (varOP->op1 * varOP->op2);
            break;
        case '/':
            result = (varOP->op1 / varOP->op2);
            break;
        default:
            result = 0;
            break;
    }

    printf("%f",result);
    exit(0);
}

int main () {

    int p;
    key_t cle;

    p = fork();

    cle = ftok(getenv("titi"), 'A');
    id = shmget(cle, sizeof(Operation),0);

    if(p == 0) // Si fils
    {
        signal(SIGUSR1,hand);
        while (1);
        exit(0);
    }

    if(p > 0)
    {
        Operation *varOP  = (Operation*) shmat(id, NULL, SHM_W); 
        varOP->op = '+';
        varOP->op1 = 2;
        varOP->op2 = 3;
        shmdt((char *) varOP);
        kill(p, SIGUSR1);

        wait(NULL);
    }

    return 0;
}

答案 2 :(得分:0)

当父母致电signal()时,孩子可能尚未执行kill()来电。