7

I was reading programming python 4th edition by Mark Luze, Oreilly, by teaching myself. There's an example on how to fork a child process, which I do not quite understand:

os.execlp('python', 'python', 'child.py', #other args#)

In an interactive shell(like bash), I know I can type python child.py #args# to ask python interpreter to run child.py with args. Why are there TWO 'python' in the execlp() function? If I put only one python in the function, I would get an error complainting cannot find file or directory, which is the 1st args of child.py

comicosmos
  • 101
  • 1
  • 1
  • 6
  • 1
    This originates in the `execlp` libc funciton, which you can get more info about using the command `man execlp`. – Keith Nov 18 '12 at 12:20
  • 1
    Here's the Open Group spec for the [exec family](http://pubs.opengroup.org/onlinepubs/009604499/functions/exec.html) of POSIX functions. The general pattern of fork/exec in Unix hasn't changed much in 40 years. – Eryk Sun Nov 18 '12 at 22:02

2 Answers2

10

The first argument is the program to execute (found on the PATH). The rest are the sys.argv arguments to the program.

The first such argument is the program name used to invoke it, and the display value used in the OS process list. It is the value of sys.argv[0] in a python script.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Yeah. I got it. The 1st python is python file path, 2nd python is argv[0], and the rests are more args. – comicosmos Nov 19 '12 at 14:23
  • It's a pity this got downvoted, it looks as if [someone misunderstood this answer](http://stackoverflow.com/questions/14174366/when-using-os-execlp-why-python-need-python-as-argv0/). – Martijn Pieters Jan 05 '13 at 17:37
  • "The first argument is the program to execute" where "program" means the Python interpreter program in this case, not the user *.py Python program itself – Ben Slade Apr 27 '22 at 21:16
4

First of all, execlp is rarely used today. In most cases, you'd use the subprocess module, like this:

subprocess.call(['python', 'child.py'])

The first argument of execlp is the file you want to execute.

The latter arguments form the argument array to that program (sys.argv in Python). The first argument is then the name the program got invoked with. For example, Python sets the name to '-c' if the program is being run with the -c option. Similarly, grep behaves differently depending on the first argument, so that users can execute rgrep to imply grep -r.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • 1
    I wouldn't assume that the latest edition is outdated -- it was published seven years after the `subprocess` module was included in the standard library. It may be demonstrating how `fork()` and `exec()` work if you ever need to dig deeper. – Dietrich Epp Nov 18 '12 at 12:17
  • 3
    subprocess.call does not replace the current process with the process it calls, which is an essential element of using execlp. subprocess.call is not a replacement for execlp. – llimllib May 15 '18 at 17:43