Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7981313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:19:51+00:00 2026-06-04T10:19:51+00:00

I’m having problems in using message queues in this program it is supposed to

  • 0

I’m having problems in using message queues in this program
it is supposed to launch a number of processes that is passed by argument to the program by the command prompt but it only launches and calculates the points for one process… the others don’t get to launch ..
Please help me .

This is the program that creates the message queues and outputs the selected data into pgm format
only the first process gets to run the other processes don’t
can anybody tell me why ?

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

void main(int argc,char *argv[]) {

    if(argc != 2) {
    } else {

       int n = atoi(argv[1]);
       int i = 0;
       struct msgbuf {
              long mtype;
        int x;
        int y;
        double value;
       };

       struct envio {
          long mtype;
          long type;
          int ny;
          double yM1;
          double yM2;
       };


        key_t key = 123;
        key_t key2 = 124;
        int msgflg = IPC_CREAT | 0666;
        int msqid = msgget(key,msgflg);
        int msqid2 = msgget(key2,msgflg);
       switch(fork()) {
           case -1:
            printf("Erro de fork");
            break;
           case 0:
            printf("Oi: %d\n",n);           
            double *b;
            int x,y,i,m;

            double *ptr = b = malloc(1000*1000*sizeof(double));
            printf("Chego(1)\n");
                struct msgbuf a;
            struct envio c;
            size_t buflen = sizeof(a) - sizeof(long);
            size_t len2 = sizeof(c) - sizeof(long);
            printf("Chego(2)\n");
            int msid = msgget(key,msgflg);
            int msid2 = msgget(key2,msgflg);
            printf("Chego(3)\n");           
            double aux = -1.0;
            double multiplier = ((1.0/n) * 2);
            c.mtype = 300;
            int ny = (int)(1000/n);

            for(i = 0; i < n; i++) {
             c.type = (i+1);
               c.ny = (int)(1000/n);
               c.yM1 = aux;
               c.yM2 = aux+multiplier;
               printf("Chego aqui(2)\n");
                           if(msgsnd(msid2,&c,len2,0) < 0) {
                  perror("Erro do 1o envio\n");
               }
            }
        for(m = 0; m < n; m++) {
               printf("Entrei no ciclo(1)\n");

            for(y = 0; y <ny ;y++) {
                //printf("Chego(4)\n");
                 for(x = 0; x < 1000;x++) {
                if(msgrcv(msid,&a,buflen,(long)(m+1),0) < 0) {
                   perror("Erro na recepcao:\n ");       
                }
                //printf("Chego(5)\n");
                b[a.y * ny + a.x] = a.value;
                 }
            }

            b = b + (1000/n)*1000;
           }
            output_pgm("mandel.pgm", ptr, 1000, 1000, 255);
            //msgctl(msid, IPC_RMID, NULL);
            //msgctl(msid2, IPC_RMID, NULL);
            printf("Processo 1\n");
            break;
           default:
             for(i = 0;i < n;i++) {
              switch(fork()) {
                  case -1:
                   printf("Erro de fork");
                case 0:
                   exit(0);
                   break;
                default:
                    printf("Fui lancado\n");
                    execlp("/home/hyper/Documents/SO2/TP3-4/rec","rec",0);
                    break;
              }
                 }
             break;         
        }             
    }   

};

The for loop in this program is run only once

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
double type;
       struct senbuf {
              long mtype;
        int x;
        int y;
        double value;
       };
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(int msqid,int nx, int ny, double xmin, double xmax,
         double ymin, double ymax,long type2 ) {

  double delta_x, delta_y;
  int x, y;
  struct senbuf sen;
  delta_x = (xmax - xmin)/nx;
  delta_y = (ymax - ymin)/ny;
  size_t buflen = sizeof(sen) - sizeof(long);
  for (y=0;  y<ny; y++) {
    //printf("Ja entrei aqui");
    double y_value = ymin + delta_y * y;
    for (x=0; x<nx; x++) {
      double x_value = xmin + delta_x * x;
      sen.mtype = type;
      sen.x = x;
      sen.y = y;
      sen.value = compute_point(x_value,y_value);
      if(msgsnd(msqid,&sen,buflen,0) < 0) {
        perror("Erro no envio:");
      };     
     // printf("%f",sen.a[y*nx + x]);
      //buffer[y*nx + x] = compute_point(x_value, y_value);
    }
  }
   printf("Ja mandei %d\n",type2);  
/*sen.mtype=500;
   sen.test = -1;
   printf("Ja to a sair\n");
   msgsnd(msqid,&sen,buflen,IPC_NOWAIT);*/
}

/* 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()
{
    int msqid;
    int msqid2;
          struct recep {
          long mtype;
          long type;
          int ny;
          double yM1;
          double yM2;
       };

     struct recep a;
     size_t len = sizeof(a) - sizeof(long);
     key_t key = 124;
     msqid = msgget(key, 0666);
     if(msgrcv(msqid, &a, len, 300, 0) < 0) {
          perror("Error checking");
     };
    printf("Dados :\n Tipo : %d\n Ny: %d\n,yM1 : %f\n yM2: %f\n",a.type,a.ny,a.yM1,a.yM2);


     type = a.type;
     printf("Vou iniciar o compute");

     key_t key2 = 123;
         msqid2 = msgget(key2,0666);    
     compute(msqid2,1000,a.ny, -1.0, 1.0,a.yM1,a.yM2,a.type);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:19:53+00:00Added an answer on June 4, 2026 at 10:19 am

    In your first switch statement you create one child doing whatever it is doing. The parent falls into a second switch statement in which the child immediately exits and the parent is overlaid with the “rec” executable. The parent no longer executes at point – it is the program “rec”. You are never going to execute more than one pass of the loop because the code that is executing is gone at that point. If you want multiple instances of “rec” running you should be using execlp on the children not the parent.

    EDIT

    There are two system calls wait and waitpid that provides various options. The simpler of these is wait and should be sufficient for what you are doing. Define and increment a counter in the parent for each child you create. Then instead of just exiting the parent you wait for all the children to finish. Something as simple as this should suffice:

    for (int i = 0; i < counter; i++)
    {
        wait(NULL);
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.