Can we use waitpid without using fork/child processes?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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...
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.
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...
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.