The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Message Queue Problem SaTYR High Level Programming 7 07-04-2008 09:30 PM
message queue etenv High Level Programming 2 11-02-2007 02:09 AM
message queue problem ramneek IP Networking 0 09-13-2005 11:14 PM
Message Queue Problem Again.. satansfury High Level Programming 2 07-05-2005 07:52 AM
Cron message queue problem mattd UNIX for Dummies Questions & Answers 7 12-16-2003 04:24 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-09-2008
Registered User
 

Join Date: Jun 2008
Posts: 1
Question [C]Problem removing a message queue

Hi!!
This code works if I don't remove the message queue.
In A.c I create 3 processes that send a message in a message queue.
in B.c other 3 processes receive 1 message for each (the messages sent from A), change the value of "dato" and put again the message in the queue.
The processes in A.c receive the modified message and then I should remove the queue.
The answer is: How can I remove the queue after ALL the processes have received the message? I tried with 3 wait(0) but the queue is removed after the first process has received the message and I can't understand why
Code here:

A.c
Code:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/msg.h>

#define key 121121
#define max 20
typedef struct {
	long type;
	char data[max];
	}message;

message mex;

int main(){

int i,n;
int father;
char stringaint[max];

mex.type=357;
int idqueue=msgget(key,IPC_CREAT |0600);

for(i=0;i<3;i++){
          father=fork();
          if(!father){
	   sprintf(stringaint,"%d",getpid());
    	   strcpy(mex.dato,stringaint);
	   printf("The son will send the data with his PID: %s\n",mex.data);
	   if(msgsnd(idqueue,&mex,sizeof(message),0)==-1) 
                         printf("It's not working!");

	   if(msgrcv(idqueue,&mex,sizeof(message),getpid(),0)==-1)
		printf("\nMessage not received\n");
	   else 
	             printf("\nMessage received");
	   i=3;
	}
}
for (i=0; i<3; i++)
      wait(0);
//Without this part the program works (but I need to remove the queue!)
msgctl(idcoda,IPC_RMID,0);
//
}
B.c
Code:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/msg.h>

#define key 121121
#define max 20

typedef struct {
	long type;
	char data[max];
	}message;


int main(){
int i;
int father;
char str[80];
messaggio temp;
int idqueue=msgget(key,IPC_CREAT |0600);


for(i=0;i<3;i++){
      father=fork();
      if(!father){
      if(msgrcv(idqueue,&temp,sizeof(message),0,0)==-1)
	printf("\nB hasn't received the message from A\n");	
      else 
	printf("\nB has received the message from A");

      temp.type=atoi(temp.data); 
      printf("\n%d\n", temp.type);
			
      strcat(temp.dato," ADD");
      printf("%s\n",temp.data);


      if(msgsnd(idqueue,&(temp),sizeof(message),0)==-1)
	printf("\nNot working!!");
      i=3;
      }
	
} 
}
Reply With Quote
Google The UNIX and Linux Forums
Forum Sponsor
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0