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

questions.md

Blame
  • questions.md 3.51 KiB

    Questions on Fork, Exec, and Linux Signals

    1. Can you think of why we use fork()/execvp() instead of just calling execvp() directly? What value do you think the fork() provides?

    Since fork creates a separate child process, it allows the command given as input to run separately from the shell without it replacing the previous process that was running. It allows the parents process to "wait" for the child process to finish or continuing allowing for user inputs.

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

    If 'fork()' fails, i.e returning -1, is exits with 'EXIT_FAILURE' meaning it didn't execute correctly.

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

    Since the first argument for 'execvp()' is the name of a file, I woudl assume that the function looks for the executable in each directory in the path to the executable and attempts to execute it. If it doesn't exist in the path, then (at least in my code) it exits.

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

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

    It gives you the exit status of a child process, which is important if we wanted to know whether a child process exited properly or with error.

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

    Since 'build_cmd_list()' splits the stdin into tokens by the pipe character, i can call 'build_cmd_buff()' on each token. When I do, it goes through the token character by character until it comes across a double quote. If it does, it keeps everything between the first quote and second quote as one token, rather than splitting it by space or tab as would be handled normally. This is important for commands like 'echo' or 'grep' where text included inbetween quotes is supposed to be kept together and handled as one argument.

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

    Barely any changes. The only real refactoring I did from the previous assignments was to merge 'build_cmd_buff()' and the code to parse by pipe together, which required more removing code than adding code.

    1. For this question, 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)? Unlike other IPCs, signals can be received by the system at any point in time. Their purpose is to "notify" processes about events, such as process termination or execution.
      • Find and describe three commonly used signals (e.g., SIGKILL, SIGTERM, SIGINT). What are their typical use cases? SIGKILL - kills a process immediately, forcefully terminating it without clean up: example: kill -9 SIGTERM - requests for a process to be terminated, allowing clean up; example: 'kill ' SIGINT - interupts a currently running process; example: 'CTRL + C'
      • What happens when a process receives SIGSTOP? Can it be caught or ignored like SIGINT? Why or why not? Both causes a process to pause however SIGSTOP (CTRL + Z) is forceful and must be adhered to whereas SIGINT can be handled by the process and subsequently ignored.