#1 Pasando y reciviendo mensajes en linux
Hola a todos, tengo un problema a la hora que quiero recivir los mensajes que he enviado por medio de un proceso. Por que el proceso que va a recivir los mensajes se queda bloaqueado, aun con mensajes en el buzon.

Posteando el codigo:

Emisor

Código:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>

#define LEN 4
#define PROCESS 3
#define TAMBUFFER 400
#define BUFFER 35

void productor();
void consumidor();
void createProcesses();
void waitProcesses();

char caracter(int i);
int cifra(char *s, char *d);

typedef struct my_msg_st {
  long int my_msg_type;
  char some_text[TAMBUFFER];
} msg;

int main()//Done!!!
{
  printf("EL PROCESO PRINCIPAL TIENE EL PID: %d\n", getpid());
  
  int msgid;
  int msgid2;
  int i;
  size_t buf_length;
  msg message;
  
  msgid = msgget((key_t)1234, 0666|IPC_CREAT);//MailBox 1 --Producer--

  if(msgid == -1)
  {
    fprintf(stderr, "Falló msgget\n");
    exit(EXIT_FAILURE);
  } // End of if
  
  msgid2 = msgget((key_t)5678, 0666|IPC_CREAT);//MailBox 2 --Consumer--

  if(msgid2 == -1)
  {
    fprintf(stderr, "Falló msgget\n");
    exit(EXIT_FAILURE);
  } // End of if
  
  message.my_msg_type = 1;
  
  strcpy(message.some_text, "Cowboy pica\n");
  
  buf_length = strlen(message.some_text) + 1 ;  
  
  for(i = 0; i  < BUFFER; i++)
    {
        // We send the message!
        if(msgsnd(msgid,&message, buf_length, IPC_NOWAIT) == -1)
        {
          fprintf(stderr,"Falló msgsnd\n");
          exit(EXIT_FAILURE);
        } // End of if
        
    }
  
  createProcesses();
  waitProcesses();

} // End of main

void productor()
{
  int i; 
  int n[LEN + 1]; 

  char cadena[LEN + 1]; 
  cadena[LEN] = '\0'; 
  
  int msgid;
  int msgid2;
  msg message;
  msg message2;
  long int msg_to_receive = 1;
  message2.my_msg_type = 1;
  size_t buf_length;

  msgid = msgget((key_t)1234, 0666|IPC_CREAT);//MailBox 1 --Producer--

    if(msgid == -1)
      {
        fprintf(stderr, "Falló msgget\n");
        exit(EXIT_FAILURE);
      } // End of if
      
    msgid2 = msgget((key_t)5678, 0666|IPC_CREAT);//MailBox 2 --Consumer--
    
    //printf("%d\n", msgid2);
    
    if(msgid2 == -1)
      {
        fprintf(stderr, "Falló msgget\n");
        exit(EXIT_FAILURE);
      } // End of if
 
  for(n[3] = -1; n[3] < 62; n[3]++)
    for(n[2] = -1; n[2] < 62; n[2]++)
      for(n[1] = -1; n[1] < 62; n[1]++)
        for(n[0] = -1; n[0] < 62; n[0]++)
        {
        
            if(msgrcv(msgid, (void *) &message, TAMBUFFER, msg_to_receive, 0) == -1) // ** FUNCION MSGRCV: BLOQUEANTE **
            {
                fprintf(stderr,"Falló msgrcv\n");
                exit(EXIT_FAILURE);
            } // End of if
        
            for(i = 0; i < LEN; i++)
                cadena[i] = caracter(n[i]);

            //printf("%s\n", cadena);
            
            strcpy(message2.some_text, cadena);
  
            buf_length = strlen(message2.some_text) + 1 ;  
    
            // We send the message!
            if(msgsnd(msgid2,&message2, buf_length, IPC_NOWAIT) == -1)
            {
                fprintf(stderr,"Falló msgsnd\n");
                exit(EXIT_FAILURE);
            } // End of if    
            
        } // End of for 

} // End of productor() function

void consumidor()
{
  char encrip[30];

  //cifra(cadena, encrip);

  //if(strcmp(encrip, "fD1Iq14X2\0") == 0)
   // printf("%s %d\n", cadena, encrip);

} // End of consumidor() function

