ZetCode

Python os.getsid Function

Last modified April 11, 2025

This comprehensive guide explores Python's os.getsid function, which retrieves the session ID of a process. We'll cover process sessions, related system calls, and practical examples of session management.

Basic Definitions

The os.getsid function returns the session ID of the process specified by its process ID (pid). A session is a collection of process groups.

Key parameters: pid (process ID to query, 0 means current process). Returns the session ID as an integer. Raises PermissionError if access is denied.

Getting Current Process Session ID

The simplest use of os.getsid retrieves the session ID of the current process by passing 0 as the pid parameter. This shows basic usage.

current_session.py
import os

# Get session ID of current process
session_id = os.getsid(0)
print(f"Current process session ID: {session_id}")

# Compare with os.getpid() and os.getpgid()
print(f"Process ID: {os.getpid()}")
print(f"Process group ID: {os.getpgid(0)}")

This example shows how to get the current session ID and compares it with related process identifiers. The session ID is typically different from both.

Note that session IDs are unique within a system and remain constant for a process's lifetime unless explicitly changed.

Getting Session ID of Child Process

This example demonstrates getting the session ID of a child process after forking. It shows how session IDs are inherited by default.

child_session.py
import os
import time

# Fork a child process
pid = os.fork()

if pid == 0:
    # Child process
    print(f"Child PID: {os.getpid()}")
    print(f"Child session ID: {os.getsid(0)}")
    time.sleep(2)
else:
    # Parent process
    print(f"Parent PID: {os.getpid()}")
    print(f"Parent session ID: {os.getsid(0)}")
    print(f"Child's session ID: {os.getsid(pid)}")
    os.wait()

The parent process retrieves both its own and the child's session ID. By default, the child inherits the parent's session ID unless setsid is called.

This demonstrates how process hierarchies maintain session membership unless explicitly changed with session management calls.

Creating New Session with setsid

This example shows how to create a new session using os.setsid and then verify the new session ID with os.getsid.

new_session.py
import os

# Fork and create new session
pid = os.fork()

if pid == 0:
    # Child process becomes session leader
    new_sid = os.setsid()
    print(f"New session ID: {new_sid}")
    print(f"Verified session ID: {os.getsid(0)}")
    print(f"Is process group leader: {os.getpid() == os.getpgid(0)}")
else:
    # Parent process
    print(f"Original session ID: {os.getsid(0)}")
    os.wait()

The child process creates a new session and becomes its leader. The new session ID matches the child's process ID, and it becomes the process group leader.

This is a common pattern for daemon processes that need to detach from their controlling terminal and parent session.

Checking Session ID of System Processes

This example demonstrates checking session IDs of system processes by reading the /proc filesystem (Linux-specific). It shows cross-process session checks.

system_processes.py
import os

def get_process_session(pid):
    try:
        return os.getsid(pid)
    except ProcessLookupError:
        return "Process does not exist"
    except PermissionError:
        return "Permission denied"

# Check session IDs of various system processes
pids = [1, os.getpid(), 1000, os.getppid()]

for pid in pids:
    sid = get_process_session(pid)
    print(f"PID {pid} has session ID: {sid}")

The function safely retrieves session IDs while handling potential errors. It checks well-known PIDs like init (PID 1) and the current process.

Note that accessing other processes' session IDs requires appropriate permissions on most systems.

Session Management in Daemon Processes

This example shows a complete daemonization process including session creation, demonstrating proper use of os.getsid for verification.

daemon_process.py
import os
import sys
import time

def daemonize():
    # Fork and exit parent
    pid = os.fork()
    if pid > 0:
        sys.exit(0)
    
    # Create new session
    os.setsid()
    
    # Verify new session
    if os.getsid(0) != os.getpid():
        sys.stderr.write("Failed to become session leader\n")
        sys.exit(1)
    
    # Fork again to ensure we're not session leader
    pid = os.fork()
    if pid > 0:
        sys.exit(0)
    
    # Change working directory
    os.chdir('/')
    
    # Demonstrate daemon operation
    while True:
        with open("/tmp/daemon.log", "a") as f:
            f.write(f"Daemon PID {os.getpid()} in session {os.getsid(0)}\n")
        time.sleep(5)

if __name__ == "__main__":
    daemonize()

This implements standard daemonization steps: forking, session creation, working directory change, and verification using os.getsid.

The double fork ensures the daemon isn't a session leader and won't acquire a controlling terminal accidentally.

Comparing Sessions Across Processes

This example compares session IDs across multiple processes to demonstrate session inheritance and isolation in different scenarios.

session_comparison.py
import os
import subprocess

def run_in_shell(command):
    proc = subprocess.Popen(command, shell=True)
    return proc.pid

# Current process session
print(f"Main process session: {os.getsid(0)}")

# Child in same session
same_session_pid = os.fork()
if same_session_pid == 0:
    print(f"Child (same session): {os.getsid(0)}")
    os._exit(0)
os.wait()

# Process in new session
new_session_pid = run_in_shell("python3 -c 'import os; print(f\"New shell session: {os.getsid(0)}\")'")

# Verify sessions
print(f"Shell process session: {os.getsid(new_session_pid)}")

The example shows three scenarios: current process, forked child (same session), and shell command (new session). Each displays its session ID.

This demonstrates how different process creation methods affect session inheritance and how to verify session membership across processes.

Security Considerations

Best Practices

Source References

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.