Parent-child bond

I need to code a simple C application that creates a process and a child (fork ()) and I have to perform an operation. Parent initializes values ​​and calculates children. I am writing this:

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

      

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

+2


a source to share


3 answers


There is also a more fundamental problem in your example code: each process has its own data space, so your technique of sending information to a child through the heap won't work. One solution is to use a pipe . This adds four more lines to your code:



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

      

+2


a source


Thanks, Joseph is working well! I am trying to do this with shared memory and I have the same problem -_-




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

      

+1


a source


The child may not have completed the call yet signal()

when the parent calls kill()

.

0


a source







All Articles