LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-10-2008, 10:28 AM   #1
Curtor
Member
 
Registered: Feb 2008
Posts: 65

Rep: Reputation: 16
Find PID of /bin/sh command in C++


Linux, C++
I want to read a command as an argument to a program, and then run it using:
Code:
int returnValue = execlp( "/bin/sh", "/bin/sh", "-c", command, "; return $?", (char*)NULL );
The catch is, I want to know the PID of the program run using 'command'.

Using
Code:
ps ax | grep 'command'
may return more than one result, so it is not sufficient.

One proposed solution is to use
Code:
ps -o ppid,pid,command ax | grep 'command' | grep '^[ *]currentPID'
and then return the result in the second column. The problem with this is:
1) If the command is something very small and exits almost immediately
(Therefore not occurring in the ps results)
2) The command forks off additional children with similar command names
(Therefore returning more than one possibly correct result)
 
Old 12-10-2008, 10:52 AM   #2
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
For the first problem I would use "fork", "execve" and "wait" (man for more info). Something like:

Code:
pid = fork();

if(fork==0) {
  // child process
  execve(command);
}

// parrent process
r = wait(pid);
For the second problem maybe you could use the "ptrace" feature of linux kernel - to intercept the forks for example. (http://www.linuxjournal.com/article/6100)
 
Old 12-10-2008, 07:37 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
One problem is that command has a good chance of resulting in multiple processes, such as with redirection, or maybe even sub-shells. If the command can be grepped from a list of running processes, though, then you can probably just skip the call to the shell and exec the command itself. I don't really understand what the purpose of the shell is in your example, and why you wouldn't use system if you need shell interpretation.
ta0kira
 
Old 12-11-2008, 09:04 AM   #4
Curtor
Member
 
Registered: Feb 2008
Posts: 65

Original Poster
Rep: Reputation: 16
I could use a system call, but a system call just does what my program is already doing. System will exec using /bin/sh -c command and then wait for the call to return. (See http://linux.die.net/man/3/system)

It still presents the same problem for not knowing what the new process ID is.

As ta0kira said, I want to use /bin/sh (or something similar) so that the command may contain shell interpretation.
 
  


Reply



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
How to find the PID of a process using C programming? socialjazz Programming 3 10-06-2006 12:53 AM
Is '/usr/bin/find' reliable if '/bin/ls' has been replaced? DigaMe Linux - Security 2 11-12-2004 10:42 PM
how to find all files with pid Zac2003 Linux - Software 2 10-27-2004 09:44 PM
How to find the pid to stop a process.... vous Linux - Networking 6 08-14-2003 04:18 AM
linux command error message bash: /usr/bin/find: No such file or directory sundaram123 Linux - General 8 04-02-2002 07:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:46 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
Open Source Consulting | Domain Registration