void createProcesses()
{
  int cont;
  int fatherId = getpid(); // We get the ID of the main process
  int pid;

  printf("El proceso principal tiene el PID: %d\n", getpid());

  for(cont = 0; cont < PROCESS; cont++)
  {
    if(getpid() == fatherId) // Only the father can execute this!
    {
      pid = fork(); // Creates a new process
   
      if(pid == 0) // Only the child can execute this!
      {
        if(cont == 0)
        {
          printf("Un proceso productor PID: %d\n", getpid());
          productor();
          exit(EXIT_SUCCESS); // Kills the process
        }
        else
        {
          printf("Un proceso consumidor PID: %d\n", getpid());
          consumidor();
          exit(EXIT_SUCCESS);
        }
      } // End of if   
   
    } // End of if
  } // End of for

} // End of createProcesses() function

void waitProcesses()
{
  int i;
  int child_pid;
  int stat_val;

  // Wait for each of the processes
  for(i = 0; i < PROCESS; i++)
  {
    child_pid = wait(&stat_val);
    printf("El hijo terminó: PID = %d\n", child_pid);

    if(WIFEXITED(stat_val))
      printf("El hijo terminó con el código %d\n", WEXITSTATUS(stat_val));
    else
      printf("Terminación anormal del hijo\n");
    
  } // End of for

} // End of waitProcesses() function

char caracter(int i)
{
  char c;
    
  if(i == -1)
    c = '\0';
  else if(i < 10)
    c = i + '0';
  else if(i < 36)
    c = i + 'A' - 10;
  else
    c = i + 'a' - 36;
  
  return(c);

} // End of caracter() function

int cifra(char *s, char *d)
{
  int i;
  int j = 0;
  char n;
  int x = 0;
    
  for(i = 0; i < strlen(s) - 1; i++)
  {
    if(s[i] == 0)
      break;

    x = 0;
    x = s[i] * s[i + 1] + s[i] - s[i + 1];
    x = abs(x);

    while(x > 0)
    {
      n = x % 62;
            
      if(n < 10)
        d[j] = '0' + n;
      else if(n < 36)
        d[j] = 'A' + n - 10;
      else
        d[j] = 'a' + n -36;
        x /= 62;
        j++;
    } // End of while  

  } // End of for
  
  d[j]=0;

} // End of cifra() function
Receptor

Código:
#include  <stdlib.h>
#include  <stdio.h>
#include  <string.h>
#include  <unistd.h>
#include  <sys/types.h>
#include  <sys/ipc.h>
#include  <sys/msg.h>

#define TAMBUFFER 400 // Length of the msg in "bytes"

struct my_msg_st {
  long int my_msg_type;
  char some_text[TAMBUFFER];
};

int main()
{
  //int running = 1;

  int msgid;
  long int msg_to_receive = 1;
  struct my_msg_st some_data;
  
  // Primero, establecemos la cola de mensajes
  msgid = msgget((key_t)5678, 0666|IPC_CREAT);
  
  printf("%d\n", msgid);

  if(msgid == -1)
  {
    fprintf(stderr, "Falló msgget\n");
    exit(EXIT_FAILURE);
  } // End of if
    
    while(1)
    {
    // We recieve a message!
        if(msgrcv(msgid, (void *) &some_data, TAMBUFFER, msg_to_receive, 0) == -1) // ** FUNCION MSGRCV: BLOQUEANTE **
        {
          fprintf(stderr,"Falló msgrcv\n");
          exit(EXIT_FAILURE);
        } // End of if
            
        printf("Escribiste : %s", some_data.some_text);
   }
   
  if(msgctl(msgid, IPC_RMID, 0) == -1)
  {
    fprintf(stderr, "Falló msgctl(IPC_RMID)\n");
    exit(EXIT_FAILURE);

  } // End of if

  exit(EXIT_SUCCESS);

} // End of main
Si los compilan y los ejecutan, el receptor ni siquiera recive un mensaje y eso que hay 35 mensajes en el buzon, eso me consta, por que ejecute el comando ipcs, hasta de hecho el receptor accede al mismo buzon, pero esta muy raro que no agarre ningun mensaje.......

Alguien me puede orientar?

Saludos
+
 
0
Me gusta
 
| Más