Explanation
1. Purpose
This program demonstrates core operating system process management concepts by showing how a program retrieves its own process IDs, creates a separate child process, and waits for it to complete.
2. Key Concepts
- System Calls: Requests made by a program to the operating system kernel (e.g., getting process IDs, creating processes).
- PID & PPID: Process ID (unique identifier for a running process) and Parent Process ID (the ID of the process that launched it).
- Process Creation & Synchronization: Spawning a new independent process (
start()) and pausing the parent until the child finishes (join()).
3. Code Walkthrough
- Lines 3–5 (
os.getpid(),os.getppid()): Calls OS-level functions to retrieve and display the unique ID of the current process and its parent process (e.g., the terminal or IDE). - Lines 6–7 (
child_process()): Defines the function that will execute inside the newly created child process. - Line 9 (
p = multiprocessing.Process(...)): Prepares a new child process withchild_processas its target task. - Line 10 (
p.start()): Issues an OS request (likefork()orspawn) to launch and run the child process concurrently. - Line 11 (
p.join()): Pauses execution of the parent process until the child finishes (similar to thewait()system call). - Lines 12–15: Runs after the child completes; the parent prints its own PID alongside the finished child's PID.
4. Sample Output
(Note: Actual PID numbers will vary every time you run the script.)
----- SYSTEM CALL DEMONSTRATION -----
Process ID: 12345
Parent Process ID: 9876
Child Process is executing with PID 12346
Parent Process
Parent PID: 12345
Child PID: 12346
Child process has completed.
System calls demonstration completed.