hello .. can anybody explain me how i can pass integer and *double pointers through message queues
i have no idea how i can do that ... :frown:
Printable View
hello .. can anybody explain me how i can pass integer and *double pointers through message queues
i have no idea how i can do that ... :frown:
You'll need to give us a bit more to go on. Show us the code where you want to add message queues (I assume the program is multi-threaded?)
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;
}
}
};
Somehow it gives me a segmentation fault in the other process ...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);
}
Processes don't share their address space. Any pointer passed between them is highly likely to be invalid on the receiving end and trying to dereference it will lead to segfault.
but then how can i share data between the two processes ... ? since the compute function needs a *double value ?!?!
I could use an alternative way but i need to share the data between the two processes :S
Threads run in the same address space. Can you use threads instead of processes?
You could create a shared memory block and instead of passing around a double* you could index into that shared memory block
I'd recommend doing this over threads. It should be easier to do then threading and more appropriate for the task; which looks more like an exercise in IPC then parallel processing.
Note to OP: Don't abandon message passing entirely. You'll still need it for communication between processes.
The function probably needs a list of doubles. If you are not allowed to share pointers, you have two options. 1 is to pass the doubles themselves. You might have to send several messages and reaasemble the list if it more than a few numbers long. 2 is to keep parent and child in synch and pass an identifier. This might be trivially easy if the listd are in read only memory, it will be harder if the lists are generated before the fork, very hard is the lists are generated after the fork, and impossible if the lists rely on separate data.
Hi can you give me an example on how to pass an identifier ?
I have already made it work with threads and shared memory before
i would like the communication between processes to be done exclusively with message queues
Because i am trying to see if a program works with every kind of inter process communication