Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine pid of terminated process

Tags:

c

signals

waitpid

I'm trying to figure out what the pid is of a process that sent the SIGCHLD signal, and I want to do this in a signal handler I created for SIGCHLD. How would I do this? I'm trying:

int pid = waitpid(-1, NULL, WNOHANG);

because I want to wait for any child process that is spawned.

like image 529
Hristo Avatar asked Apr 07 '10 20:04

Hristo


People also ask

How do you find the PID of a process?

Task Manager can be opened in a number of ways, but the simplest is to select Ctrl+Alt+Delete, and then select Task Manager. In Windows, first click More details to expand the information displayed. From the Processes tab, select Details to see the process ID listed in the PID column.

How do I find the PID of a process in Linux?

A process is nothing but running instance of a program and each process has a unique PID on a Unix-like system. The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.

Can a process have multiple PIDS?

Pids are one-per process. There will NEVER be more than 1 pid for a process - the internal data structures that handle the process in the OS only have a single PID field in them.

What is PID in Linux?

Overview. As Linux users, we're familiar with process identifiers (PID). PID is the operating system's unique identifier for active programs that are running. A simple command to view the running processes shows that the init process is the owner of PID 1.


1 Answers

If you use waitpid() more or less as shown, you will be told the PID of one of the child processes that has died — usually, that will be the only process that has died, but if you get a flurry of them, you might get one signal and many corpses to collect. So, use:

void sigchld_handler(int signum)
{
    pid_t pid;
    int   status;
    while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
    {
        unregister_child(pid, status);   // Or whatever you need to do with the PID
    }
}

You can replace &status with NULL if you don't care about the exit status of the child.

Different systems document the return value of waitpid() slightly differently. However, POSIX is one of the more careful ones and says:

If wait() or waitpid() returns because the status of a child process is available, these functions shall return a value equal to the process ID of the child process for which status is reported. If wait() or waitpid() returns due to the delivery of a signal to the calling process, -1 shall be returned and errno set to [EINTR]. If waitpid() was invoked with WNOHANG set in options, it has at least one child process specified by pid for which status is not available, and status is not available for any process specified by pid, 0 is returned. Otherwise, -1 shall be returned, and errno set to indicate the error.

like image 114
Jonathan Leffler Avatar answered Sep 21 '22 03:09

Jonathan Leffler