Communication between parent and child
I have to code a simple C application that creates a process and a child (fork()) and I have to do an operation. Parent initializes the values and child calculates. I write 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);
开发者_JS百科 wait(NULL);
}
return 0;
}
But my child is never called. Is there something wrong with my code? Thanks for your help!
Your sample code also has a more fundamental problem: each process has its own data space, and so your technique of sending information to the child via the heap will not work. One solution is to use a pipe. This adds only 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;
}
Thanks Joseph it is working well ! I'm tried to do it with memory segementation 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;
}
It may be that the child has not yet executed the signal()
call when the parent calls kill()
.
精彩评论