q2

 #include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/wait.h>


int main() {

    pid_t pid;

    int status;


    // Fork a child process

    pid = fork();


    // Error handling

    if (pid < 0) {

        printf("Fork failed\n");

        exit(EXIT_FAILURE);

    }


    // Child process

    if (pid == 0) {

        // Execute 'ls' command to list files

        execlp("/bin/ls", "ls", NULL);

        // If execlp fails, print an error message

        printf("Execution failed\n");

        exit(EXIT_FAILURE);

    } 

    // Parent process

    else {

        // Wait for the child process to finish

        waitpid(pid, &status, 0);

        printf("Child process finished\n");

    }


    return 0;

}


Post a Comment

0 Comments