0

Here a piece of code showing the problem

//main

Process* process = NULL;
while(!process) {
    cout<<endl<<"Waiting for second process.\nPress any key";
    getchar();
    getchar();

    process = Process::takeExisting("process");
}
Process::waitEnd(process); //Problem here
cout<<endl<<"second process ended";

//Process::waitEnd

static void waitEnd(Process* proc) {
        int w = waitpid(proc->hProcess, &(proc->exitCode), WCONTINUED);
        Debug::error(errno," waitpid error - ");
    }

I've tried: stop first, wait second ends, continue first.

The problem: the second process is not child( I run it from second console ) so with this code errno is equal to 10. How to solve this on Linux?

Nikita
  • 1,019
  • 2
  • 15
  • 39
  • What are you trying to do? – Dietrich Epp Jun 23 '16 at 22:32
  • @DietrichEpp run first process, wait while second not open, open another console, run second process, wait while it ends, continue first – Nikita Jun 23 '16 at 22:37
  • What do you mean by "wait while second not open"? What are you waiting for? – Dietrich Epp Jun 23 '16 at 22:48
  • What does `Process::takeExisting()` do, and how does it do it? You may need to look at Linux's [`clone()`](http://linux.die.net/man/2/clone). However, in general, a process cannot inherit processes it did not spawn — the exception being the system process that inherits orphaned child processes. – Jonathan Leffler Jun 23 '16 at 22:51
  • @JonathanLeffler Process is my wrapper-class for processes. Process::takeExisting() - wraps real process with my class. – Nikita Jun 23 '16 at 22:56
  • 1
    You cannot `wait()` or `waitpid()` for a process that you did not create. How is the process created? – Dietrich Epp Jun 23 '16 at 22:57
  • @DietrichEpp Yes I know, I run it in the second console, how to wait for such process? – Nikita Jun 23 '16 at 23:04
  • Did you write both applications? Or are you in control of both of them? Why not making the second one connect to a socket / send a signal / use any other IPC to talk back to the first one? – peppe Jun 23 '16 at 23:08
  • 2
    Btw, the typical workarounds for waiting for a non-child process to finish (which you can't normally do) are `ptrace`ing it and waiting for an `exit_group` syscall, or polling by sending via `kill` the signal 0 to the other process and checking for `ESCHD`. – peppe Jun 23 '16 at 23:11
  • @peppe Just because I have the same Process::waitEnd() on windows and it works fine with WaitForSingleObject. I need to write the similar interface on linux. I hope that it possible to wait for other process, that not created by the first) – Nikita Jun 23 '16 at 23:13
  • @peppe thanks I will try this – Nikita Jun 23 '16 at 23:14
  • Using `kill` on a non-child process is unreliable. You can't make it race-free. – melpomene Jun 24 '16 at 04:58

0 Answers0