Sponsored Content
Full Discussion: C Posix - msgsnd() msgrcv
Top Forums Programming C Posix - msgsnd() msgrcv Post 302285001 by carl33p on Friday 6th of February 2009 06:21:10 PM
Old 02-06-2009
C Posix - msgsnd() msgrcv

Hey guys, Im doing message passing for the first time on a linux OS. Im new to C programming, so bear with me. I made two .c files : central.c and external.c

I simply wanted to pass a message from the central process to the external process. BUT Whenever each process gets to the msgsnd()/msgrcv() method the processes stall there. In the command line I start both processes with:
Code:
./central 60 & ./external 1 60

central.c
Code:
#include <stdio.h> 
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/msg.h>

typedef struct mymsg{
 long priority;
 int temp;
 int pid;
 int stable;
}msgp;

int main(int argc, char *argv[])
{
 msgp msgp;
 msgp.priority=2;
 msgp.temp=(int) argv[1];
 msgp.pid=70;
 msgp.stable=0;

//Create central mailbox
 int stat, msqid;
 if ((msqid = msgget(0070, 0666 | IPC_CREAT)) < 0) {
   perror("msgget");
   return 1;
 }
 else 
   (void) fprintf(stderr,"Central msgget: msgget succeeded: msqid = %d\n", msqid);

//Send message to external processes
 stat = msgsnd(msqid, &msgp, sizeof(msgp) - sizeof(long), 0);
 if ( stat < 0) {
    printf ("Insert details of message sent");
    perror("msgsnd");
    return 1;
 }
 else {
   printf("Central successfully sent the Message");
 }

 //temp=(2*temp+exTemps)/6;
 return 0;

}

external.c
Code:
#include <stdio.h> 
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/msg.h>

typedef struct my_msg{
 long priority;
 int temp;
 int pid;
 int stable;
}msgp;

int main(int argc, char *argv[])
{
 msgp msgp;
 msgp.priority=2;
 msgp.temp=(int) argv[1];
 msgp.pid=(int) argv[2];
 msgp.stable=0;

 int msqid, stat;

//Create external mailbox
 if ((msqid = msgget(0070, 0666 | IPC_CREAT)) < 0) {
   perror("msgget");
   return 1;
 }
 else 
   (void) fprintf(stderr,"External msgget: msgget succeeded: msqid = %d\n", msqid);

//Wait to receive message
 stat = msgrcv(msqid, &msgp, sizeof(msgp)-sizeof(long), 2, 0);
 if (stat < 0) {
    printf ("Insert details of attempted received message");
    perror("msgrcv");
    exit(1);
 }
 else {
   printf("External successfully received the Message");
 }

 return 0;

}

Any comments or help would be much appreciated!Smilie

Last edited by carl33p; 02-06-2009 at 09:16 PM..
 

10 More Discussions You Might Find Interesting

1. Programming

msgrcv pending forever !!!

When I am using msgrcv to get a message from a queue, in case of msgsnd some error, the msgrcv thread will waiting forever. Is there some way that I can specify a time out value for this queue ? just let msgrcv wait for some time, if no message comes during this time slot, msgrcv just return... (3 Replies)
Discussion started by: Yun Gang Chen
3 Replies

2. UNIX for Dummies Questions & Answers

msgrcv : Invalid argument

Hi All, Please guide me how to get rid : msgrcv : Invalid argument. I am using message queues: msgsnd and msgrcv, I am able to send through msgsnd and receive through msgrcv, but at times i get the belo error. msgrcv : Invalid argument. (1 Reply)
Discussion started by: answers
1 Replies

3. AIX

msgrcv crash in AIX 5.3 SP6

Hi All, I have a piece of code like blow. It is working fine with AIX 5.3 SP3. When the same thing is getting executed in AIX 5. SP6 it is giving segmentation fault. If I put debug statements before and after, it works fine. rc = msgrcv(mqid, &msg, size, HIGH_PRIORITY_FIRST , 0) ... (0 Replies)
Discussion started by: sandhya_chp
0 Replies

4. Programming

Posix

HI, When i am configuring php in SUN Solaris. I am getting the below error. configure: error: Your system seems to lack POSIX threads. Do i need to install POSIX? If so can somebody let me know where can i download POSIX for Solaris 8? Thanks, (2 Replies)
Discussion started by: Krrishv
2 Replies

5. UNIX for Advanced & Expert Users

Posix threads

Hi, consider the code below: #include <stdio.h> . . struct myStruct { char *message ; int id; }; . . . void *thread_function( void *ptr ); nt main() { pthread_t thread1, thread2 ,thread3 ; struct myStruct nico1; (2 Replies)
Discussion started by: Behnaz
2 Replies

