Communication between parent and child

Posted by Pierre on Stack Overflow See other posts from Stack Overflow or by Pierre
Published on 2010-05-20T12:36:23Z Indexed on 2010/05/20 12:50 UTC
Read the original article Hit count: 324

Hi every one !

I have a little problem. I have to code a simple C application that creat a process and his child (fork()) and I have to do an operation. Parent initialize the values and child calculate. I write this :


#include 
#include 
#include 
#include 
#include 



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;
}

But my child is never called. Is there something wrong with my code? Thanks for your help !

© Stack Overflow or respective owner

Related posts about fork

Related posts about process