My question is, how can I pass the X value between process, I don't wanna use a global variable for this.
int main(int argc, char *argv[])
{
pid_t pid;
int x;
x = 1;
pid=fork();
// Daqui para baixo executa o fork
if (pid == 0){ //Processo filho
printf("Processo Filho\n");
x = x + 2;
printf("Somando 2 ao X, temos: %d\n", x);
exit(0);
}
else
{ //Processo pai
printf("Processo pai\n");
printf("O valor inicial de X é: %d\n", x);
wait(NULL);
x = x * 4;
printf("Agora multiplicando o valor de X por 4 temos: %d\n", x);
printf("Criança completa\n");
}
return 0;
}
I'm trying just to print the X value using the shmget, but I get -1, I'm currently just testing if I can pass the X value using that
int main(int argc, char *argv[])
{
pid_t pid;
int x;
int shmid;
int *shmptr;
x = 1;
shmid = shmget(x, 4*sizeof(int), IPC_CREAT | 0666);
shmptr = (int *) shmat(shmid, NULL, 0);
pid=fork();
// Daqui para baixo executa o fork
if (pid == 0){ //Processo filho
printf("Processo Filho\n");
printf("%d\n",shmid);
x = x + 2;
printf("Somando 2 ao X, temos: %d\n", x);
exit(0);
}
else
{ //Processo pai
printf("Processo pai\n");
printf("O valor inicial de X é: %d\n", x);
wait(NULL);
x = x * 4;
printf("Agora multiplicando o valor de X por 4 temos: %d\n", x);
printf("Criança completa\n");
}
return 0;
}