Mike
Mike

Reputation: 45

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

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()

Upvotes: 4

Views: 2318

Answers (2)

user149341
user149341

Reputation:

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.

Upvotes: 2

Mike
Mike

Reputation: 45

I found the answer. When we execute os.system(cmd) from a Python thread T1, the new subshell is created as a subprocess of T1. Therefore, test.exe shares T1's address space (which is basically the address space of the T1's parent, Python code). Using pmap on Linux, my answer can be verified.

Here I provide the pmap output for the parent process

Here I provide the pmap output for the subprocess (test.exe)

Upvotes: 0

Related Questions