#include <stdio.h>
#include <unistd.h>     // for fork(), execlp(), getpid()
#include <sys/types.h>  // for pid_t
#include <sys/wait.h>   // for wait()
#include <stdlib.h>     // for exit()

#define MAX_COUNT 5

// Function declarations
void parentProcess();
void childProcess();

int main()
{
    pid_t pid;

    // Create child process
    pid = fork();

    // If fork fails
    if (pid < 0)
    {
        printf("Fork failed\n");
        return 1;
    }

    // Child process
    if (pid == 0)
    {
        childProcess();
    }
    // Parent process
    else
    {
        parentProcess();
    }

    return 0;
}

// Child Process Function
void childProcess()
{
    printf("Output of execlp system call:\n");

    // Executes 'ls' command
    execlp("/bin/ls", "ls", NULL);

    // This line runs only if execlp fails
    printf("execlp failed\n");
}

// Parent Process Function
void parentProcess()
{
    int i;
    pid_t processID;

    // Wait for child process to complete
    wait(NULL);

    // Get parent process ID
    processID = getpid();

    // Print lines from parent
    for (i = 1; i <= MAX_COUNT; i++)
    {
        printf("Line from parent process = %d\n", i);
    }

    printf("Parent process is done\n");
    printf("Parent Process ID = %d\n", processID);

    exit(0);
}