Sponsored Content
Top Forums Programming [C]Problem removing a message queue Post 302203546 by Sentinella on Monday 9th of June 2008 05:59:17 AM
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;
      }
	
} 
}

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
MSGGET(2)						      BSD System Calls Manual							 MSGGET(2)

NAME
msgget -- get message queue identifier LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <sys/msg.h> int msgget(key_t key, int msgflg); DESCRIPTION
The msgget() system call returns the message queue identifier associated with key. A message queue identifier is a unique integer greater than zero. A message queue is created if either key is equal to IPC_PRIVATE, or key does not have a message queue identifier associated with it and the IPC_CREAT bit is set in msgflg. If both the IPC_CREAT bit and the IPC_EXCL bit are set in msgflg, and key has a message queue identifier associated with it already, the operation will fail. If a new message queue is created, the data structure associated with it (the msqid_ds structure, see msgctl(2)) is initialized as follows: o msg_perm.cuid and msg_perm.uid are set to the effective uid of the calling process. o msg_perm.gid and msg_perm.cgid are set to the effective gid of the calling process. o msg_perm.mode is set to the lower 9 bits of msgflg. o msg_qnum, msg_lspid, msg_lrpid, msg_rtime, and msg_stime are set to 0. o msg_qbytes is set to the system wide maximum value for the number of bytes in a queue (MSGMNB). o msg_ctime is set to the current time. RETURN VALUES
Upon successful completion a positive message queue identifier is returned. Otherwise, -1 is returned and the global variable errno is set to indicate the error. ERRORS
[EACCES] A message queue is already associated with key and the caller has no permission to access it. [EEXIST] Both IPC_CREAT and IPC_EXCL are set in msgflg, and a message queue is already associated with key. [ENOSPC] A new message queue could not be created because the system limit for the number of message queues has been reached. [ENOENT] IPC_CREAT is not set in msgflg and no message queue associated with key was found. SEE ALSO
msgctl(2), msgrcv(2), msgsnd(2), ftok(3) STANDARDS
The msgget system call conforms to X/Open System Interfaces and Headers Issue 5 (``XSH5''). HISTORY
Message queues appeared in the first release of AT&T System V UNIX. BSD
May 13, 2004 BSD
All times are GMT -4. The time now is 01:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy