LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 12-20-2007, 08:18 AM   #1
a.toraby
Member
 
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
matthewg42
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
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
See here: http://www.tldp.org/LDP/abs/html/x8618.html#BGREF
 
Old 12-23-2007, 06:44 PM   #4
ta0kira
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.
ta0kira
 
  


Reply


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


All times are GMT -5. The time now is 11:05 AM.

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