6. What is on Your Mind?

Linux posix

Hi everybody, i couldn't think of any better place to ask this question. Does LINUX totally confirm with ALL of the POSIX standards??. If not which areas does it diverge?? my apologies if this questions seems sooo stupid to some of you.. thanks (0 Replies)
Discussion started by: abhiram7
0 Replies

7. UNIX for Dummies Questions & Answers

IPC Message Queue. msgrcv doesnt work..

Hi everybody, this is the situation. there is a programm XYZ which opens a message queue with the key 47110815 and waits for a SIGUSR1. After receiving this signal it sends a message with type 100 and a number (as ASCII) in the message-body. I have to write a prog which frist sends the... (1 Reply)
Discussion started by: daredevil82m
1 Replies

8. UNIX for Advanced & Expert Users

System V or POSIX

Hi , I am using UNIX network programming Vol1 (by R Stevens) book to learn about IPC. I would be using HP-UX,Solaris and Linux at my work. I have sections for POSIX and for System V in that book. I am quite confused in indentifying those OSs as POSIX or SYstem V. Can anyone please... (1 Reply)
Discussion started by: kumaran_5555
1 Replies

9. Programming

POSIX Thread Help

I want to create a program that creates 2 child process, and each of them creates 2 threads, and each thread prints its thread id. I0ve allread done that the outuput isn't the outuput i want. When a run the following comand "$./a.out | sort -u | wc -l" I have the folowing output 2 $: It should... (3 Replies)
Discussion started by: pharaoh
3 Replies

10. Programming

Message queue is not blocked in msgsnd

Hi, I am trying to send/receive data by message queue and expecting it to be blocked on send/read for other side (at least this is my understooding ) , I am connecting message between perl<->C , perl is working as expected , but in C msgsnd and msgrcv are not waiting (blocked) untill second side... (6 Replies)
Discussion started by: alexse
6 Replies
MSGRCV(2)						      BSD System Calls Manual							 MSGRCV(2)

NAME
msgrcv -- receive a message from a message queue LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> int msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg); DESCRIPTION
The msgrcv() function receives a message from the message queue specified in msqid, and places it into the structure pointed to by msgp. This structure should consist of the following members: long mtype; /* message type */ char mtext[1]; /* body of message */ mtype is an integer greater than 0 that can be used for selecting messages, mtext is an array of bytes, with a size up to that of the system limit (MSGMAX). The value of msgtyp has one of the following meanings: o The msgtyp argument is greater than 0. The first message of type msgtyp will be received. o The msgtyp argument is equal to 0. The first message on the queue will be received. o The msgtyp argument is less than 0. The first message of the lowest message type that is less than or equal to the absolute value of msgtyp will be received. The msgsz argument specifies the maximum length of the requested message. If the received message has a length greater than msgsz it will be silently truncated if the MSG_NOERROR flag is set in msgflg, otherwise an error will be returned. If no matching message is present on the message queue specified by msqid, the behavior of msgrcv() depends on whether the IPC_NOWAIT flag is set in msgflg or not. If IPC_NOWAIT is set, msgrcv() will immediately return a value of -1, and set errno to ENOMSG. If IPC_NOWAIT is not set, the calling process will be blocked until: o A message of the requested type becomes available on the message queue. o The message queue is removed, in which case -1 will be returned, and errno set to EINVAL. o A signal is received and caught. -1 is returned, and errno set to EINTR. If a message is successfully received, the data structure associated with msqid is updated as follows: o msg_cbytes is decremented by the size of the message. o msg_lrpid is set to the pid of the caller. o msg_lrtime is set to the current time. o msg_qnum is decremented by 1. RETURN VALUES
Upon successful completion, msgrcv() returns the number of bytes received into the mtext field of the structure pointed to by msgp. Other- wise, -1 is returned, and errno set to indicate the error. ERRORS
The msgrcv() function will fail if: [EINVAL] The msqid argument is not a valid message queue identifier. The message queue was removed while msgrcv() was waiting for a message of the requested type to become available on it. The msgsz argument is less than 0. [E2BIG] A matching message was received, but its size was greater than msgsz and the MSG_NOERROR flag was not set in msgflg. [EACCES] The calling process does not have read access to the message queue. [EFAULT] The msgp argument points to an invalid address. [EINTR] The system call was interrupted by the delivery of a signal. [ENOMSG] There is no message of the requested type available on the message queue, and IPC_NOWAIT is set in msgflg. SEE ALSO
msgctl(2), msgget(2), msgsnd(2) HISTORY
Message queues appeared in the first release of AT&T System V UNIX. BSD
July 9, 2009 BSD
All times are GMT -4. The time now is 12:48 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy