kill and signal types in Linux CLI - Time & Space Complexity
When using the kill command to send signals to processes, it is important to understand how the time to send signals grows as the number of processes increases.
We want to know how the execution time changes when signaling many processes.
Analyze the time complexity of the following script that sends signals to multiple processes.
#!/bin/bash
for pid in $(cat pids.txt); do
kill -SIGTERM "$pid"
done
This script reads process IDs from a file and sends the SIGTERM signal to each process one by one.
- Primary operation: Sending a signal with
killinside a loop. - How many times: Once for each process ID in the list.
As the number of process IDs increases, the script sends more signals, so the total time grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 signals sent |
| 100 | 100 signals sent |
| 1000 | 1000 signals sent |
Pattern observation: The number of operations grows linearly with the number of processes.
Time Complexity: O(n)
This means the time to send signals grows directly in proportion to how many processes you signal.
[X] Wrong: "Sending signals to many processes happens instantly no matter how many there are."
[OK] Correct: Each signal requires a system call and some processing time, so more processes mean more total time.
Understanding how loops and system calls scale helps you write efficient scripts and explain your reasoning clearly in interviews.
"What if we sent signals in parallel using background jobs? How would the time complexity change?"
Practice
kill command if no signal is specified?Solution
Step 1: Understand the default behavior of
Thekillkillcommand sends signals to processes. If no signal is specified, it sends the default signal.Step 2: Identify the default signal
The default signal isSIGTERM, which asks the process to terminate gracefully, allowing cleanup.Final Answer:
SIGTERM (terminate gracefully) -> Option BQuick Check:
Default kill signal = SIGTERM [OK]
- Confusing SIGKILL as default signal
- Thinking SIGSTOP pauses instead of terminates
- Assuming SIGINT is default for kill
Solution
Step 1: Recall the syntax for sending signals with kill
Thekillcommand can use-s SIGNALor numeric signal with a dash before the number.Step 2: Identify the correct numeric signal and position
-9is the numeric code for SIGKILL. The correct syntax iskill -9 PID. All other options incorrectly place the signal specification after the PID, which is invalid syntax.Final Answer:
kill -9 1234 -> Option DQuick Check:
kill -9 PID sends SIGKILL [OK]
- Placing signal after PID
- Using -SIGKILL instead of -s SIGKILL
- Confusing signal names and numbers
sleep 100 & kill -15 $!
Assuming
$! is the PID of the sleep process.Solution
Step 1: Understand the commands
sleep 100 &runs sleep in background.$!stores its PID.kill -15 $!sends SIGTERM (signal 15) to that PID.Step 2: Effect of SIGTERM on sleep
SIGTERM asks the process to stop gracefully. Sleep will stop waiting and exit cleanly.Final Answer:
The sleep process stops gracefully after receiving SIGTERM -> Option AQuick Check:
kill -15 sends SIGTERM = graceful stop [OK]
- Confusing SIGTERM with SIGKILL immediate stop
- Assuming sleep ignores SIGTERM
- Thinking kill command syntax is wrong
kill -15 5678 but it keeps running. What is the best fix?Solution
Step 1: Understand why SIGTERM might fail
SIGTERM (signal 15) asks process to stop nicely, but some processes ignore or handle it differently.Step 2: Use SIGKILL to force stop
SIGKILL (signal 9) forces immediate termination and cannot be ignored. Usingkill -9is the best fix for stubborn processes.Final Answer:
Use kill -9 5678 to force kill immediately -> Option CQuick Check:
kill -9 forces stop when SIGTERM fails [OK]
- Using kill without signal defaults to SIGTERM
- Using SIGHUP does not force stop
- Restarting system is last resort, not first
Solution
Step 1: Identify signals to pause and resume
SIGSTOPpauses a process immediately without termination.SIGCONTresumes a paused process.Step 2: Understand incorrect options
SIGTERMandSIGKILLterminate processes, not pause or resume.SIGINTinterrupts but does not pause/resume.Final Answer:
Send SIGSTOP to pause, then SIGCONT to resume -> Option AQuick Check:
Pause = SIGSTOP, Resume = SIGCONT [OK]
- Using SIGTERM or SIGKILL to pause processes
- Confusing SIGINT with pause/resume signals
- Thinking kill without signal pauses process
Ready to prove it?
Pass the test and this pattern moves unknown
