LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   get process ID (https://www.linuxquestions.org/questions/programming-9/get-process-id-546818/)

wjevans_7d1@yahoo.co 04-19-2007 12:51 PM

Back to the original, unhijacked (grin) question:

There's a way to do this that's possibly simpler than your C code.

Open the /proc directory, as you're doing. Concentrate on the process names by concentrating on numbers, as you're doing.

For process 2345, for example, look at 2345/exe, which is a symbolic link. It links to whatever is being executed, like /bin/bash. That gives you the name of the program you're running. It's arguably easier to read a symbolic link than to parse text.

It's also fun to try the proof of concept on the command line, since bash will use its own process number when you put $$ on the command line:

Code:

ls -l /proc/$$/exe
Um, since you're already concentrating on numbered processes, you can leave out the explicit check for "..".

Hope this helps.

culin 04-20-2007 01:20 PM

Hi wjevans_7d1@yahoo.co,
i am doing what u told in the code posted before... but there is a problem with this code.. i think.. may be its the drawback of the code...
say i will run and executable named ./testing from a program inside which i will be doing execl() or execvp(./testing , "the name by which executable has to be spawned")
say execvp(./testing, test);
now if do a ps -aux then it will be showing the process named "test" with so and so pid... now if i want to have the pid of the process running "test" by my code what i had posted.. it is returning -1... also killall -9 test will tell that no such process.. even though ps -aux shows that it is running with some pid.. but when i do "/sbin/pidof test" this gives the pid with what it is running..
so why killall -9 test wont kill that process ????????
:)

wjevans_7d1@yahoo.co 04-21-2007 05:41 PM

culin, I'm having difficult understanding what you're saying. Could you translate that into a small C program? (Maybe you already have that as part of a larger program.) Then post here the entire small program, what you typed at the command line to run it, and the exact output it gave.

I think that will take you less time than it will for me to figure out what you said. Sorry. (grin)


All times are GMT -5. The time now is 07:41 AM.