What happens when os.system() is executed in python thread?

Viewed 1472

I am new to python threading. I am trying to understand what happens when os.system() is called from a python thread. I understand that threads do share file descriptors, heap, code and global variables. I also read that os.system(cmd) creates a new subshell and there it executes the cmd provided.

My question is, when python threads call os.system(cmd) and the cmd executes "./test.exe input_file.dat", does the process for ./test.exe share anything (i.e. input files, address space, heap, etc.) with the python threads? In other words, does os.system(cmd) create a new process which has no relation with the caller process or thread?

Below, I provided the python code I wrote.

#!/usr/bin/python

import threading
import os

semaphore = threading.Semaphore(3)

def run_command(cmd):
    with semaphore:
        os.system(cmd)

for i in range(3):
    threading.Thread(target=run_command, args=("./test.exe input_file.dat", )).start()
2 Answers

What happens when os.system() is executed in python thread?

  • A thread is created. This thread -- like all threads -- shares its address space with the thread that created it.

  • That thread calls fork() to create a new child process almost exactly like itself. Only the thread that called fork() is present in the child process; other threads are not copied. The child process has a separate address space from the parent process, but has memory mapped at all the same addresses.

    (This child process is only present for a very brief time -- you won't see it in this state unless you take specific steps in a debugger to pause the process here. Using threads in conjunction with fork() is generally inadvisable, but in this case it's mostly okay.)

  • The new child process calls execve() to fully replace itself with a new process running test.exe. The address space of the child process is destroyed, and a new one created for the image of the new process.

  • Meanwhile, the thread that called fork() now calls waitpid() (or possibly wait()) to suspend its execution until the new process finishes. Once that happens, os.system() returns.

Related