Hanging while using msgsnd and msgrcv
Hi All,
I see a hang on executing below source.. > Can we have have msgsnd and msgrcv between processes?Examples? > I understand the parent process is hung msgrcv, What is wrong with this approach? Below is the code: Code:
int main() Thanks in Advance.. |
On this line:
Code:
... |
No, that's not it. The parent process needs to wait for a message.
Are you checking the result of all these calls to see if an error occurred? |
Thanks for the reply.....
I have modified the source with following: Code:
msgsnd(msgqid,&msgdata,sizeof(struct data),IPC_NOWAIT); Observations are it is now able to receive ....but junk and also the child process is not exiting hence the hang.... Following is the output: Code:
msgsnd error : Invalid argument |
Have a look at the man pages for msgsnd/msgrcv, also this may help, its quite old but still works:
http://cli-apps.org/content/show.php...content=148626 |
what is 'mymsg_stuct' and 'data' defined as? we really need to know what your private data structures are defined as so the code can be debugged properly.
|
struct pdata{
long mtype; struct data { //char name[30]; int k; int empid; }mine_info; }; typedef struct pdata mymsg_stuct; |
Right took some sorting but this
Code:
#include <stdio.h> To start you was hanging on the scanf after sending the msg so the child was not exiting, second you have to set 'msgdata.mtype' which is a simple message type id ( in this case '666' ), you were setting it to '2' in the msgrcv but not setting it before using msgsnd. The reason you were getting nonsense or a segfault when getting the msg is that your printf expects a null terminated c string but you are giving it an int, so use "printf("Rcv from child %i\n",msgdata.mine_info.k);" Hope this helps. |
Thanks for your time!!!!!
What is the reason for the child process to hang with scanf() around?....my objective was to read a value and send it across the parent? |
scanf read from stdin so it was just sitting waiting for input, its not the best way to get input as it can overflow the buffer, see the man page.
Don't forget to mark the thread SOLVED, and you can click on the 'Did you find this post helpful? Yes' link if you want ;) |
All times are GMT -5. The time now is 06:35 PM. |