Visit Our UNIX and Linux User Community


Message queues


 
Thread Tools Search this Thread
Top Forums Programming Message queues
# 1  
Old 05-10-2005
Message queues

Hi all,

I've been trying for hours to figure out how to turn my 2-program (one to send and one to receive) "chat system" using message queues, into a single program where each concurrent component (entity) will both send and receive messages. PLEASE give me a hand with this, I'm starting to think I'm stupid (Maybe I am, but only I can say it Smilie )

THANX!!

Here I give you the code to both programs. Both of them are working fine:

Code:
/*Sending Program*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

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


main()
{
        int running=1;
        int msgid;
        char buffer[BUFSIZ];
        struct my_msg_st some_data;

        msgid=msgget((key_t)1234, 0666 | IPC_CREAT);

        if(msgid == -1)
        {
                fprintf(stderr, "msgget failed\n");
                exit(EXIT_FAILURE);
        }

        while(running)
        {
                printf("Enter some text: ");
                fgets(buffer, BUFSIZ, stdin);
                some_data.my_msg_type= 1;
                strcpy(some_data.some_text, buffer);

                if(msgsnd(msgid, (void*)&some_data, BUFSIZ, 0) == -1)
                {
                        fprintf(stderr, "msgsnd failed\n");
                        exit(EXIT_FAILURE);
                }

                if (strncmp(buffer, "end", 3) == 0)
                {
                        running=0;
                }
        }

        exit(EXIT_SUCCESS);
}

Code:
/*Receiving program*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

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


main()
{
        int running=1;
        int msgid;
        struct my_msg_st some_data;
        long int msg_to_receive=0;

        msgid=msgget((key_t)1234, 0666 | IPC_CREAT);

        if(msgid == -1)
        {
                fprintf(stderr, "msgget failed\n");
                exit(EXIT_FAILURE);
        }

        while(running)
        {
                if(msgrcv(msgid, (void *)&some_data, BUFSIZ, msg_to_receive, 0) == -1)
                {
                        fprintf(stderr, "msgrcv failed\n");
                        exit(EXIT_FAILURE);
                }

                printf("You wrote: %s", some_data.some_text);

                if(strncmp(some_data.some_text, "end", 3) == 0)
                {
                        running=0;
                }
        }

        if(msgctl(msgid, IPC_RMID, 0) == -1)
        {
                fprintf(stderr, "msgctl(IPC_RMID) failed\n");
                exit(EXIT_FAILURE);
        }

        exit(EXIT_SUCCESS);
}


Last edited by mgchato; 05-10-2005 at 06:43 AM..
# 2  
Old 05-11-2005
You mean that you want the same program to send and receive messages, but how do you plan to run this exactly?
Will the program recv messages that it has itself sent? Or will there be two or more instances of the program running, each putting up messages of its own, while 'recv'ing all other messages?
# 3  
Old 05-12-2005
Quote:
Originally Posted by blowtorch
...Or will there be two or more instances of the program running, each putting up messages of its own, while 'recv'ing all other messages?
That's the one I'm looking for.
# 4  
Old 05-13-2005
You would be best off having two programs, but not in the same way that you have created. Have one program that is the "server". This will create the message queue when it starts up, and destroy the queue when it shuts down. The second program will be the client program that will just connect to the message queue (no IPC_CREAT).

The client program will put messages with a type flag set to some unique value ( using the program's pid would be a good idea ); while msgrcv will be done with type set to the the unique value, but with the flag set to MSG_EXCEPT - this will pick up messages with any type that is not the value set as msgtyp in the msgrcv call.

A single program will probably complicate things unnecessarily.
# 5  
Old 05-13-2005
Yeah, I DID think think about a server/client approach to this problem, but since I'm fairly new to UNIX I didn't know how to apply my limited knowledge to this type of implementation, so I just tried the above solution taking bits and pieces from textbooks and from what I learned at uni. Would u happen to have a sample code for this?? I'm sorry I'm such a pain in the ass, but I can admit when I need help, and this is one of those ocassions.

Thanx a lot for your help blowtorch, you're a legend.
# 6  
Old 05-13-2005
I know you already answered this question, but will there be exactly two or more than two processes communicating between each other? This would affect how the program will work.
# 7  
Old 05-16-2005
I'm supposed to write a set of programs that allow users logged on to a host to send messages to each other.

At first, I should start with this messenger program I'm telling you about and add to it a coordination program. Since the coordinator should accept messages from the clients and forward them to all attached clients, I think that the answer to your question is that there will be more than 2 processes communicating between each other.

THANX!!!

Previous Thread | Next Thread
Test Your Knowledge in Computers #378
Difficulty: Medium
AIX was UNIX 03 compliant.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Performance calculation for Message Queues

i have a program(C++ Code) that sends/receives information through queue's (Uses MQ) Is there any UNIX/LINUX tool that calculates the load and performance time for the same. If not how do i design the program that calculates the performance time. i know that time.h can be used but it gives... (2 Replies)
Discussion started by: vkca
2 Replies

2. Shell Programming and Scripting

Cleaning Message Queues

i have an application installed on AIX 5.3 and i have made a script that shutdown a proccesses that exceeded 10000kb of memory usage but i have a problem with cleaning the message queues of these proccesses after shutting them down. Is there any way to clean the message queues for this particular... (8 Replies)
Discussion started by: Portabello
8 Replies

3. Programming

Persisting message queues to disk

Hi, I have searched the forums and could not find a relavant thread discussing my use case, hence the new post. Basically am trying to pass on work to dummy worker instances from controller which will pass on work to workers (client) To make use of host capacity, am planning to serialize... (2 Replies)
Discussion started by: matrixmadhan
2 Replies

4. UNIX for Dummies Questions & Answers

message queues

can any body provide a tutorial that explains the concept of message queues in UNIX in great detail (1 Reply)
Discussion started by: asalman.qazi
1 Replies

5. UNIX for Advanced & Expert Users

message queues

#include <sys/ipc.h> #include <sys/msg.h> int main() { int qid; int t; struct msgbuf mesg; qid=msgget(IPC_PRIVATE,IPC_CREAT); mesg.mtype=1L; mesg.mtext=1; t=msgsnd(qid,&mesg,1,0); printf("%d",t); } the program prints -1 as the result of msgsnd ,which means that msgsnd doesn't... (1 Reply)
Discussion started by: tolkki
1 Replies

6. UNIX for Advanced & Expert Users

UNIX Message Queues vs. Sockets

If I use sockets for IPC, and can easily distribute my applications. UNIX Message Queues are local to the processor. As I understand it, Message Queues still incur system call overhead, just like socket calls. What advantage does a UNIX Message Queue provide versus a TCP or UDP Socket,... (2 Replies)
Discussion started by: zen29sky
2 Replies

7. Programming

shared memory and message queues

Hi, According to my understanding.. When message queues are used, when a process post a message in the queue and if another process reads it from the queue then the queue will be empty unlike shared memory where n number of processess can access the shared memory and still the contents remain... (2 Replies)
Discussion started by: rvan
2 Replies

8. Linux

maximun number of message queues

how to check the maximun number of message queues in current linux enviornment? is there any command ? (4 Replies)
Discussion started by: princelinux
4 Replies

9. Solaris

rogue message queues solaris 9

We have message queues created from our ERP system to our tax system via an application api written by the ERP software vendor. Occasionally when a user does not gracefully exit the ERP application, the message queue hangs. After a few months, this becomes a problem as the queues are all used... (2 Replies)
Discussion started by: MizzGail
2 Replies

10. UNIX for Dummies Questions & Answers

message queues

let 3 processes a, b and c are sharing msgs using msg queues.process 'a' sending msg to 'c' and in turn 'c' send sthat msg to 'b'.if something happens to c how can 'a' and 'b' know that 'c' is not available?????? (2 Replies)
Discussion started by: sukaam
2 Replies

Featured Tech Videos