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

Process Creation in C Using Fork()

This C program uses fork() to create child processes and print their process IDs. The parent process creates one child process, prints their IDs, then creates another child and prints the IDs. If fork() fails, it returns -1. Otherwise, the child process ID is 0 and the parent's is the PID of the child. The program demonstrates how fork() creates child processes and they can access their own and parent's process IDs.

Uploaded by

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

Process Creation in C Using Fork()

This C program uses fork() to create child processes and print their process IDs. The parent process creates one child process, prints their IDs, then creates another child and prints the IDs. If fork() fails, it returns -1. Otherwise, the child process ID is 0 and the parent's is the PID of the child. The program demonstrates how fork() creates child processes and they can access their own and parent's process IDs.

Uploaded by

Vishal Birajdar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
  • Practical 10: Fork System Call
  • Conclusion

Practical : 10

AIM:
To write a C program to perform process creation using fork() system call .
OPERATING SYSTEM (OS) LAB IN LINUX ENVIRONMENT
ALGORITHM:·
Start program.
· Assign fork() system call to pid.
· if pid is equal to -1, child process not created.
· if pid is equal to 0, child process will be created.
· Print the id of parent process and child process.
· Create another one child process in same loop.
· Print id of parent process and the child process.
· Print grand parent id.
· Stop the program.

Code :
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main() {
pid_t pid, mypid, myppid;
pid = getpid();
printf("Before fork: Process id is %d\n", pid);
pid = fork();
if (pid < 0) {
perror("fork() failure\n");
return 1;
}

// Child process
if (pid == 0) {
printf("This is child process\n");
mypid = getpid();
myppid = getppid();
printf("Process id is %d and PPID is %d\n", mypid, myppid);
} else { // Parent process
sleep(2);
printf("This is parent process\n");
mypid = getpid();
myppid = getppid();
printf("Process id is %d and PPID is %d\n", mypid, myppid);
printf("Newly created process id or child pid is %d\n", pid);
}
return 0;
}

Output:
Conclusion: In this way ,We studied about parent and child process creation.

Common questions

Powered by AI

Executing the given C program provides practical insights into the inherent hierarchical nature of processes in a Linux environment. The parent process creates a child process using fork(), illustrating the concept of process branches from a single parent. By printing process IDs (PIDs) and parent process IDs (PPIDs), the program visually confirms the linkage and dependencies between processes, thereby enhancing the understanding of process lifecycle and management. Additionally, displaying the grandparent process ID by indirectly accessing the parent of the parent process helps better understand multi-level process hierarchies as multiple generations of processes are simultaneously managed and synchronized within the operating system .

The C program demonstrates the concept of a parent-child relationship in process management by utilizing fork() to instantiate a child process from a parent process. The identifiers, such as the parent process ID (PPID) and the child process ID (PID), distinguish the parent from its child. The parent process executes the fork() call, receives the child's PID, and prints it, reinforcing its role as the creator. The child executes operations like printing its PID and obtaining the PPID, showcasing its dependent nature yet separate path. The execution order, slightly staggered by a sleep call in the parent, confirms the child typically runs immediately after fork() completion unless explicitly delayed, signaling potential synchronization strategies .

From the final program outputs and messages, one can conclude that process creation dynamics allow parent and child processes to effectively identify and communicate their states through PIDs and PPIDs. The displayed messages confirm the successful creation and execution of processes, while also identifying process lineage and structural hierarchy within the OS. Errors encountered during creation, indicated by fork() failure messages, highlight constraints linked to resource or limit restrictions. Thus, diagnostics drawn from output messages can effectively guide debugging, resource allocation, and system optimization techniques necessary for stable and efficient process management and execution .

In the program, process IDs (PIDs) and parent process IDs (PPIDs) are used to identify and differentiate between the parent and child processes. When fork() is called, both child and parent processes execute their respective blocks. The child's PID is printed using the getpid() function, and the parent process ID of the child (PPID) is obtained and displayed using getppid(). Within the parent block, similar functions getpid() and getppid() are used to display its PID and PPID respectively. Furthermore, the parent process also prints the child's PID, which is obtained as the return value of the fork() call in the parent context, allowing clear differentiation and understanding of process relationships .

The program handles errors during process creation by checking the return value of fork(). If fork() returns -1, it indicates failure to create a child process, possibly due to exceeding the system's process limit. In such cases, the program prints an error message using perror("fork() failure\n") and returns 1, thus exiting the program without proceeding further .

The given C program exemplifies the concepts of process control block (PCB) and process states through detailed process management techniques. When a fork() system call is made, the OS creates a PCB for the new child process, containing crucial data such as PID, PPID, program counter, and memory pointers. This encapsulates process state tracking and scheduling information. After creation, the child and parent process transitions control states from running to sleep and back or terminated based on execution paths and sleep() influence. The ability to print process IDs demonstrates dynamic state alteration via system calls or explicit user-level commands, providing a practical view of multiprogramming and multitasking principles with varying process stages in the lifecycle, as managed by Linux's native process scheduler .

Using fork() for process creation in operating systems presents several benefits, primarily in enabling parallel execution and resource sharing. Each new child process is a replica of the parent, ensuring memory and file descriptors are duplicated safely. This allows for concurrent processing, better utilization of CPU, and process-level isolation, leading to robust application designs. However, potential limitations include increased overhead due to the duplication of process states, potential for excessive resource consumption, and complexity in managing inter-process communications effectively. Additionally, improper handling of concurrent executions can lead to race conditions, signaling the need for prudent process synchronization and error-handling mechanisms .

The fork() system call is used in Linux to create a new process, which is known as a child process. It is called by the parent process, resulting in the creation of a new process. In the given C program, fork() is executed, and the callee becomes both the parent and child processes—the parent will continue its execution while the duplicate child process begins its execution simultaneously. The system call returns a zero to the newly created child process, and a non-zero (child's PID) to the parent process, allowing for separate execution flows as depicted in the program .

The program's output highlights the immediate bifurcation of execution flows following a fork() system call. This bifurcation is evident as the child process outputs 'This is child process' and its details immediately upon creation, while the parent process, due to a controlled delay via sleep(2), waits before executing its specific print statements. This delay ensures the child process executes its segment entirely before the parent process resumes, directly illustrating asynchronous execution and separate independent process paths tailored by the OS. The observed output clarifies the split between concurrent process executions after a fork() call, illuminating concurrent process management in a Linux environment .

The sleep() function in the parent process plays a critical role in process synchronization and temporal ordering of output. By invoking sleep(2), the parent process is intentionally suspended for two seconds, allowing the child process to execute and complete its print statements before the parent resumes operation. This logical ordering is pivotal in demonstrating controlled process execution flow and ensuring distinct outputs from the child appear ahead of the parent's outputs, reinforcing the asynchronous nature and independent execution paths of processes spawned from fork(), thereby aiding in clearer conceptual separation of execution outputs in concurrent processing .

Practical : 10
AIM:
To write a C program to perform process creation using fork() system call . 
OPERATING SYSTEM (OS) LAB IN
if (pid < 0) {
      perror("fork() failure
");
      return 1;
   }
   // Child process
   if (pid == 0) {
      printf(
Conclusion: In this way ,We studied about parent and child process creation.

You might also like