# include
# include
# include
# define MSGKEY 75
Struct msgform
{long buy;
Char mtext [1030]. } MSG.
Int msgqid, I;
Void CLIENT ()
{int I;
Msgqid=msgget (MSGKEY, 0777);
For (I=10; I>=1; I -)
{MSG. What=I;
Printf (" (the client) sent \ n ");
MSGSND (msgqid, & amp; MSG, 1024, 0); }
exit(0); }
Void SERVER ()
{msgqid=msgget (MSGKEY, 0777 | IPC_CREAT);
Do {MSGRCV (msgqid, & amp; MSG, 0, 1030);
Printf (" (server) received \ n "); }
While (MSG. What!=1);
MSGCTL (msgqid, IPC_RMID, 0);
exit(0); }
Void main () {
While ((I=the fork ())==1);
if(! I) SERVER ();
While ((I=the fork ())==1);
if(! I) the CLIENT ();
Wait (0);
Wait (0); }
This is a realization of interprocess communication code, but it doesn't run out of the correct results (alternate shows ten times) is where there is a problem, please? Thank you bosses!
CodePudding user response:
Void main ()
{
While ((I=the fork ())==1);
if(! I) SERVER ();
Usleep (1000);
While ((I=the fork ())==1);
if(! I) the CLIENT ();
Wait (0);
Wait (0);
}
Pay attention to the coding standards
CodePudding user response:
@ mouse_zhu bosses in the way you did, I although appear alternately, but the result or SERVER function infinite jumping outCodePudding user response: