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?"