Questions about background/foreground running processes
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Questions about background/foreground running processes
I have an Arch X64 running Awesome WM stand-alone.
I've been reading about processes in Linux. And I came up with some questions, that I couldn't find good answers to.
1- say I ran something at the background: "vlc &"
I can now type whatever commands I want in the shell that I used to start vlc, if I type "exit", the shell is destroyed and vlc will remain alive. BUT if I quit the shell with my Awesome WM quit keyboard shortcut (Ctrl+Shift+C by default), both the shell and vlc die!
Why did vlc die the 2nd time, but didn't in the first time? I think this has to do with the way awesome kills the thing itself, right?
I read about using 'disown %n' where n is the job number. (I also tried typing its name, as in 'disown vlc' it also worked )
After disowning it, quitting in both ways won't kill vlc.
What's going on?
2- I read that, everytime a user process runs, it MUST get assosiated with a shell (because it's a user process, not a system process, daemon)
But when I run something like, firefox, (not from a terminal, but from a launcher bar, or a globally-set key, via awesome or something) I don't see a shell. And when I "ps -elf | grep firefox" I get a '?' in the tty column. I read that the '?' appears when the process is a system process, or when the process disassociate itself from its shell. I'm assuming it's the 2nd case here. This gives birth to:
A) Can a user process live without a shell?
B) I'm guessing that: firefox first created within a shell, then it forked it self into another shell, and the prvious shell (the one that created it) died, so we don't know the original tty who created it, since it has died. Is this true?
3- What happens actually, when a process is run in the background?
is there a shell associated with it?
I just, can't go on reading a book or something, if logical explanation to details like this is missing.
I just worked out something that adds more credibility to my 2nd point, here's what I did:
in a terminal: "vlc &" checked out its tty, it was 'pts/3' and then "exit" the shell I ran vlc from. opened up another terminal, did a
"ps -fle | grep vlc" looked at the tty column, and guess what, it was '?'
So, is this like a slow motion to what actually happened?
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986
Rep:
Quote:
2- I read that, everytime a user process runs, it MUST get associated with a shell (because it's a user process, not a system process, daemon)
I believe this is only true if not using a GUI. If you are using a GUI bash is not involved,no shell. A background process as far as BASH is concerned is a way to be able to keep using BASH while a different process is running otherwise the terminal is occupied until the process is finished.
The shell term is not very well understood. BASH is a interpreter that executes commands that are kept in a separate library from what I understand. Commands are small separate applications that BASH coordinates with other commands. A shell is a method to interact with the operating system. A desktop environment is like a shell. The kernel compared to an onion is a bunch of garbage if that is where you are getting the info from in my opinion .http://www.linux-tutorial.info/modul...ntent&pageid=5 This guide while well meaning is too terse to be much good.Read it last.
Some Commands that BASH executes are not killed when the ("shell" invocation of BASH) is closed. BASH is not the only interpreter that can be used as a shell. There are a few others.
If this reply was useful please move my reputation mark off of 49. I'm sick of looking at it.Thanks!
Last edited by theKbStockpiler; 03-04-2013 at 07:45 PM.
1 - vlc is still running in that shell, it's just in the background. Typing exit tells the shell that you want to close, but it won't actually close until all background processes have stopped. If you force quit it, all foreground and background processes will be killed along with it. This all becomes VERY clear if you ssh into a machine and repeat your experiment. You can launch something in the background on the remote machine, then type exit. What happens? The screen goes black, ssh is sitting there waiting for the background process to exit so that it can return you back to your own machine. If you kill the ssh connection (close the terminal, type ~. or whatever, then the background process will also be killed).
2a - Processes don't need a shell. Of course if you start the process from a shell, and then subsequently kill the shell while the process is still running, you'll kill the process as well, but there are ways around that. For example, processes started through cron have no shell, they're just running in the background dumping output to a service that mails the user when the process is finished. Alternatively, you can start a process using "nohup" to TRULY fork it to the background and disassociate it from your shell. If you do that, you can do whatever you want with your shell, keep running things, type exit, kill it, it doesn't matter. Perhaps it's best to think of running a command with a trailing "&" like you're not really running it in the background, you're just separating it's I/O from your terminal. It's still running in your shell, it's just not writing to it, and things you type aren't being written to it.
2b - There probably was no tty for firefox, same as if a command is started from cron.
Last edited by suicidaleggroll; 03-05-2013 at 07:56 PM.
Okay, now here's the answer you're looking for ...
If you launch a background command (sometimes called a "job"), it is owned by your current bash-shell. You can switch which one has input focus.
If you want a command to survive your logoff, use "nohup." It may or may not do so.
All user commands are owned by some shell. Your GUI session is a shell. Shells can be nested, as all processes can.
The CPU will always try to run at 100% utilization. "Background" vs. "Foreground" sets the dispatching-priority, which is one of several variables used to choose which process or thread to run next on a particular CPU.
CPU-intensive work actually runs better at lower priority because it likes long, uninterrupted slots of time but can readily afford to give them up. I/O-intensive work (and GUIs) need quick response. By running CPU-intensive work with "nice," you help the scheduler make good choices for the work.
Last edited by sundialsvcs; 03-06-2013 at 08:27 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.