i need to pass the double *b from the child process to the parent process ... using message queues
but since it's a pointer i am getting a segmentation fault at the compute() function that is in the second process(parent)
Code:
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
void main(int argc,char *argv[]) {
if(argc != 2) {
} else {
int n = atoi(argv[1]);
int i = 0;
struct msgbuf {
long mtype;
char message[100];
double b;
};
switch(fork()) {
case -1:
printf("Erro de fork");
break;
case 0:
printf("Oi");
double *b;
int msgflg = IPC_CREAT | 0666;
b = malloc(1000*1000*sizeof(double));
struct msgbuf a;
a.mtype = 1;
strcpy(a.message,"1");
a.b = *b;
key_t key = 123;
size_t buflen = sizeof(a) - sizeof(long);
int msqid = msgget(key,msgflg);
msgsnd(msqid, &a,buflen, IPC_NOWAIT);
printf("Processo 1\n");
break;
default:
wait();
printf("Chego aqui\n");
execlp("/home/hyper/Documents/SO2/TP3-4/rec","rec",0);
/*
printf("Entro aqui\n");
for(i = 0;i < n;i++) {
switch(fork()) {
case -1:
printf("Erro de fork");
case 0:
exit(0);
break;
default:
printf("Processo 2\n");
break;
}
}*/
break;
}
}
};
Code:
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
int max_iterations = 256;
double compute_point(double ci, double cr) {
int iterations = 0;
double zi = 0;
double zr = 0;
while ((zr*zr + zi*zi < 4) && (iterations < max_iterations)) {
double nr, ni;
/* Z <-- Z^2 + C */
nr = zr*zr - zi*zi + cr;
ni = 2*zr*zi + ci;
zi = ni;
zr = nr;
iterations ++;
}
return iterations;
}
/* The "compute" function computes the Mandelbrot function over every
point on a grid that is "nx" points wide by "ny" points tall, where
(xmin,ymin) and (xmax,ymax) give two corners of the region the
complex plane.
*/
void compute(double *buffer, int nx, int ny, double xmin, double xmax,
double ymin, double ymax) {
double delta_x, delta_y;
int x, y;
delta_x = (xmax - xmin)/nx;
delta_y = (ymax - ymin)/ny;
for (y=0; y<ny; y++) {
double y_value = ymin + delta_y * y;
for (x=0; x<nx; x++) {
double x_value = xmin + delta_x * x;
buffer[y*nx + x] = compute_point(x_value, y_value);
printf("Chego aqui %d \n",x);
}
}
}
/* Output the data contained in the buffer to a Portable Greymap format
image file. The parameter "max" should be an upper bound for the
data values in the buffer.
*/
void output_pgm(char *filename,double *buffer, int nx, int ny, double max) {
int i;
FILE *file;
file = fopen(filename,"w");
fprintf(file,"P2\n");
fprintf(file,"%d %d\n",nx,ny);
fprintf(file,"%d\n",(int)max);
for (i=0; i<nx*ny; i++) {
if (!(i%nx)) fprintf(file,"\n");
fprintf(file,"%d ",(int)buffer[i]);
}
fclose(file);
}
int main()
{
printf("Chego aqui(2)\n");
int msqid;
struct msgbuf {
long mtype;
char message[100];
double b;
};
struct msgbuf a;
size_t len = sizeof(a) - sizeof(long);
key_t key = 123;
printf("Chego aqui(2)\n");
msqid = msgget(key, 0666);
printf("Chego aqui(2)\n");
msgrcv(msqid, &a, len, 1, 0);
printf("Chego aqui(2)\n");
double *b = malloc(1000*1000*sizeof(double));
b = &(a.b);
printf("Chego aqui(3)\n");
compute( b, 1000, 1000, -1.0, 1.0, -1.0, 1.0);
printf("Chego aqui(4)\n");
output_pgm( "mandel.pgm", b, 1000, 1000, 255);
}
Somehow it gives me a segmentation fault in the other process ...