Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

[In C] You will write a program to offer a list of several functions. When your

ID: 3886939 • Letter: #

Question

[In C]

You will write a program to offer a list of several functions. When your program is started, show a menu of three functions as follows:

1. Hello World!

2. List files

3. Exit

Please select:

After the user makes his choice by typing the index of the function, your program should read the user input into a 1024-byte buffer, and then execute the corresponding function.

1. If the user input is 1, create a new process with fork()and print out Hello World! in the child process. The parent process should wait for its child process to complete. Then reprint the menu and ask the user for choice.

2. If the user input is 2, create a new process with fork()and run “ls” in the child process to list all the files in the current directory. The parent process should wait for its child process to complete. Then reprint the menu and ask the user for choice.

3. If the user input is 3, the program terminates.

4. If the user input is not 1, 2, or 3, print out an error message, reprint the menu and ask the user for choice.

Hints:
1. You can use the function fgets() to read user input into a buffer. The declaration of fgets() is char *fgets(char *str, int n, FILE *stream) When FILE *stream is stdin, fgets()reads user input from standard input.
2. After your program reads user input into the buffer, the user input is stored as a string. When you compare the user input with the indexs of the functions on the list, you may need to covert the user input from a string to an integer. You can use the function atoi(). The declaration of atoi()is int atoi(const char *str)
3. How to use execlp()to list all the files in the current directory can be found in the example in our lecture note. You can also find how to print out an error message there. 4. Remember to include necessary header files. Here is a sample execution on a Linux machine:

1. Hello World!
2. List files
3. Exit
Please select: 1
Hello World!
1. Hello World!
2. List files
3. Exit
Please select: 2
file1 file2 file3
1. Hello World!
2. List files
3. Exit
Please select: 4
Invalid choice!
1. Hello World!
2. List files
3. Exit
Please select: 3

Explanation / Answer

#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main () {

    char* inputString = calloc(1,1);
    const int maxCharInput = 3;
    pid_t pid;

    /* for case 1 */
    char str[20];
    const int td = 1;
    char* buf = str;
    size_t count;

    printf(" ");
    do {
        printf("1. Hello World! 2. List files");
        printf(" 3. Exit Please select: ");

        fgets(inputString, maxCharInput, stdin);

        switch (atoi(inputString)) {
            case 1:
                pid = fork();
                if (pid == 0) {
                    count = sprintf(str, "hello world! ");
                    write(1, str, count);
                    exit(0);
                } else if (pid < 0) { /* error */
                    fprintf(stderr, "Fork failed ");
                    exit(1);
                } else { /* parent process */
                    wait(NULL);
                }
                break;

            case 2:
                pid = fork();
                if (pid == 0) {
                    execlp("/bin/ls", "ls", NULL);
                    exit(0);
                } else if (pid < 0) { /* error */
                    fprintf(stderr, "Fork failed ");
                    exit(1);
                } else { /* parent process */
                    wait(NULL);
                }
                break;

            case 3:
                exit(0);
                break;
            default:
                printf("Invalid choice! ");
                break;
        }
    } while (1);

    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote