Find the answer to your Linux question:
Results 1 to 9 of 9
hello, how to make a process wait for another process(not its child) when it knows its PID...
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Location
    Algeria
    Posts
    26

    Smile wait for a process;

    hello,
    how to make a process wait for another process(not its child) when it knows its PID

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by khlitoshi View Post
    hello,
    how to make a process wait for another process(not its child) when it knows its PID
    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.

  3. #3
    Just 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;

  4. #4
    Just Joined!
    Join Date
    Dec 2007
    Location
    Algeria
    Posts
    26
    i tried waitpid() it doent work, it says error: no child processes;

  5. #5
    Just 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);

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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.

  7. #7
    scm
    scm is offline
    Linux 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.

  8. #8
    Just 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

  9. #9
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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:
    1. One finishing process reads the 3 from shared memory.
    2. That process decrements it to 2 and writes it back to shared memory.
    3. The other finishing process reads the 2 from shared memory.
    4. 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:
    1. One finishing process reads the 3 from shared memory.
    2. The other finishing process also reads the 3 from shared memory.
    3. 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...