LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-12-2005, 05:11 AM   #1
nice_indian
Member
 
Registered: Oct 2004
Location: India
Distribution: redhat 9
Posts: 32

Rep: Reputation: 15
Angry function for getting ppid of "any" process


Hi,
i want to get process id of the parent of some process whose pid is known.
whats the c function for this. getppid() gives parent pid of current process only. Is there any way to get ancestor id. Please help!!! Thanx in advance.
I can get ppid of any process using "ps -el | grep pid" but i want c function.

Last edited by nice_indian; 01-12-2005 at 05:52 AM.
 
Old 01-12-2005, 07:00 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
AFAIK there's no standard function call available to do this. You need to use the /proc filesystem to read it. Here's a quick program that uses its own function to get its parent pid from /proc.
Code:
#include <sys/types.h>
#include <unistd.h>

#define MAXPIDLEN 10
#define MAXPROCPATHLEN (MAXPIDLEN + 11)


pid_t parentpidof(pid_t pid)
{
    pid_t ppid;
    char procpath[MAXPROCPATHLEN];
    FILE *procstat;

    snprintf(procpath, MAXPROCPATHLEN, "/proc/%u/stat", pid);
    procstat = fopen(procpath, "r");
    if (procstat < 0) {
        perror("fopen(procstat)");
        return 0;
    }
    fscanf(procstat, "%*d %*s %*c %u", &ppid);
    fclose(procstat);
    return ppid;
}


int main()
{
    pid_t mypid;
    pid_t myparentpid;

    mypid = getpid();
    myparentpid = parentpidof(mypid);
    printf("%u\n", myparentpid);
    return 0;
}
 
Old 01-12-2005, 09:54 AM   #3
nice_indian
Member
 
Registered: Oct 2004
Location: India
Distribution: redhat 9
Posts: 32

Original Poster
Rep: Reputation: 15
Thanx a lot sir, This is exactly what i wanted.
 
  


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
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 10:47 AM
When "function pointer" meets "template"... I can't get rid of this compiling er cyu021 Linux - Software 3 12-17-2004 07:52 PM
using bash to find the pid of a process called "name" poiuytrewq Linux - Newbie 3 10-25-2004 09:01 PM
"Function not implemented" error in call to "sem_open()" Krishnendu8 Linux - Newbie 1 06-07-2003 02:52 AM
"Function not imlemented" error in call to "sem_open()" Krishnendu8 Linux - Networking 0 06-07-2003 02:19 AM

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

All times are GMT -5. The time now is 01:34 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