Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-20-2007, 08:18 AM   #1
Registered: Sep 2006
Location: tehran
Distribution: open suse & open solaris
Posts: 102

Rep: Reputation: 15
sending processes to background when they are running

i want to write a shell program that can send processes from background to foreground and and from foreground to back. how to i can do it? what instructions can help me?
thanks for any reply
Old 12-20-2007, 08:56 AM   #2
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
The terms "background" and "foreground" are not qualities of a process - they are to do with the shell, and it's relation to it's child progress. Control of this relationship is performed using the shell-internal commands bg and fg, and the key combination control-Z. See the bash manual page for details - you should read the JOB CONTROL section (search for that in all upper-case and you will find it).
Old 12-20-2007, 05:11 PM   #3
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,406

Rep: Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396
See here:
Old 12-23-2007, 06:44 PM   #4
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Each command line should take the form of a process group, created using setpgrp. All off the different programs involved in I/O redirection will be separate processes but will belong to the same process group. For example, with a command line of ls | grep hello you would create a set of pipe descriptors with pipe and two forks with fork (one for ls and one for grep hello,) making the output descriptor standard output for ls and the input descriptor standard input for grep hello. Normally you'd want to raise(SIGSTOP) in the fork(s) right before calling execvp (etc.) to give the shell time to set everything up. The shell would then set the process group of both forks to the process ID of the first fork and then send a SIGCONT signal to the process group to start command execution.

To determine which process group controls the terminal, the shell needs to call tcsetpgrp using the file descriptor of the controlling terminal as well as the group number of the command line which needs to take control. If the command is to be executed in the foreground right away, the shell should tcsetpgrp to the new process group before signaling the new process group with SIGCONT. The shell should tcsetpgrp back to itself when the foreground process ends. That should also be the behavior if the foreground process is stopped with SIGSTOP, as with [Ctrl]+Z in bash.

If a program is in the background and wants the terminal, it can call tcsetpgrp using getpgrp() as the process group and it will block until the shell gives it terminal control (the shell will be sent a SIGCHLD signal.)

In order for the shell to control which commands go to the foreground and background it must be a session leader. If the shell will be opening the terminal itself it will probably need setsid at some point. If you are going to call it from another shell such as bash you can just start it with exec and the shell program must avoid using setsid.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Urgent : Regarding sending a current running process in background Ricky Linux - General 5 11-27-2007 02:07 PM
Terminals and Background Processes dfego Linux - General 3 11-11-2006 08:52 AM
background processes digit Linux - Software 3 07-27-2003 12:27 PM
processes in background zetsui Linux - General 8 06-14-2003 12:52 PM
background processes hakum Programming 1 04-08-2002 10:29 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:39 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration