q1

 #include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/wait.h>


int main() {

    pid_t pid;

    int status;


    // Create a directory named 'mmm/lab7'

    pid = fork();

    if (pid == 0) {

        execlp("mkdir", "mkdir", "mmm/lab7", NULL);

        exit(EXIT_SUCCESS);

    } else {

        waitpid(pid, &status, 0);

    }


    // Create a file named 'test_file.txt' inside 'mmm/lab7' directory

    pid = fork();

    if (pid == 0) {

        execlp("echo", "echo", "Hello, World!", ">", "mmm/lab7/test_file.txt", NULL);

        exit(EXIT_SUCCESS);

    } else {

        waitpid(pid, &status, 0);

    }


    // List files in 'mmm/lab7' directory

    pid = fork();

    if (pid == 0) {

        execlp("ls", "ls", "mmm/lab7", NULL);

        exit(EXIT_SUCCESS);

    } else {

        waitpid(pid, &status, 0);

    }


    // Remove the directory 'mmm/lab7' and its contents

    pid = fork();

    if (pid == 0) {

        execlp("rm", "rm", "-r", "mmm/lab7", NULL);

        exit(EXIT_SUCCESS);

    } else {

        waitpid(pid, &status, 0);

    }


    return 0;

}


Post a Comment

0 Comments