Link to home
Create AccountLog in
Avatar of aditya_khaund
aditya_khaundFlag for India

asked on


Hi I  Have a scenario like where a  c executable will be called by shell/perl script . So I have a requirement to implement a function in that c executable which can give me the parent process name using its pid . I don't want to use ps command inside c code for some reasons . Is there any library which can be used to get the same results?

I got one function like getprocs() which I tried but it executes successfully but failed to print the process name stored inside structure struct procsinfo as pi_comm{MAXCOMMLEN+1];.

Any help on this will be highly appreciated.

Avatar of woolmilkporc
Flag of Germany image


with 64 bit AIX (check using "bootinfo -K" as root) you should use "getprocs64", using the structs "procentry64" and "fdsinfo64".

From "AIX Technical Reference":

int getprocs64 ( ProcessBuffer,  ProcessSize,  FileBuffer,  FileSize,  IndexPointer,  Count)

struct procentry64 *ProcessBuffer;
int ProcessSize;
struct fdsinfo64 *FileBuffer;
int FileSize;
pid_t *IndexPointer;
int Count;

Avatar of aditya_khaund


Hi, I tried using getprocs64 also . But the issue is not printing the parent script name , instead its printing some characters from another script in the same directory.

My scenario is that I am calling my c executable from a shell script .
So my understanding is that when I do getppid I will basically get the id of parent which I guess is the shell .I m passing the same id to the getprocs(32 | 64) bit function but the result is undesirable . I expect the name of shell as result.

Please correct me if I have any wrong notion about the idea I just mentioned and suggest me some effective solution.

I'll attach a tiny program I wrote for you and which works for me.
When started from a script "Parent Program" contains the name of the shell used for this script.
So if the shebang line of the script contains "#!/bin/ksh" "Parent Program" will show "ksh".
The same is true for bash, csh, whatever.


#include <stdio.h>
#include <sys/types.h>
#include <procinfo.h>
#include <errno.h>

int main(void)
struct procentry64 procbuffer;
struct fdsinfo64 filebuffer;
pid_t procindex, mypid, parent;
int procsize, filesize;
int count = 1;

char *format="\n\
         My PID:  %d\n\
     Parent PID:  %d\n\
Grandparent PID:  %d\n\
     Parent SID:  %d\n\
     Parent GRP:  %d\n\
 Parent Program:  %s\n";

mypid  = getpid ();
parent = getppid ();

 procindex = parent;

if ( getprocs64 (&procbuffer, sizeof(struct procentry64), &filebuffer, sizeof(struct fdsinfo64), &procindex, count) <= 0 )
 printf("\nPID is %d.\n", procindex);

printf( format,

return 0;

Open in new window

Thanks this solution makes some sense , But it will only give me the name of the shell (ksh,bash,csh) in case of shell or perl in case parent is a perl script. but is there no way that I can get the name of parent script . e,g whatever it is as we get  using "ps" ?
Yes, you're right.

Your script is not a program in a narrow sense. It just serves as input to the interpreter program, which is e.g. perl or ksh. The name of your script is thus just a parameter to the actual program, and such a parameter cannot be obtained via procinfo.

Ok , then how does ps prints the name of the program name also along with other information ?

if I run a cmd like
ps -ef | grep <PID> | grep -v grep | egrep "(\.sh | \.pm) "

i will get the name of caller I guess . Can I get any such using system libraries.
The work around solution I am using now is to run the command using popen and grabbin the output.
But I am curious to use only C libraries for this . Please guide me if you have any comment on the same
I think you will have to work with "getargs":

#include <procinfo.h>
#include <sys/types.h>
int getargs (processBuffer, bufferLen, argsBuffer, argsLen)
struct procentry64  *processBuffer;
int  bufferLen;
char  *argsBuffer;
int  argsLen;

Fill the PID in question into pi_pid of procentry64 and allocate an argsbuffer of the desired size using malloc before calling getargs. argsbuffer will then contain a NULL separated array of strings with an extra NULL at the end.

Avatar of woolmilkporc
Flag of Germany image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
THanks a lot for this . My Problem is solved.

       I need to do the same thing for RHEL  and CYGWIN also . Can you help me which libray/function should I use for this.