LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   get process ID (https://www.linuxquestions.org/questions/programming-9/get-process-id-546818/)

culin 04-17-2007 01:50 PM

get process ID
 
Hi all,
can anyone tell me how do i get(using coding) a pid of the process that is running when i know only the name of the process.... i.e. can some gimme a ready code to find the pid of the process by its name... ?
thanks.. :)

osor 04-17-2007 01:57 PM

have you considered pgrep?

Matir 04-17-2007 01:59 PM

Look at the source code for 'pidof'. It takes a name and returns a PID.

MOS JEFF-INITELY 04-17-2007 02:01 PM

ps ux | awk '/name/ && !/awk/ {print $2}'

where name is the name of the process.

you can change the ps params this is set to get the processes you own that are running

EDIT: not sure what you mean by coding? shell scripting? C code? .. etc?

culin 04-17-2007 02:25 PM

wow.. thanks a lot for all replies..:)
i don know much about pgrep.. but i need a C code..... its fine if it directly works on the command line.. with some pipe of grepping.. so that i can use that in the C code inside a system() function....EG: system("ps ux | awk '/name/ && !/awk/ {print $2}'");....
what i need is the PID of the process when the process name is known....
thanks again....

osor 04-17-2007 08:14 PM

Quote:

Originally Posted by culin
.. but i need a C code.....

The utilities pidof and pgrep both have open source C implementations. The sysvinit package contains the pidof utility (look for the file “killall5.c”) and the procps package has pgrep (look here).

culin 04-18-2007 12:55 AM

thanks osor :)the source is too good.. but also too complex and big...
how can i get a simple piece of code.. to do this job... i.e. to return the pid when i give the input as process name ??? or is this possible to get the output in the console to have inside the C program....
below i have a code to do this job !!! but it is hanging in the while loop.... dunno why ..can u tellme what is wrong in the program....
Code:

/****************************************************************************
 *
 * This function gets the process id of a process. It opens the /proc
 * directory and then checks the entries in the directory. If the name of the
 * process exists, then the pid is obtained.
 *
 ****************************************************************************/

/* This part of code is ported from busybox library */
static pid_t get_pid_by_name(char *process)
{
    DIR *dir;
    struct dirent *next;
    long* pidList = NULL;
    int i = 0;
    char *pidName;
    pid_t retval;

    pidName = process;

    dir = opendir("/proc");
    if (!dir)
        printf("Cannot open /proc");

    while ((next = readdir(dir)) != NULL)
    {
        printf(" Entering the while loop ");
        sleep(1);
        FILE *status;
        char filename[READ_BUF_SIZE];
        char buffer[READ_BUF_SIZE];
        char name[READ_BUF_SIZE];

        /* Must skip ".." since that is outside /proc */
        if (strcmp(next->d_name, "..") == 0)
            continue;

        /* If it isn't a number, we don't want it */
        if (!isdigit(*next->d_name))
            continue;
        printf(" searching");
        sprintf(filename, "/proc/%s/status", next->d_name);
        if (! (status = fopen(filename, "r")) )
        {
            continue;
        }
        if (fgets(buffer, READ_BUF_SIZE-1, status) == NULL)
        {
            fclose(status);
            continue;
        }
        fclose(status);

        /* Buffer should contain a string like "Name:  binary_name" */
        sscanf(buffer, "%*s %s", name);
        if (strcmp(name, pidName) == 0)
        {
            pidList=(long *)xrealloc( pidList, sizeof(long) * (i+2));
            pidList[i++]=strtol(next->d_name, NULL, 0);
        }
    }
    if (pidList)
        pidList[i]=0;
    else
    {
        pidList=(long *)xrealloc( pidList, sizeof(long));
        pidList[0]=-1;
    }

    retval = (pid_t)pidList[0];
    free(pidList);
    return retval;
}

/****************************************************************************
 *
 * This function allocates memory.It changes  the  size  of the memory block
 * pointed to by ptr to size bytes.
 *
 ****************************************************************************/

static void *xrealloc(void *ptr, size_t size)
{
    ptr = realloc(ptr, size);
    if (ptr == NULL && size != 0)
        printf("Memory_exhausted");

    return ptr;
}

it is printing only the first statement... i.e. Entering the while loop... but not the 2nd printf searching......what went wrong ???

jlliagre 04-18-2007 02:22 AM

Quote:

Originally Posted by culin
but i need a C code.....

How would people guess it if you don't tell it in the first place ?

As a general advice, when posting in this forum a question where the programming language matters, do not hesitate to make it clear in the posting text, or better, show it in the thread title.

eg: "Get process ID [C, C++]"

That would benefit to everyone, you, the people providing answers and the people browsing/searching the forum.

slzckboy 04-18-2007 03:07 AM

If its not getting to "searching"

then your continue statements must be being executing.

Try focusing your attention there to see if that is the cause.

use more printfs etc or better yet use gdb to step through the code and see where the problem is.

culin 04-18-2007 03:45 AM

thanks for all replies and suggestions...:).i got this code working fine.... had made a mistake while defining the xrealloc() funciton....and typecasting that while calling...:)

kshkid 04-18-2007 04:05 AM

pidof is the symlink to /usr/bin/killall


Code:

It sends a signal to all processes except the processes in its own session, so it won’t kill the shell that is running the script it was called from.
But when killall is executed from any of the terminal, all the terminal sessions including the session from which it is executed is also killed.

Why is that ?

osor 04-18-2007 09:33 AM

Quote:

Originally Posted by kshkid
But when killall is executed from any of the terminal, all the terminal sessions including the session from which it is executed is also killed.

Perhaps your killall is not from sysvinit but from something else (psmisc perhaps?). On my system, “/bin/pidof” is a symlink to “../sbin/killall5” (the 5 is for SysV — i.e., Sys“five”).

kshkid 04-18-2007 10:50 PM

/sbin/pidof -> killall5

where killall5 is as

/sbin/killall5

osor 04-19-2007 09:23 AM

Quote:

Originally Posted by kshkid
/sbin/pidof -> killall5

where killall5 is as

/sbin/killall5

That’s what I meant — you say that “when killall is executed from any of the terminal, all the terminal sessions including the session from which it is executed is also killed.” But what happens when you execute “killall5” from the terminal?

P.S. By terminal, I assume you mean a tty terminal, not an xterm.

kshkid 04-19-2007 12:23 PM

Executing that all the sessions including the session from which i do execute the killall5 is getting terminated.

I could not relate the behaviour the action being done when i execute the command!

Thank you very much ! :)


All times are GMT -5. The time now is 09:54 PM.