LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Can we use waitpid without using fork/child processes? (https://www.linuxquestions.org/questions/linux-newbie-8/can-we-use-waitpid-without-using-fork-child-processes-4175546220/)

kol_bro 06-23-2015 01:10 PM

Can we use waitpid without using fork/child processes?
 
Hi,

i am creating a watchdog for an app, for this i am using waitpid, but i cant use fork(), So is there any way that i could use waitpid in my watchdog

if YES, kindly let me know with an example that would be helpful


Thanks:)

rtmistler 06-24-2015 05:51 AM

You cannot.

wait(2) and waitpid(2) both are designed where the signals are sent from a child process to its parent process for the express purpose of indicating a change in state of the child process to its parent.

You can find an alternative way to monitor a process, either looking at the code for things like ps(1) or top(1) or one of the associated utilities, however the correct way to do what you are doing is to use fork(2).

And is the "can't use fork()" statement because this is an assignment?

kol_bro 06-24-2015 06:21 AM

hi rtmistler,

as i have told before i am creating a watchdog in c langauge for an app, and i cant use fork becuase my watchdog will run on uclinux which is not currently supporting FORK, fork method is not implemented in it yet...


thanks.

jpollard 06-24-2015 08:09 AM

Quote:

Originally Posted by kol_bro (Post 5381797)
Hi,

i am creating a watchdog for an app, for this i am using waitpid, but i cant use fork(), So is there any way that i could use waitpid in my watchdog

if YES, kindly let me know with an example that would be helpful


Thanks:)

No, and yes....

You can't change the default reaper... but you can specify a "subreaper" proces to collect the status.

Check the man page on "prctl" and the option PR_SET_CHILD_SUBREAPER. I haven't found an example using it yet...

The documentation indicates that once it is set by a process, then children of that process will get reparented to the specified process (as long as it exists) before being reparented to init. It still calls for doing this in the parent of whatever starts the process you want monitored though. It looks like a simple wrapper utility would work.

kol_bro 06-25-2015 07:16 AM

i am creating watchdog using ptrace and waitpid, do u think it will be helpful, i am doing something like this


Code:

long ret = ptrace (PTRACE_ATTACH, pid, NULL, NULL);

               
                ret = ptrace (PTRACE_CONT, pid, NULL, NULL);

               
                do {
                        int w = waitpid(pid, &status, 0);
                        if (w == -1) {
                                perror("waitpid error :");
                                exit(EXIT_FAILURE);
                        }

                        if (WIFEXITED(status)) {
                                printf("exited, status=%d\n", WEXITSTATUS(status));
                        } else if (WIFSIGNALED(status)) {
                                unexpected_app_shoutdown++;
                                printf("killed by signal %d\n", WTERMSIG(status));
                        } else if (WIFSTOPPED(status)) {
                                printf("stopped by signal %d\n", WSTOPSIG(status));
                               
                                unexpected_app_shoutdown++;
                                kill(pid, SIGQUIT);
                        }/* else if (WIFCONTINUED(status)) {
                            printf("continued\n");
                            }*/
                } while (!WIFEXITED(status) && !WIFSIGNALED(status))

;



What you think about it

For me its creating two problems
1-it need root access
2-ptrace create zombie processes sometimes

kindly give me better solution/suggestion if u have...

Thanks

rtmistler 06-25-2015 12:15 PM

But you said you cannot use fork().

Therefore from where you're invoking ptrace(), you are not the parent. Correct?

ptrace() is for a parent process to observe and control execution of a child process.

The solution I'd offer would be to use fork() and in the case where fork() returns a positive valued pid, you are then in the parent process and hence you perform your do-while{} loop using waitpid(). I took a brief look and the logic seems sound for that do-while{} loop, therefore I'd say that you should not have zombies providing the exits from waitpid() were never in error.

jpollard 06-25-2015 02:40 PM

Ptrace can attach to any process with the same owner as the process using the ptrace... unless that process is root, in which case it can attach to any process.

The problem is that it inherently has a race condition, and will not work in all cases. One is where the process already has a ptrace track on it... This is done (in at least one case) to disable the use of ptrace.

What you have described is the purpose of "prctl" and the option PR_SET_CHILD_SUBREAPER.


All times are GMT -5. The time now is 12:18 AM.