Wait status

hi all!

In my C++ program I have a parent process which forks 5 children processes.The processes do a job and then they have to do some sort of sleeping(not terminate) until the parent wakes them up again.There might be 1,2,5 or even 0 processes awake at any moment.The thing is that in the parent process there's the

int status;
wait(&status);

for every process.But this refers to the termination of each process.How can a process do a job and then "sleep" instead of terminate.I guess with some sort of a signal but I have absolutely no idea how this can be achieved.
Any ideas?

The most obvious way would be a pipe... The child reads from the read end of the pipe, but when there's nothing to be read, that will cause it to wait. The parent writes when it wants to wake the child up.

1 Like

So in that case the parent doesn't have to use the wait-status at all right?
(I forgot to mention that when the parent wants all of its children to terminate it sends a certain signal..)

wait() waits for children to die. If they're not supposed to die, don't wait() at all.

1 Like

I see.So if I understood correctly the parent has a pipe for each of its children and depending on what child it wants to wake up writes to that pipe right?
But how does the parent know which children are "sleeping" in order to write to their pipe and not to the pipe of a child which is running?

Does it matter? It writes the message and whoever gets it, gets it and deals with it when they read it.

To put it another way, what are you actually trying to do here by waking up processes?

The parent process sends a string message to the children in order to do some things.When a child is finished waits for the next string message.Only one child can deal with each message but there's a chance that more than one children deal with messages at the same time.That happens because while a child is dealing with a msg, the parent might want to send another msg which has to be received from a child that does nothing at the time,so in that case 2 children are working at the same time.My question was how the parent knows which of its children are "waiting" and which are "working",in order to send the msg to one of the sleeping ones.If I use pipes,do I have to use one for each child?Also how do I make sure that no more than one children gets the msg string?

Not what I mean. I mean, what problem are you trying to solve with this? Not how you want to do it -- what it's for.

I have a main parent code that collects data from every process it forks and then prints all of the data to a file.a child starts collecting data only when receiving a message.Otherwise waits.
The parent through a loop sends a certain number of messages to the children.
For example the parent sends 4 messages,and each one has to be received of one child that is waiting at the moment.When receiving the message it starts

:wall:

You're just rephrasing what you want to do, over and over. I'm not looking for that.

I want to know what actual function you want to use this program for. Because I can think of lots of solutions for you, but nothing which obeys what you want to the letter, so knowing what you actually need would be better than knowing how you're hellbent on doing it.

Without any actual problem you're trying to solve beyond programming itself, it sounds contrived, like homework. And you've avoided answering, so I think it is. And therefore against the rules.