quiz Informatica · 24 questions

Shell Process Management and Expansion

help_outline 24 questions
timer ~12 min
auto_awesome AI-generated
0 / 24
Score : 0%
1

When a Bash command is executed, which two system calls are performed first to create the new process?

2

What is the effect of the exec() system call on the process image?

3

In Bash, which meta‑character initiates a tilde expansion?

4

Which of the following is true about the Bash builtin trap pseudo‑signal DEBUG?

5

When a pipeline contains a builtin command, what does Bash create for the builtin?

6

What does the redirection operator '2>&1' accomplish?

7

Which command can be used to permanently ignore a specific signal for a process?

8

What is the purpose of the 'nice' command when launching a process?

9

If a Bash script contains the line 'exec 3< filein', what is the effect?

10

Which of the following statements about the 'wait' builtin is correct?

11

When using 'ls | sort', which system call is used to create the pipe between the two processes?

12

What is the effect of setting the SGID bit on a directory?

13

Which of the following best describes the purpose of the 'chmod 2770' command on a directory?

14

When a process opens a file with the flag O_CREAT, which of the following is true?

15

What does the command 'ps aux' display?

16

Which of the following is true about the 'systemd' target unit type?

17

When configuring a static IP address with the 'ip' command, which subcommand is used?

18

What is the primary purpose of the 'systemd-run --on-active=1w' command?

19

Which of the following statements about the 'chmod' symbolic mode 'a=r,g-rx,u+rw' is correct?

20

When a process receives a SIGINT signal from the terminal, what is the default disposition?

21

Which of the following is true about the 'systemctl mask' command?

22

What does the 'find / -type f -nouser -mtime -2' command locate?

23

In the context of LDAP, what does the attribute type 'cn' represent?

24

Which of the following best describes the effect of the 'nice' command when used by a non‑root user with a negative value?

menu_book

Shell Process Management and Expansion

Review key concepts before taking the quiz

Introduction to Bash Process Management and Expansion

Understanding how the Linux Bash shell creates, controls, and manipulates processes is essential for anyone working with system administration or software development. This course breaks down the core concepts tested in a typical quiz on Shell Process Management and Expansion. By the end of the lesson you will be able to explain the fork() and exec() sequence, describe tilde expansion, use the trap builtin effectively, and master common redirection and priority‑setting commands.

1. The Fork‑Exec Sequence in Bash

When you type a command at the Bash prompt, the shell does not simply run the program directly. It follows a two‑step system‑call pattern that is fundamental to Unix‑like operating systems.

1.1 fork() – Creating a Child Process

The first system call is fork(). This call duplicates the current process, producing a child that inherits the parent’s memory layout, environment variables, and open file descriptors. The child receives a unique process identifier (PID) while the parent continues execution.

1.2 exec() – Replacing the Process Image

Immediately after fork(), Bash invokes exec() (or one of its variants such as execve()) in the child. The exec() call replaces the child’s memory image with the program you asked to run, loading the new executable code, data, and stack. The original shell code disappears, but the PID remains the same.

Key takeaway: The correct order is fork() then exec(). This sequence ensures that the new program runs in its own process while the original shell can continue to accept further commands.

2. What Happens to the Process Image After exec()?

The exec() family of system calls performs a complete transformation of the calling process. It does not create a new process; instead, it overwrites the existing one.

  • Replaces the current process image with the binary specified.
  • All previous code, data, and heap are discarded.
  • Open file descriptors remain open unless they have the FD_CLOEXEC flag set.
  • Environment variables are preserved, allowing the new program to inherit the shell’s environment.

This behavior is why exec() is often described as “the process becomes the new program”. It is a cornerstone of how shells launch external utilities without spawning unnecessary intermediate processes.

3. Tilde Expansion in Bash

Meta‑characters are symbols that Bash interprets before executing a command. The tilde (~) is one of the most common and useful.

3.1 Initiating Tilde Expansion

When Bash encounters a tilde at the beginning of a word, it expands it to the home directory of the appropriate user:

  • ~ → current user’s $HOME
  • ~root/root (or the home directory of the user root)
  • ~+ or ~-$PWD or $OLDPWD respectively

Because the tilde is a meta‑character, it must appear unquoted; quoting it (e.g., "~") disables the expansion.

4. The Bash Builtin trap and the DEBUG Pseudo‑Signal

The trap builtin lets you specify commands to run when the shell receives signals or reaches certain execution points. One of the less‑known pseudo‑signals is DEBUG.

4.1 When Does DEBUG Trigger?

DEBUG is executed before every simple command, pipeline, or compound command that the shell runs. This makes it an invaluable tool for:

  • Logging each command as it is executed.
  • Implementing custom debugging or auditing mechanisms.
  • Temporarily modifying environment variables on a per‑command basis.

Because it fires prior to execution, DEBUG can be used to capture the exact command line that will be run, which is useful for tracing complex scripts.

5. Pipelines and Builtin Commands: Subshell Creation

When you build a pipeline (e.g., grep pattern file | wc -l), Bash must decide how to run each component. For external commands this is straightforward: each segment runs in its own process. Builtins, however, require special handling.

5.1 Why a Subshell Is Needed

If a builtin were executed in the current shell, any side effects (like variable assignments) would affect the rest of the script, breaking the isolation expected from a pipeline. Therefore, Bash creates a subshell process for the builtin when it appears in a pipeline.

Example:

echo "Hello" | read line

Here read runs in a subshell, so the variable line is not set in the parent shell after the pipeline finishes.

6. Understanding Redirection 2>&1

File descriptor manipulation is a core skill for any Bash user. The expression 2>&1 is a concise way to redirect the standard error stream (fd 2) to the same destination as standard output (fd 1).

6.1 How It Works

  • First, Bash evaluates the right‑hand side (&1) to determine where fd 1 is currently pointing.
  • Then it duplicates that file descriptor onto fd 2, making both descriptors refer to the same open file or pipe.

Typical usage:

command >output.txt 2>&1

This ensures that both normal output and error messages are captured in output.txt, simplifying log collection and debugging.

7. Permanently Ignoring a Signal with trap

Signals are asynchronous notifications sent to processes. While many signals have default actions (terminate, stop, etc.), you can instruct a process to ignore a specific signal indefinitely.

7.1 The Correct Syntax

Use the trap builtin with an empty string as the command:

trap '' SIGINT

This tells Bash to ignore SIGINT (generated by Ctrl‑C) for the lifetime of the script or until the trap is reset. It is the most reliable method because it works across all POSIX‑compatible shells.

8. Adjusting Process Priority with nice

The nice command influences the Linux scheduler by assigning a niceness value to a process. The range is typically –20 (most favorable) to +19 (least favorable).

8.1 Practical Use Cases

  • Running a CPU‑intensive backup with nice -n 10 tar -czf backup.tgz /data so it yields CPU time to interactive tasks.
  • Lowering the priority of a long‑running data‑analysis script to keep the system responsive.
  • Increasing priority (with renice or sudo nice -n -5) for time‑critical services.

Remember that only privileged users can set a negative niceness value; regular users can only increase the niceness (i.e., lower priority).

Conclusion and Further Study

Mastering Bash process management, expansion rules, and signal handling equips you with the tools to write robust, efficient, and maintainable scripts. Review each section, experiment with the commands in a safe test environment, and explore the official Bash manual for deeper insights. By internalizing the fork‑exec model, the nuances of trap DEBUG, and the power of redirection and nice, you’ll be prepared for advanced system‑level programming and troubleshooting tasks.

Stop highlighting.
Start learning.

Join students who have already generated over 50,000 quizzes on Quizly. It's free to get started.