Home > OS >  Help the child to communication problems
Help the child to communication problems

Time:09-25

A great god for help all of you!
A parent p0, create two child process p1 and p2. Only use the signal mechanism as a means of communication, let p1 print out p2 pid, p2 print out p1 pid,

The current thinking is a child record two by the main process of PID, and respectively p1 of PID through the signal is sent to p2, p2 PID through signal to p1, but don't know how to implement? Thank you bosses!

 
Int main ()
{
Pid_t pid1=0 XFFFF, pid2=0 XFFFF, proc1, proc2;
If (pid1!=0 & amp; & Pid2!=0)//the parent process?
Pid1=fork ();
If (pid1!=0 & amp; & Pid2!=0)//the parent process?
Pid2=fork ();
If (pid1!=0 & amp; & Pid2!=0)//the parent process?
{
//TODO
}
return 0;
}

CodePudding user response:

You can use anonymous pipes, anonymous pipe can be used for messages, a parent and child process using signal this seems impossible
 
Int the FDS [2];
Pipe (FDS);//initialize a pipeline
if(! Fork ())
{
Close (FDS [0]);//close the read end
Write (FDS [1], "do you want to transfer messages", strlen (" do you want to transfer the message "));
}
The else
{
Close (FDS [1]);//close the write end
Char buf [128]={0};
Read (FDS [0], buf, sizeof (buf));
Printf (" % s \ n ", buf);
}

This is I used to write a small example, you can refer to, who opened the write end, a parent and child process who open the read comprised of your demand, the FDS [0] said read end, FDS [1] said to write end
  • Related