简体   繁体   中英

What happens to the previous foreground job, when command fg a new job?

A shell can only have one foreground job at a time. When using fg to place a job to the foreground, what would happen to the previous running foreground job? Will the shell send a SIGTSTP to it, and change job state? Or will it just be put to the background by bg?

To start a job in the foreground in the same shell the previous job first needs to be send to the background with CTRL+Z and bg . If you omit bg , the previous foreground job sent back with CTRL+Z would still be stopped (understood as: paused, not getting any processing time).

In other words: I am not aware of a way to place another foreground job in a shell, when another job is already running in the foreground of that same shell , because the stdin is used by the running foreground job.

Example:

$ sleep 4223
^Z
[1]+  Stopped                 sleep 4223
$ bg
[1]+ sleep 4223 &
$ sleep 1234
^Z
[2]+  Stopped                 sleep 1234
$ jobs 
[1]-  Running                 sleep 4223 &
[2]+  Stopped                 sleep 1234
$ fg 1
sleep 4223
fg 2  # this command has no effect, stdin is used by job 1
^Z
[1]+  Stopped                 sleep 4223
$ jobs
[1]+  Stopped                 sleep 4223
[2]-  Stopped                 sleep 1234
$ bg
[1]+ sleep 4223 &

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM