Change language

Python | os.fork () method

|

All functions in the os module raise OSError for invalid or unreachable file names and paths or other arguments that are of the correct type but are not accepted by the operating system.

os.fork () in Python is used to create a child process. This method works by calling the base OS function fork () . This method returns 0 in the child process and the child process ID in the parent process.

Note: os.fork () only available on UNIX platforms.

Syntax: os.fork ()

Parameter: No parameter is required

Return Type: This method returns an integer value representing child’s process id in the parent process while 0 in the child process.

Code: using the os.fork () method to create a child process

# Python program to explain the os.fork () method

 
# import of the os module

import os

 < / p>

 
# Create a child process
# using the os.fork () method

pid = os.fork ()

 
# pid greater than 0 represents
# parent process

if pid"  0 :

print ( "I am parent process:" )

  print ( "Process ID:" , os.getpid ())

print ( "Child’s process ID : " , pid)

  
# pid equals 0 represnts
# created child process

else :

print ( "I am child process:" )

print ( "Process ID:" , os.getpid ())

print ( "Parent’s process ID:" , os.getppid ())

 

 
# If in time of any error
# using the os.fork () method
# OSError will be raised

Exit:

 I am Parent process Process ID: 10793 Child’s process ID: 10794 I am child process Process ID: 10794 Parent’s process ID: 10793 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method