Aim:
To write a program that adds two integers provided via the command line and compile it to an executable named myadder.

To write another program that uses fork() to create a child process and make the child process execute the myadder program using execvp().

🧩 Theory:
The fork() system call creates a new process (a child) by duplicating the calling process.

The exec() family of functions replaces the current process image with a new program.

execvp() searches for the program in the PATH and executes it with given arguments.

When a child process calls execvp(), its memory image is replaced with that of the new program — meaning the child becomes the new program (myadder in this case).

🧪 Step 1: Program 1 — myadder.c
This program reads two integers from the command line, adds them, and prints the result.

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

int main(int argc, char *argv[]) {
    if (argc != 3) {
        printf("Usage: %s <num1> <num2>\n", argv[0]);
        return 1;
    }

    int a = atoi(argv[1]);
    int b = atoi(argv[2]);
    int sum = a + b;

    printf("Sum of %d and %d is %d\n", a, b, sum);

    return 0;
}
🧩 Compile and Test
gcc myadder.c -o myadder
./myadder 10 20
Output:

Sum of 10 and 20 is 30
✅ myadder is ready.

🧪 Step 2: Program 2 — fork_exec.c
This program creates a child process using fork().
The child process executes the myadder program using execvp().

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
    pid_t pid;
    pid = fork();   // Create a child process

    if (pid < 0) {
        perror("Fork failed");
        exit(1);
    } 
    else if (pid == 0) {
        // Child process
        printf("Child process (PID: %d) executing myadder...\n", getpid());

        // Arguments for myadder
        char *args[] = {"./myadder", "10", "20", NULL};

        // Replace current process with myadder
        execvp(args[0], args);

        // execvp returns only if it fails
        perror("execvp failed");
        exit(1);
    } 
    else {
        // Parent process
        printf("Parent process (PID: %d) created child (PID: %d)\n", getpid(), pid);
        wait(NULL);  // Wait for child to finish
        printf("Child completed execution.\n");
    }

    return 0;
}
🧩 Compile and Run
gcc fork_exec.c -o fork_exec
./fork_exec
🧾 Sample Output:
Parent process (PID: 4100) created child (PID: 4101)
Child process (PID: 4101) executing myadder...
Sum of 10 and 20 is 30
Child completed execution.
🧩 Step 3: Explanation
Concept	Description
fork()	        Creates a child process. Both parent and child continue execution.
pid == 0	        Indicates the child process.
execvp()	        Replaces the child’s process image with another executable (myadder).
wait()	        Makes the parent wait until the child finishes.
After execvp():

The child process stops executing fork_exec.c code.

It becomes the myadder process.

Once myadder completes, the child terminates, and the parent resumes.

🧩 Step 4: Verify Process Replacement
While running fork_exec, observe the process list using another terminal:

ps -ef | grep myadder
You’ll see something like:

user  4101  4100  0 11:45 pts/0  00:00:00 ./myadder
➡️ This confirms that the child process has been replaced by the myadder image.

🧩 Step 5: Observe Using pstree
Run pstree to visualize the process hierarchy:

pstree -p
Example Output:

systemd(1)─┬─bash(4000)───fork_exec(4100)───myadder(4101)
This shows:

fork_exec created a child process

The child was replaced by the myadder program

🧾 Summary Table
Step	Operation	    Function/Command	Observation
1	Create process	    fork()	    Child and parent created
2	Replace process image	    execvp()	    Child replaced with myadder
3	Synchronization	    wait()	    Parent waits for child
4	Verify tree	    pstree	    Visual process hierarchy
✅ Result:
A child process was successfully created using fork().

The child process was replaced with the myadder executable using execvp().

The parent process waited until the child finished execution.

Verified using pstree that the child process became myadder.

