LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack 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: ubuntu
Posts: 2,524

Rep: Reputation: 93
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 04:33 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration