Results 1 to 9 of 9
hello,
how to make a process wait for another process(not its child) when it knows its PID...
- 03-21-2008 #1Just Joined!
- Join Date
- Dec 2007
- Location
- Algeria
- Posts
- 26
wait for a process;
hello,
how to make a process wait for another process(not its child) when it knows its PID
- 03-21-2008 #2Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
In bash you have the wait builtin:
wait [n ...]
Wait for each specified process and return its termination status. Each n
may be a process ID or a job specification; if a job spec is given, all pro‐
cesses in that job’s pipeline are waited for. If n is not given, all cur‐
rently active child processes are waited for, and the return status is zero.
If n specifies a non-existent process or job, the return status is 127.
Otherwise, the return status is the exit status of the last process or job
waited for.
- 03-21-2008 #3Just Joined!
- Join Date
- Dec 2007
- Location
- Algeria
- Posts
- 26
waitpid(); i found;
ill try it; yours is a bit more complicated,but ill try it too, thank u very much;
- 03-21-2008 #4Just Joined!
- Join Date
- Dec 2007
- Location
- Algeria
- Posts
- 26
i tried waitpid() it doent work, it says error: no child processes;
- 03-21-2008 #5Just Joined!
- Join Date
- Dec 2007
- Location
- Algeria
- Posts
- 26
for wait(); it tells me invalid argument ;
waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);
i used it like this: waitid(P_PID,pid,NULL,0);
- 03-21-2008 #6
Assuming you're using C or C++, you can't use wait() or waitpid() to wait for a process that is not a child of the current process.
Edit: Same for waitid().--
Bill
Old age and treachery will overcome youth and skill.
- 03-23-2008 #7Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
One way I can think of is to loop on kill -0 PID and wait until you get an exit status of no such process. You'll have to run as root to monitor processes with user IDs that aren't yours.
- 03-28-2008 #8Just Joined!
- Join Date
- Dec 2007
- Location
- Algeria
- Posts
- 26
i found a solution and it works, i created a shared memory(int) and i initialised it with the number of processes that i should wait for, and every process that finishes it make a minus one for this shm,
the processus that must wait for them always test this shm if it still greater than zero.
may be complicated but it works
- 03-28-2008 #9
That solution will work most of the time. But you can't guarantee it will work all the time.
Let's say there are three process running. Two of them finish about the same time.
What we hope happens is this:
- One finishing process reads the 3 from shared memory.
- That process decrements it to 2 and writes it back to shared memory.
- The other finishing process reads the 2 from shared memory.
- That process decrements it to 1 and writes it back to shared memory.
So far so good. But if the two processes finish at closer to the same time, this can happen:
- One finishing process reads the 3 from shared memory.
- The other finishing process also reads the 3 from shared memory.
- Each of the two processes (it doesn't matter which order this happens in) decrements it to 2 and writes it back to shared memory.
Unlikely, yes. And if you don't need it to be bulletproof, why bother to fix it?
But for those who absolutely need to get it right, such as implementers of database software like Oracle, it is important to pay attention to synchronization details like this.--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote
