LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 06-23-2015, 02:10 PM   #1
kol_bro
LQ Newbie
 
Registered: Jun 2015
Posts: 5

Rep: Reputation: Disabled
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
 
Old 06-24-2015, 06:51 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
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?
 
Old 06-24-2015, 07:21 AM   #3
kol_bro
LQ Newbie
 
Registered: Jun 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-24-2015, 09:09 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,600

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by kol_bro View Post
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.
 
Old 06-25-2015, 08:16 AM   #5
kol_bro
LQ Newbie
 
Registered: Jun 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-25-2015, 01:15 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
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.
 
Old 06-25-2015, 03:40 PM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,600

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple Perl Sockets + Child Processes (fork) Reion Programming 3 10-13-2008 07:17 PM
Create N child processes - fork() embesil Programming 5 03-24-2008 09:03 PM
creating sequential child processes using fork() BrokenFighter Programming 1 03-06-2007 11:11 PM
why there're lots of child processes when fork? iclinux Programming 3 01-18-2005 08:09 AM


All times are GMT -5. The time now is 10:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration