Home > OS >  For help! Linux beginner, consult the code
For help! Linux beginner, consult the code

Time:09-22

# include
# 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 out

CodePudding user response:

reference
but the result or SERVER function infinite out

What do you mean

CodePudding user response:

reference mouse_zhu reply: 3/f
reference
but the result or SERVER function infinite jump out to

What do you mean
in the way you run on my computer, alternate displays infinite (server) received, but run on my classmate's computer have no problem, although I don't know why, but thank you!
  • Related