0% found this document useful (0 votes)
64 views3 pages

Understanding PID and PPID in Linux

This document discusses Linux processes. It explains that each process has a unique process ID (PID) and parent process ID (PPID). The getpid() and getppid() system calls return a process's PID and PPID. Example code is given to print these IDs. The ps and pstree commands can view processes and their hierarchy. Fork is used to create child processes that are copies of the parent process. Example fork code is provided to demonstrate parent and child processes. An assignment asks to run the example programs multiple times and write a program that creates four processes using fork.

Uploaded by

San Dip
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views3 pages

Understanding PID and PPID in Linux

This document discusses Linux processes. It explains that each process has a unique process ID (PID) and parent process ID (PPID). The getpid() and getppid() system calls return a process's PID and PPID. Example code is given to print these IDs. The ps and pstree commands can view processes and their hierarchy. Fork is used to create child processes that are copies of the parent process. Example fork code is provided to demonstrate parent and child processes. An assignment asks to run the example programs multiple times and write a program that creates four processes using fork.

Uploaded by

San Dip
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab 3

Processes

1. Looking at Processes
1.1 Process IDs
Each process in a Linux system is identified by its unique process ID, referred to as pid.
Process IDs are 1- 32768 numbers that are assigned sequentially by Linux as new process
are created.
Every process also has a parent process (except init). Thus, we can think the processes in
the Linux are arranged in a tree, with the init process at its root. The parent process ID, or
ppid, is simply the process ID of the process's parent.
Most of the process manipulation functions are declared in the header file <unistd.h>. A
program can obtain the process ID of the process it's running with the getpid() system
call, and it can obtain the process ID of its parent process with the getppid() system call.

Ex. 3.1: Printing the Process ID


print-pid.c
#include<stdio.h>
#include<unistd.h>
int main()
{
int pid, ppid;
pid = getpid()
ppid = getppid();
printf(“The Process ID is %d \n”, pid);
printf(“The Parent Process ID is %d \n”, ppid);
return 0;
}

1.2 Viewing Processes


ps – display the process running on that system.
pstree – display the all process in process hierarchy.
ps [options] – ps has different options.
e.g.: ps -e -o pid,user,start_time,command
ps -o pid,ppid,user.

1
2 Creating a processes

2.1 Using fork


Linux provides one function, fork, that makes the child process that is an exact copy of its
parent process. Linux also provides another set of functions, the exec family, that replaces
the current process image with a new process image.

Using fork
When programs calls fork, the parent process continues executing the program from the
point that fork was called. The child process, too, executes the same process from the
same place. The fork return some value for its parent and the child process always has 0
pid.

Ex. 3.3: Using fork


#include<stdio.h>
int main()
{
printf(“ This is to demonstrate the fork\n”);
fork();
printf(“Hi!, Everybody\n”)
}

2
Ex. 3.4: Using fork
#include < stdio.h>
#include <unistd.h>
int main()
{
int pid;
printf(“The main program process ID is %d \n”, (int) getpid());
pid = fork();
if (pid != 0){
printf(“This is the parent process, with id % d \n”, (int) getpid());
printf(“The child process ID is %d \n”, pid);
}
else
printf(“ This is the child process, with id %d\n”, (int) getpid());
return 0;
}

Assignment #L3
1. Execute each of the programs (given in Ex. 3.1 – 3.4) at lest three times, and
analyze their outputs.

4. Write the program that creates the the four process using fork() system call.

Common questions

Powered by AI

In the Linux operating system, each process is identified by a unique process ID (pid), which ranges from 1 to 32768. These IDs are assigned sequentially as new processes are created. Each process also has a parent, which can be traced using the parent process ID (ppid). Processes are organized in a hierarchical tree structure with the 'init' process at the root. This structure is essential for managing the parent-child relationships and process manipulation. Functions for obtaining process IDs, such as getpid() and getppid(), are declared in <unistd.h> .

When the fork() system call is executed, two main outcomes occur: in the parent process, fork() returns the child's process ID; in the child process, it returns zero. This distinction allows each process to perform different tasks post-fork(). The parent process can manage or wait for the child process, while the child may execute a separate path or logic. This behavior supports concurrent execution and process separation in Linux .

Options in the 'ps' command enhance process management and analysis by allowing customized views of running processes. For instance, using '-e' provides a list of all processes, and '-o' can specify the output format, including details like PID, user, start time, and command. This flexibility helps in monitoring system performance, diagnosing issues, and ensuring efficient process handling by filtering processes based on specific criteria .

The 'ps' command displays the processes running on the system based on the provided options, allowing users to see details like process ID, user, start time, and command. 'pstree', on the other hand, displays processes in a process hierarchy format, showing relationships between processes in a tree structure. An administrator might use 'ps' for a detailed, list-like overview of process attributes and 'pstree' for understanding the process hierarchy and parent-child relationships more visually .

A Linux program can obtain its process ID using the getpid() system call and its parent's process ID using the getppid() system call. An example code snippet is: ```c #include<stdio.h> #include<unistd.h> int main() { int pid, ppid; pid = getpid(); ppid = getppid(); printf("The Process ID is %d \n", pid); printf("The Parent Process ID is %d \n", ppid); return 0; } ``` This program will print its process ID and the parent process ID .

Understanding the hierarchical tree structure of processes in Linux is crucial for developers because it provides insight into the parent-child relationships and dependencies among processes. This knowledge allows for effective resource management, debugging, and system optimization. Developers can troubleshoot process propagation issues, prevent orphan processes, and manage signals and process termination accurately, which enhances the system's reliability and performance .

Creating multiple processes using fork() without proper management can lead to resource exhaustion, system instability, and degraded performance. Each fork creates a new process with its own copy of the parent's resources, which multiplies resource consumption exponentially. If not managed, processes can overwhelm CPU and memory resources, causing system slowdowns or crashes. It is essential to implement mechanisms to control the spawning of processes, limit resource usage, and ensure proper cleanup after processes terminate to maintain system stability and performance .

Analyzing program outputs when executing fork() multiple times is crucial to understanding process behavior and resource utilization. Each call to fork() doubles the number of processes, leading to exponentially more parallel instances, each executing from the same point. Proper analysis ensures that developers can trace execution flow, detect potential errors, and manage resources effectively. Misinterpretation may lead to incorrect conclusions about process integrity and inefficiencies. For Linux systems, such analysis also verifies the correct propagation of parent-child relationships and proper resource allocation, preventing issues like process thrashing .

The getppid() function in Linux is used to obtain the process ID of the parent of the calling process. This function helps in identifying the parent process from which the current process was created, aiding in tracking the hierarchy and relationships between processes. It simply returns the parent process ID as an integer value, which can then be used for process management or logging purposes .

When the fork() system call is used, it creates a child process that is an exact copy of the parent process. After the fork() call, both processes continue executing from the point of the call. The fork() returns a value to both processes: 0 to the child process and the child's process ID to the parent. This allows the two processes to differentiate their execution and take subsequent actions accordingly. This technique is fundamental for parallel processing in Linux environments .

You might also like