Computing.Net > Forums > Programming > How to display Shell PID in C?

How to display Shell PID in C?

Reply to Message Icon

Original Message
Name: JustinUD
Date: February 29, 2004 at 12:46:03 Pacific
Subject: How to display Shell PID in C?
OS: Unix
CPU/Ram: 1GB
Comment:

I am writing a shell and I need to write a built-in command, 'pid' that will print the pid of the shell. I cannot for the life of me figure out how to only display the shell's pid. Can someone please help me out here? I am writing this in C and running it on a unix system.

Thanks!


Report Offensive Message For Removal


Response Number 1
Name: wharfie
Date: February 29, 2004 at 19:58:44 Pacific
Reply: (edit)

if your C program is run from the shell then the get parent PID function, getppid(), will return the PID of the shell, eg;

<pre>
#include <sys/types.h>
#include <unistd.h>

pid_t getppid(void);

int main(int argc, char* argv[])
{
printf("parent pid: %i\n",getppid());
}
</pre>

This is the same as;

echo $PPID

when executed in a script.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: How to display Shell PID in C?

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge