fork() and exec() System Calls

fork() and exec() System Calls

Understanding Fork and Exec System Calls in Threading

Introduction to Threading Issues

  • The lecture transitions from multi-threading models and hyper-threading to discussing issues in threading, emphasizing the importance of understanding fork and exec system calls.
  • A prior lecture covered system calls; viewers are encouraged to review it for foundational knowledge.

Overview of Fork and Exec System Calls

  • Fork and exec system calls are specific to Linux-based systems, highlighting differences in usage across operating systems.
  • Learning about Linux is beneficial due to its open-source nature, allowing users to explore and modify the code.

Detailed Explanation of Fork System Call

  • The fork system call creates a separate duplicate process (child process) from the calling process (parent process).
  • The child process is an exact replica but has a different Process ID (PID), distinguishing it from the parent.

Understanding Exec System Call

  • The exec system call replaces the entire calling process with a new program specified as a parameter.
  • Unlike fork, exec does not create a new PID; instead, it retains the original PID while changing the contents of the process.

Practical Application: Writing Programs in C

  • To understand how fork and exec work practically, programs will be written in C language on a Linux system.
  • The demonstration will occur on Linux Mint, where two folders containing relevant programs have been created.

Running Example Program

  • An example program named FIIx1.c prints "Hello NASA Academy" along with its Process ID when executed.
  • Compilation is done using GCC (GNU Compiler Collection), generating an executable file named a.out.

Understanding the Fork System Call in C Programming

Introduction to Fork System Call

  • The speaker introduces the concept of the fork system call, indicating that they will edit a program to include this functionality and observe its behavior.
  • After editing, the program is compiled using GCC, resulting in an output file named a.out, which replaces any existing file with that name.

Execution of Forked Processes

  • Upon running the modified program, it prints "hello NASA Academy" twice, each time with a different Process ID (PID), demonstrating that the program executed two times due to the fork system call.
  • The first execution is by the parent process while the second is by a newly created child process. Each instance has distinct PIDs but identical output content.

Multiple Fork Calls and Their Effects

  • The speaker modifies the program to include three fork calls and prompts viewers to predict how many times "hello NASA Academy" will be printed upon execution.
  • After running the updated program, it outputs "hello NASA Academy" eight times, showcasing how multiple forks exponentially increase process creation.

Process Creation Breakdown

  • The explanation begins with one parent process (P1), which prints once. When P1 forks, it creates a child process (P2).
  • With each subsequent fork call, new processes are generated: for every existing process at each level of forking, another child is created.

Final Count of Processes

  • By analyzing all forks made during execution, it’s concluded that eight processes were created in total. Each printed statement had unique PIDs but identical content.
  • This section concludes with clarity on how fork works—creating separate processes while maintaining identical output across them.

Exploring Exec System Call

Introduction to Exec System Call

  • Transitioning from fork to exec system calls, two C programs (ex1.c and ex2.c) are introduced as examples for understanding exec's functionality.

Overview of Program Structure

Understanding the execl System Call in C Programming

Overview of execl System Call

  • The execl system call is part of a family of exec functions used to execute programs. The specific variant discussed here is execl, which allows passing arguments to the executed program.
  • The output from the first program (ex1) is passed as an argument to the execl system call, indicating how one program can invoke another.

Exploring ex2.c

  • In ex2.c, a main function includes two print statements: one confirms entry into the file and another prints its process ID using getpid().
  • Compiling both programs requires navigating to their directory and using GCC (GNU Compiler Collection). The default output file name is a.out, which can lead to conflicts if not specified differently.

Compiling Programs with GCC

  • To compile multiple programs without overwriting previous outputs, specify unique names for each output file using the -o option in GCC.
  • For example, compiling ex1.c creates an output named after it (e.g., ex1), while compiling ex2.c should create a separate output (e.g., ex2) to avoid confusion.

Execution Flow Between Programs

  • When executing the first program (ex1), it prints its PID and invokes the second program via the execl system call.
  • Upon running, control transfers from ex1.c to ex2.c, demonstrating how processes can replace their contents through this system call.

Process Replacement Mechanics

  • After invoking the second program, it displays its own PID. Notably, despite switching programs, the original PID remains unchanged because no new process was created; instead, content was replaced.
  • This behavior illustrates that while executing a new program replaces existing content within a process, it retains its identity through its PID.

Conclusion on execl Functionality

  • The lack of return control back to the first program after executing the second highlights that once control passes via an exec call, there’s no automatic return unless explicitly coded.

Understanding Fork and Exec System Calls

Overview of Process Management

  • The concept of a child process is introduced, highlighting that while the process ID remains the same, the contents can differ when using system calls like fork and exec.
  • The fork system call creates a new process by duplicating the existing one, allowing for concurrent execution.
  • The exec system call replaces the current process image with a new program, effectively changing what the process does while keeping its ID intact.
  • Understanding these two system calls is crucial for grasping how processes are managed in an operating system environment.