Skip to content
Snippets Groups Projects
Select Git revision
  • main
1 result

questions.md

Blame
  • questions.md 4.21 KiB
    1. Can you think of why we use fork/execvp instead of just calling execvp directly? What value do you think the fork provides?

      Answer: We need to use fork() since execvp() completely replaces the current process. If we just ran execvp(), our shell would be replaced with the command and then exited. We use fork() to create a copy of our shell process first, allowing the child process to run the command while the parent shell process continues to take commands. This also allows us to execute many commands and wait for them to complete.

    2. What happens if the fork() system call fails? How does your implementation handle this scenario?

      Answer: Fork() sets errno to indicate the error and returns -1 if it fails. Our implementation would terminate the program and print an error message.

    3. How does execvp() find the command to execute? What system environment variable plays a role in this process?

      Answer: In the directories specified in the PATH environment variable, execvp() looks for the command.

    4. What is the purpose of calling wait() in the parent process after forking? What would happen if we didn’t call it?

      Answer: After a fork, the parent process calls wait() to wait for the child process to finish and obtain its exit state. This enables the parent process to manage the errors or output of the child process. The parent process would proceed right away after forking without waiting for the child process to finish if wait() wasn't used.

    5. In the referenced demo code we used WEXITSTATUS(). What information does this provide, and why is it important?

      Answer: The child process's exit status is provided by WEXITSTATUS(). It's crucial since it enables the parent process to determine if the child process ended successfully or unsuccessfully.

    6. Describe how your implementation of build_cmd_buff() handles quoted arguments. Why is this necessary?

      Answer: I use it for execvp() after copying every character in an argument—aside from the quote marks—to a new string. Prior to that, all leading and trailing spaces are eliminated. This is required because one argument with white spaces between it is regarded as several arguments if quotation marks are not removed by excecvp().

    7. What changes did you make to your parsing logic compared to the previous assignment? Were there any unexpected challenges in refactoring your old code?

      Answer: In this implementation, I employed a straightforward parsing technique that uses strtok() to divide the command line into the executable name and parameters. The command name is the first token, while the remaining tokens are the arguments. The primary difficulty was ensuring that the argv array was correctly NULL-terminated for execvp() to function.

    8. For this quesiton, you need to do some research on Linux signals. You can use this google search to get started.

    • What is the purpose of signals in a Linux system, and how do they differ from other forms of interprocess communication (IPC)?

      Answer: In a Linux system, signals are used to facilitate communication between processes. Signals are asynchronous and unidirectional, which sets them apart from other types of IPC like pipes or sockets.

    • Find and describe three commonly used signals (e.g., SIGKILL, SIGTERM, SIGINT). What are their typical use cases?

      Answer: Identify and explain three frequently used signals (such as SIGKILL, SIGTERM, and SIGINT). Which usage cases are normal for them?

    • What happens when a process receives SIGSTOP? Can it be caught or ignored like SIGINT? Why or why not?

      Answer: A process instantly freezes (suspends) upon receiving SIGSTOP. Since SIGSTOP is meant to always return control to the user, it cannot be intercepted or disregarded like SIGINT. This is crucial for process management and debugging since you need a dependable method of stopping a process. With SIGCONT, the procedure can be continued at a later time.