[C]Problem removing a message queue


 
Thread Tools Search this Thread
Top Forums Programming [C]Problem removing a message queue
# 1  
Old 06-09-2008
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 Smilie
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;
      }
	
} 
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Ubuntu

Message Queue in Linux

Hello How can I see the created message queues in the system? (4 Replies)
Discussion started by: xyzt
4 Replies

2. Programming

Message Queue Problem

Hi all, I need help about message queues, i have a server-client program that communicates each other via msg queue, firstly server opens its msg queue and waits for msg then client opens server msg queue and its own msg queue(for receiving msg from server,clients sends msg to server msg... (7 Replies)
Discussion started by: SaTYR
7 Replies

3. Programming

How to limit max no of message in a posix message queue

Hii can anyone pls tell how to limit the max no of message in a posix message queue. I have made changes in proc/sys/fs/mqueue/msg_max But still whenever i try to read the value of max. message in the queue using attr.mq_curmsgs (where struct mq_attr attr) its giving the default value as 10.... (0 Replies)
Discussion started by: mohit3884
0 Replies

4. Programming

message queue

Hello, i need to write a message queue "chat server", that should work only localy. Can anyone please help me with some ideas and peshaps code. I'm studying the UNIX IPC mechanisms right now. So far, i understand how it works but i still cannot get an idea how to write a chat programm... ... (2 Replies)
Discussion started by: etenv
2 Replies

5. Programming

Message Queue with fork() help

hi all... ive been trying this program where i spawn 4 threads... and i am trying to use message queue to send msgs from 3 of the threads to the parent thread... but it doent seem to be working... ive almost pulled out my hair tryin to fix the prob :confused: another wierd thing... (1 Reply)
Discussion started by: strider
1 Replies

6. IP Networking

message queue problem

I am sending and retriving the message to the queue the problem is after retrieving the message can i see what is there in my message queue. (actually in my application i am encountring some garbage value) so i want to retieve this garbage value and also want to know its size how... (0 Replies)
Discussion started by: ramneek
0 Replies

7. Programming

Message Queue Problem Again..

Is there any way one can delete , say , a particular message from a message queue on system V? (2 Replies)
Discussion started by: satansfury
2 Replies

8. Programming

Deleting ALL message queue

hi all, I'm working on this problem for 2 days. Can somebody tell me that how to delete all message queues from the system ? Since "ipcs -q" gives the list of all existing message queue, then there must be a system call and data stucture where from I can fetch the data about all existing... (2 Replies)
Discussion started by: v_rathor
2 Replies

9. UNIX for Dummies Questions & Answers

Cron message queue problem

I have a problem with running jobs out of cron on Solaris 8. Initially when one of the users on the box (other than root) attempted to save the crontab after modification by using "crontab -e", the message "Crontab: cannot open the crontab file in the crontab directory" was given. I then... (7 Replies)
Discussion started by: mattd
7 Replies

10. UNIX for Advanced & Expert Users

Unix message Queue

Hi, I am working closly with unix message queues i have encountered the following - after creating the Q and start working with it (pushing & pulling) i receive the following stange parameters on the q's - STIME=no_entry Qnum=0 CBYTES=4140 when this happens, the Q is disabled (meaning i... (3 Replies)
Discussion started by: kel
3 Replies
Login or Register to Ask a Question