fg and bg commands in Linux CLI - Time & Space Complexity
We want to understand how the time to bring jobs to the foreground or background changes as the number of jobs grows.
How does using fg or bg scale when many jobs are running?
Analyze the time complexity of the following commands managing jobs.
# List jobs
jobs
# Send job 1 to background
bg %1
# Bring job 2 to foreground
fg %2
This snippet shows listing jobs, then moving specific jobs to background or foreground.
Look for operations repeated as the number of jobs increases.
- Primary operation: Searching the job list to find the job by its ID.
- How many times: Once per
fgorbgcommand, but the search scans through jobs.
As the number of jobs grows, finding the right job takes longer because the shell checks each job until it finds a match.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 jobs | About 10 checks to find a job |
| 100 jobs | About 100 checks |
| 1000 jobs | About 1000 checks |
Pattern observation: The time to find a job grows roughly in direct proportion to the number of jobs.
Time Complexity: O(n)
This means the time to bring a job to foreground or background grows linearly with the number of jobs.
[X] Wrong: "Using fg or bg is instant no matter how many jobs there are."
[OK] Correct: The shell must find the job in its list, so more jobs mean more searching time.
Understanding how commands scale with input size shows you think about efficiency, a key skill in scripting and automation.
"What if the shell stored jobs in a hash table instead of a list? How would the time complexity of fg and bg change?"
Practice
fg command do in a Linux shell?Solution
Step 1: Understand the purpose of
Thefgfgcommand is used to bring a job running in the background to the foreground so you can interact with it directly.Step 2: Compare with other commands
jobslists jobs,bgresumes jobs in background, and stopping or terminating jobs use other commands.Final Answer:
Brings a background job to the foreground for interaction -> Option AQuick Check:
fg= bring job to foreground [OK]
- Confusing fg with bg
- Thinking fg lists jobs
- Assuming fg stops jobs
Solution
Step 1: Identify the command to resume a stopped job in background
Thebgcommand resumes a stopped job in the background, allowing the terminal to be free.Step 2: Understand job specification
Using%1specifies job number 1. Sobg %1resumes job 1 in background.Final Answer:
bg %1 -> Option BQuick Check:
bg %jobnumberresumes job in background [OK]
- Using fg instead of bg to resume in background
- Trying to use jobs command to resume
- Using stop which is invalid
sleep 100 & jobs fg %1
What will happen after running
fg %1?Solution
Step 1: Analyze the commands
sleep 100 &runs sleep in background.jobslists it as job 1.fg %1brings job 1 to foreground.Step 2: Understand effect of fg
Bringing sleep to foreground means it blocks terminal until it finishes or is stopped.Final Answer:
The sleep command will be brought to the foreground and block the terminal -> Option DQuick Check:
fg %1= foreground job blocks terminal [OK]
- Thinking fg keeps job in background
- Assuming fg terminates job
- Believing %1 is invalid without jobs output
sleep 200 & but accidentally typed fg 1 instead of fg %1. What will happen?Solution
Step 1: Understand job specification syntax
Thefgcommand requires job IDs to be prefixed with%, e.g.,fg %1.Step 2: Analyze incorrect command
Typingfg 1without%causes shell to look for a job named '1' which does not exist, causing an error.Final Answer:
An error message will appear: 'fg: 1: no such job' -> Option CQuick Check:
Job IDs need % prefix for fg/bg [OK]
- Omitting % before job number
- Assuming fg accepts plain numbers
- Ignoring error messages
jobs as:[1]+ Stopped nano file1.txt [2]- Stopped vim file2.txt
How do you resume
vim file2.txt in the background and then bring nano file1.txt to the foreground?Solution
Step 1: Resume vim in background
Usebg %2to resume job 2 (vim) in background so it runs without blocking terminal.Step 2: Bring nano to foreground
Usefg %1to bring job 1 (nano) to foreground for interaction.Final Answer:
bg %2 followed by fg %1 -> Option AQuick Check:
bg %job runs in background, fg %job runs in foreground [OK]
- Mixing up job numbers
- Using fg for both jobs
- Not using % before job number
Ready to prove it?
Pass the test and this pattern moves unknown
