You are on page 1of 2

OPERATING SYSTEM

VIRTUAL LAB ASSINGMENT – 10

To understand the overlay concepts and practice how to overlay the current
process to new process in Linux using C.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
// Get the current process ID.
pid_t pid = getpid();

// Create a new process.


pid_t child_pid = fork();

// If the current process is the parent process, then overlay the current process to the
new process.
if (pid == 0) {
execl("/bin/bash", "bash", NULL);
}

// Wait for the child process to finish.


waitpid(child_pid, NULL, 0);

// Return 0.
return 0;
}
This code will overlay the current process to a new process that is running the bash shell.

The getpid() function returns the process ID of the current process. The fork() function
creates a child process that is an exact copy of the parent process. The execl() function
replaces the current process with a new process. The waitpid() function waits for a child
process to finish.

In this code, the fork() function is called in the main() function. If the fork() function
returns a value of 0, then the current process is the child process. Otherwise, the current
process is the parent process.

In the child process, the execl() function replaces the current process with a new process
that is running the bash shell.

In the parent process, the waitpid() function waits for the child process to finish.

This code can be used to overlay the current process to a new process in Linux.

Here are some additional notes about overlays:

Overlays are a technique for running programs that are larger than the size of the physical
memory available to the process.
Overlays work by dividing the program into smaller blocks, called overlays, that can be
loaded into memory as needed.
The operating system manages the overlay blocks, loading and unloading them as
needed.
Overlays can be used to run programs that are too large to fit in physical memory.
Overlays can also be used to improve the performance of programs by reducing the
number of times the operating system has to switch between processes.

You might also like