3

I need some help regarding job control in linux.

I run my program out of a terminal, like this

$ ./test.elf &

My program/process is then being run as the background process.

After a while (let say 5 seconds), I type fg to put my process in the foreground.

After a while (let say 5 seconds), I want my program to go back to the background process and put the bash program as the foreground process, so that I can continue using the terminal.

Here is what I do:

  1. When startng my pprogram, I get the group process id of the foreground process (the bash program) using tcgetpgrp function.
  2. I then wait until my process is the foreground process. I use the tcgetpgrp(STDIN__FILENO).
  3. I then wait ~5 seconds
  4. Using tcsetpgrp I put the process which I got in point 1 above, in the foreground.

Problem is, I can see that my process is put in the background and the bash program in the foreground, but I cann't enter command in the terminal. The bash doesn't "return", it is still waiting or sleeping.

I know I can press Ctrl-z and then bg but I don't want to use that because after Ctrl-z is pressed my process is stopped and that isn't good because I am running a "real-time" control application. So I want to switch back to the terminal "instantly" without stopping or pausing my program.

Sorry if my description of my problem seems confusing.

Any comments?

jcollado
  • 39,419
  • 8
  • 102
  • 133
joeks
  • 31
  • 1
  • 2
  • 1
    Why don't you use a dedicated terminal with your process running in foreground, while you do your other business in another terminal? – mouviciel Feb 24 '12 at 08:45
  • Yes, that is what will happen: I use a dedicated terminal for testing and debugging my program (while in foreground) but when I am finished I want to terminate the terminal without stopping my program or pressing ctrl-z and then 'disown' – joeks Feb 24 '12 at 08:59
  • If you terminate the terminal, how could it be possible to give control back to it? – mouviciel Feb 24 '12 at 09:10

2 Answers2

1

I have to admit that I am not 100% if this is the solution you are looking for, but what about using the GNU Screen utility? Your app can be running in one screen, while you have an available terminal in the other screen. Switching between screens is as easy as one keyboard-shortcut. The documentation for GNU screen can be found here.

Another option could be the disown command (only available in Bash as far as I know). It lets you put a process to run in the background and some documentation is found here.

Kristian Evensen
  • 1,315
  • 13
  • 14
  • But when my process is in the foreground, how do I get back to the terminal to type in 'disown' (without stopping my process with ctrl-z)? I basically want to perform 'bg' and 'fg' but in my program. – joeks Feb 24 '12 at 09:03
  • Hm, now that is a good question. Does this application only provide output or is it interactive? If it only provides output, one solution, even though it might be overkill, would be to fork the process and use named pipes to provide output to a client-like application. – Kristian Evensen Feb 24 '12 at 09:33
  • It is interactive (reads commands from stdin). A thread reads commands from stdin and output info on stdout. When it receives a certain command it must "exit" the terminal and give stdin,stdout back to the terminal but the whole process must still be running – joeks Feb 24 '12 at 09:39
  • Hm, are you sure you are using tcget/setgrp correctly? I.e., following the instructions [here](http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC421) (especially the last two lines in this put_job_in_foreground example). I have had some strange behavior due to me not resoting state properly when switching between processes. – Kristian Evensen Feb 24 '12 at 09:56
0

you can add the following in you script . it will give you terminal back for 5 second while your process in background .

timelimit=5; ## The time limit in seconds
NEW_PROMPT_COMMAND="; if [[ \\\$SECONDS -gt $timelimit ]]; then echo \\\"---Timelimit hit, shutting down shell---\\\"; flush_keyboard_buffer; exit; fi";
touch ~/.bashrc_timelimit;
cp ~/.bashrc ~/.bashrc_timelimit;
(cat ~/.bashrc; echo -e "\nPROMPT_COMMAND+=\"$NEW_PROMPT_COMMAND\"") > ~/.bashrc_timelimit;
bash --rcfile ~/.bashrc_timelimit;

try this running script for batter understanding -

function pause(){
read -n 1 -p "$*"
}
clear
until [ "selection" = "0" ] ; do
echo " "
echo -e '\t\t\t' "Unix Helper Utility"
echo -e '\t\t\t' "==================="
echo " "
echo "[1] List File names in current directory"
echo "[2] Show Time and Date"
echo "[3] Process ID"
echo "[4] Send this menu to Background"
echo "[0] Exit"
echo " "
echo "Please pick an option listed above: "
read selection
echo " "
case $selection in
    1 ) clear
    echo " Current Directory list:"
    ls
    pause
    clear ;;
    2 ) clear
    date
    pause
    clear ;;
    3 ) clear
    echo "option 3"
    pause
    clear ;;
    4 ) clear
    timelimit=5; ## The time limit in seconds
    NEW_PROMPT_COMMAND="; if [[ \\\$SECONDS -gt $timelimit ]]; then echo \\\"---Timelimit hit, shutting down shell---\\\"; flush_keyboard_buffer; exit;             fi";
    touch ~/.bashrc_timelimit;
    cp ~/.bashrc ~/.bashrc_timelimit;
    (cat ~/.bashrc; echo -e "\nPROMPT_COMMAND+=\"$NEW_PROMPT_COMMAND\"") > ~/.bashrc_timelimit;
    bash --rcfile ~/.bashrc_timelimit;
    clear ;;
    0 ) clear
    exit
    esac
done
raj_gt1
  • 4,653
  • 2
  • 20
  • 28