Os.fork in Python

Overview:

  • The os.fork() function in Python provides the implementation of the system call fork().
  • The os.fork() when called from a program it creates a new process.
  • Thus executing os.fork() creates two processes: A parent process and a child process.
  • The newly created child process is the exact replica of the parent process.
  • The child process will have copies of the descriptors if any used by the parent process. However, the copies of the descriptors will refer to the same underlying objects. Hence care must be taken while calling functions like os.lseek() or seek() on such underlying objects from such a parent/child process setup.
  • If successful, the return value of the os.fork() call in the parent program will be the process id of the child process. In case of any failure the return value will be -1.   For the successful case in the child process the return value will be zero.
  • When os.fork() is called, it is called once but returns twice - once in parent process and once in child process. The return value in the child process is zero and the return value in the parent process is the process id of the child.
  • To get the process id within the child, use os.getpid()  and to get the process id of the parent process within the child call os.getppid().

os.fork() method call in Python

Example 1:

import os

 

ppid = os.getpid()

print("Pid of parent process %d:"%(ppid))

 

# Create a child process

retVal = os.fork()

 

# Get the process id

pid = os.getpid()

 

if retVal is 0:

    print("Return value of fork in child process is %d:"%(retVal))

else:

    print("Return value of fork(i.e., Child pid) in parent process is %d:"%(retVal))

 

for i in range(0, 10):

    print("Printing the value %d from the Process: %d"%(i, pid))

 

 

Output:

Pid of parent process 1338:

Return value of fork(i.e., Child pid) in parent process is 1339:

Printing the value 0 from the Process: 1338

Printing the value 1 from the Process: 1338

Printing the value 2 from the Process: 1338

Printing the value 3 from the Process: 1338

Printing the value 4 from the Process: 1338

Printing the value 5 from the Process: 1338

Printing the value 6 from the Process: 1338

Printing the value 7 from the Process: 1338

Printing the value 8 from the Process: 1338

Printing the value 9 from the Process: 1338

Return value of fork in child process is 0:

Printing the value 0 from the Process: 1339

Printing the value 1 from the Process: 1339

Printing the value 2 from the Process: 1339

Printing the value 3 from the Process: 1339

Printing the value 4 from the Process: 1339

Printing the value 5 from the Process: 1339

Printing the value 6 from the Process: 1339

Printing the value 7 from the Process: 1339

Printing the value 8 from the Process: 1339

Printing the value 9 from the Process: 1339

 

Example 2:

import os

 

def ParentProcess():

    for i in range(1,10):

        print("Parent Proc: %d"%(i))

    print("Parent process exiting")

 

def ChildProcess():

    for i in range(1,10):

        print("Child Proc: %d"%(i))

    print("Child process exiting")

 

# Fork and create a child process

retVal = os.fork()

print("Return value is %d"%(retVal))

 

# Separate logic for parent and child

if retVal is 0:

    ChildProcess()

else:

    ParentProcess()

 

 

Output:

Return value is 1351

Parent Proc: 1

Parent Proc: 2

Parent Proc: 3

Parent Proc: 4

Parent Proc: 5

Parent Proc: 6

Parent Proc: 7

Parent Proc: 8

Parent Proc: 9

Parent process exiting

Return value is 0

Child Proc: 1

Child Proc: 2

Child Proc: 3

Child Proc: 4

Child Proc: 5

Child Proc: 6

Child Proc: 7

Child Proc: 8

Child Proc: 9

Child process exiting

 


Copyright 2024 © pythontic.com