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?
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?
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.