- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Creating child process using fork() in Python
Our task is to create a child process and display process id of both parent and child process using fork() function in Python.
When we use fork(), it creates a copy of itself, it is a very important aspect of LINUX, UNIX. fork() is mainly applicable for multithreading environment that means the execution of the thread is duplicated created a child thread from a parent thread. When there is an error, the method will return a negative value and for the child process, it returns 0, Otherwise, it returns positive value that means we are in the parent process.
The fork() module can be used from the os module or from Pseudo terminal module called pty. So we should import os or pty for it.
The fork() is used to create a process, it has no argument and its return the process ID. The main reason for using fork() to create a new process which becomes the child process of the caller. When a new child process is created, both processes will execute the next instruction.
The return value of fork() we can understand which process we are when return 0 that means we are in the child process and if return positive value that means we are in the parent process and return negative value means that some error occurred.
import os def parentchild(): n = os.fork() if n > 0: print("Parent process : ", os.getpid()) else: print("Child proces : ", os.getpid()) # Driver code parentchild()
Parent process : 8023 Child process : 8024 $
The Pseudo-terminal utility module pty is defined to handle pseudo-terminal concepts. Using this we can start another process, and also can read or write from controlling terminal using programs.
This module is highly platform oriented. We should use UNIX systems to perform these operations.
import pty, os def process_parent_child(): (process_id, fd) = pty.fork() print("The Process ID for the Current process is: " + str(os.getpid())) print("The Process ID for the Child process is: " + str(process_id)) process_parent_child()
The Process ID for the Current process is: 12508 The Process ID for the Child process is: 12509
- Related Articles
- Creating multiple process using fork() in C
- Calculation in parent and child process using fork() in C++
- Run Python script from Node.js using child process spawn() method?
- Python program to communicate between parent and child process using the pipe.
- Process vs Parent Process vs Child Process
- Prevent fork bomb by limiting user process in linux
- fork() in C
- Creating Classes in Python
- Creating Instance Objects in Python
- Creating Database Table in Python
- The fork() Function in Perl
- fork() to execute processes from bottom to up using wait() in C++
- Creating and using packages in Java