Chapter 20: C Under Linux

Fork , Page number: 655

In [7]:
import os

print  "Before Forking" 
child = os.fork() #create a child process
print "After Forking\n"  

Creating a Child Process , Page number: 656

In [ ]:
import os

pid = os.fork()
if pid == 0:
    print  "In child process"  # code to play animated GIF file
else:
    print  "In parent process"  #code to copy file 
         

PID of Parent And Child Processes , Page number: 657

In [ ]:
import os
from multiprocessing import Process

if __name__ == '__main__':
    ppid=os.getpid()
    p = Process()
    p.start()
    cid = os.getpid()
  

if (cid):
    print ("Child : Hello I am the child process")
    print ("Child : Child’s PID: ", os.getpid( ) )
    print ("Child : Parent’s PID: ", os.getppid( ) )
else:
    print  ("Parent : Hello I am the parent process" )
    print  ("Parent : Parent’s PID: ", os.getpid( ) )
    print  ("Parent : Child’s PID: ", cid )

Execl , Page number: 659

In [*]:
import os


pid = os.fork()
if pid == 0:
    os.execl ( "/bin/ls","-al", "/etc", NULL ) 
    print "Child: After exec( )"
else:
    print "Parent process"

Waitpid , Page number: 662

In [*]:
import os

i = 0 
pid = os.fork( ) 
if ( pid == 0 ):
    while ( i < 4294967295 ):
        i=i+1
        print  "The child is now terminating" 
else:
    os.waitpid ( pid, status, 0 )
    if ( os.WIFEXITED ( status ) ):
        print  "Parent: Child terminated normally" 
    else:
        print  "Parent: Child terminated abnormally" 
In [ ]: