Obtaining A Process ID (PID)

Is there any way I can get UNIX to return the Process ID of a process when I actually issue the command to invoke the process?

For example, if I was to launch an emacs process in batch mode, by issuing the UNIX command :

% emacs file.txt&

then UNIX would return the Process ID (PID). "2343", for example.
Is this possible?

The reason I ask this question is because I am invoking programs from a remote machine and need to periodically probe for which processes are running to see whether the processes are still running or if they have terminated.

I suppose I could just issue a 'ps' command, pipe the output to a file and then grep the file for the commands issued, but the processes I am looking for depend on the directory they were invoked from and I don't think the 'ps' utility records the directory which a program/process was executed from - or does it?

Many thanks!

There's no need to double post your question like that. I don't think any of us ignore any of the forums on this small site.

Your question is a shell issue. The pid of a subprocess in ksh is available in the ! variable.

So you want to do:
emacs file.txt &
pid=$!

(Not that I understand why you'd background an editor, but we'll ignore that.)

If your shell doesn't have a ! variable, then please tell us which shell you are using.

Sorry for the double post.

How does the "pid=$!" command reveal the value of the ! variable for the process invoked in the "emacs file.txt&" command?

Is the "pid=$!" command issued separately to the emacs command, or is it on the same command line?

Essentially, all I want to do is 'echo' the value of the PID for each program execution command.

Could you please show me an example?

Many thanks.

To just echo the pid, do this:
emacs file.txt &
echo $!

In short $! returns you the process id of the last process you ran in the background.

:slight_smile:

Thankyou very much for your help! My only outstanding query involves the allocation of a PID to UNIX processes:

Is it simply a case of UNIX maintaining a pool of 'free' PIDs, and then allocating a free PID when a process is run by a user. Conversely, the PID of a terminated process is returned to the 'free' pool ready for another process to receive?

I presume that at any point in time on the UNIX machine, no 2 processes will have the same PID?

Thanks!

The algorithm for allocating process IDs differs by type of OS. Some OS types allocate in sequential order up to 32,000 and then start over at the beginning using free slots. Others, namely AIX, will reuse PIDs as they are freed. Regardless of how the PID is allocated, the PID is actually an index into the Process Table in the Kernel which is an array of structures of type proc (see /usr/include/sys/proc.h). If the process is runnable, active, or in a zombie state, the slot in the process table is marked as full. When the process dies and is successfully cleaned up, the slot is available for allocation by the dispatcher when a new process is started. So, to summarize, PIDs are unique in all types of UNIX.

1 Like

Thanks wizard. Welcome to the forums. Thanks for your excellent PID summary.