vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Hi all How to get the process id inside the c code. My requirement is like ... I may execute a system call inside a c PROGRRAM, and in the next step I wud like to know what is the process id of the previously executed program ? {.. .... system("/usr/bin/some_prog"); // Next I would like to know the pid of some_prog } How to get it ? Any lib call or system call for this ? regards, vasanth. |
| |||
| Vasantharaj <vasantharajg@nestec.net> wrote: > Hi all > How to get the process id inside the c code. My requirement is like ... I > may execute a system call inside a c PROGRRAM, and in the next step I wud > like to know what is the process id of the previously executed program ? > {.. > ... > system("/usr/bin/some_prog"); > // Next I would like to know the pid of some_prog > } > How to get it ? Any lib call or system call for this ? > regards, > vasanth. The way you've written the above? I don't think you can. According to the system(3S) man page -- system() does the equivalent of a fork()/exec()/wait() for the target program. So by the time flow of control returns to your code, the child process is already gone. The pid of that child may well have been re-used for something else. What I would do here is to replace system() with a fork(), which will return the pid of the child process. The child process can then exec() "/usr/bin/sh" with the argument "/usr/bin/some_prog" [or just exec some_prog directly if this is a binary and not a script] and the parent can wait() on it. Just like system() -- but with the child pid visible before it exits. Don -- kernel, n: A part of an operating system that preserves the medieval traditions of sorcery and black art. |
| |||
| Hi Thank you for the reply. But, if the "some_prog" is a demon, the system() will return and continue isn't it ? So, in that case is there any method to get the pid of "some_prog" other than using IPC? thanks and regards, vasanth. dmorris@cup.hp.com wrote in message news:<4010120a@usenet01.boi.hp.com>... > Vasantharaj <vasantharajg@nestec.net> wrote: > > Hi all > > > How to get the process id inside the c code. My requirement is like ... I > > may execute a system call inside a c PROGRRAM, and in the next step I wud > > like to know what is the process id of the previously executed program ? > > > {.. > > ... > > system("/usr/bin/some_prog"); > > // Next I would like to know the pid of some_prog > > > > } > > > How to get it ? Any lib call or system call for this ? > > > regards, > > vasanth. > > The way you've written the above? I don't think you can. > According to the system(3S) man page -- system() does > the equivalent of a fork()/exec()/wait() for the target > program. So by the time flow of control returns to your > code, the child process is already gone. The pid of that > child may well have been re-used for something else. > > What I would do here is to replace system() with a fork(), > which will return the pid of the child process. The child > process can then exec() "/usr/bin/sh" with the argument > "/usr/bin/some_prog" [or just exec some_prog directly > if this is a binary and not a script] and the parent > can wait() on it. Just like system() -- but with the > child pid visible before it exits. > > Don |
| |||
| > My requirement is like ... I may execute a system call inside a c PROGRRAM, > and in the next step I wud like to know what is the process id of the > previously executed program ? are you confusing 'system call' with 'a call to system()' ? if so, the system call you are looking for is fork. a call to the libc 'system()' API is not a system call. |
| ||||
| > How to get the process id inside the c code. My requirement is like ... I > may execute a system call inside a c PROGRRAM, and in the next step I wud > like to know what is the process id of the previously executed program ? If your running up a daemon then there is no way of tracking it (without any IPC) as the daemon will fork a new process and then kill off the parent (your child process). Regards Paul |
| Thread Tools | |
| Display Modes | |
|
|