I am writing a code in C on a unix system. I have created a message queue server. Each time I receive a new message I fork and the child process handles the new client. The server waits for new client. Here's the code.
for (;;)
{
struct my_msgbuf buf;
if (msgrcv (msqid, &(buf.mtype), sizeof (buf), 1, 0) == -1)
perror ("msgrcv");
if((pid = fork())<0)
perror("fork");
if(pid==0)
{
//child code
}
}
Now the code works for the first iteration but on the second iteration msgrcv gives the following error - msgrcv: Invalid Arguments instead of waiting for new messages.
How do I fix this?