Getpgrp() Method Of Os Module In Python

Method Name:

os.getpgrp()

Method Signature:

os.getpgrp()

Return Value:

Id of the current process group the process belongs to. 

Method Overview:

  • The method os.getpgrp() returns the id of the current process group under which the current process is grouped.
  • A process is a basic unit managed by the operating systems and is identified by a process id or pid. Process Id of a current process can be obtained by calling the os.getpid() method.
  • Operating systems group processes for several reasons including inter process communication and termination of abandoned processes by a terminal when it closes.
  • In Unix PGID groups the processes together.
  • When a child process is created the child process gets the group id of the parent process. This PGID can be later changed by calling os.setpgid() method.

 

Example:

import os

 

# Get the pid and pgid of the current process

pid  = os.getpid()

PGID = os.getpgrp()

print("The parent process %d belongs to process group: %d"%(pid, PGID))

 

# Create a child process

retVal = os.fork()

 

# Will get executed for child process

if retVal is 0:

    childPID = os.getpid()

    print("Child process %d belongs to process group: %d"%(childPID, PGID))

 

Output:

The parent process 2155 belongs to process group: 2155

Child process 2156 belongs to process group: 2155


Copyright 2020 © pythontic.com