How do I know if an executable program file is running in the foreground or in the background?

In my C program, I want to know if my foreground executable is running like this

$./a.out 

or how is it

 $./a.out & 
+4
source share
5 answers

If you are doing the main task,

 getpgrp() == tcgetpgrp(STDOUT_FILENO) 

or STDIN_FILENO or STDERR_FILENO or some file descriptor to which you are bound to the control terminal. (If you are not sure, open("/dev/tty") will always provide you with a file descriptor to your control terminal, if one exists.)

This is what openssh is , and it's a little easier than handling SIGTTIN/SIGTTOU if you just need a quick check.

On the other hand, you may have been associated with the background

  $ ./a.out
 ^ Z
 [1] + Stopped ./a.out
 $ bg
 [1] + ./a.out &

or in the foreground

  $ fg
 ./a.out

at any given time. You cannot expect that you can verify this once, and it will still be true (or false) later.

+6
source

From the Bash Reference Guide: Job Management Basics :

Background processes are those whose process group identifier differs from terminals; such processes are immune to the signals generated by the keyboard. You can only read foreground processes or write them to the terminal. Background processes that try to read (write to) the terminal are sent by the SIGTTIN (SIGTTOU) signal to the terminal driver, which, if it has not been delayed, pauses the process.

So, the solution is to install a signal handler for SIGTTIN , and then try reading from stdin (reset buffering or blocking it). If you get "0 byte read" back, then you are working in the foreground.

[EDIT] Please note that the status of the process may change. To do this, you can use shell job control commands (Ctrl-Z, bg , fg and jobs ).

+7
source

As far as I know, this is impossible and usually not necessary.

Please explain why you want to do this.

+1
source

[not valid]
IIRC, getppid () (on * nix systems) will give you the parent id. if it is 0, the console is your parent, so you are running in the background.
[/ Invalid]

[edit]

 int devtty; if ((devtty = open ("/dev/tty", O_RDWR)) < 0) printf ("daemon\n"); 

Note that this is true only for * nix systems (and then only if no one has deleted / dev / tty - for some reason)
[/ Edit]

0
source

Perhaps you have several processes running in the background:

 $ jobs [1] Stopped teamviewer [2]- Stopped vim [3]+ Stopped firefox 
  • use: fg %2 to bring the vim process to the fore.

  • To send the last process to the forefront, simply use: fg without arguments.

  • You can also enter% process_name to resume a stopped process.

To pause a process running in the background , use:

 kill -19 %job_id. 

Signal -19 is SIGSTOP (signal sent using Ctrl-Z).

you can always view the list by typing kill -l

Moving jobs between background / foreground:

If you already typed a command and forgot to use & , you can transfer the foreground job in the background by typing ^Z (CTRL-Z) to pause the job and then bg to put it in the background

 $ sleep 99 ^Z [1]+ Stopped sleep 99 $ bg [1]+ sleep 99 & 

You can list the jobs of the current shell using the jobs command.

Just remember that β€œexiting the shell” also affects the operation:

  • Jobs running in the background when you exit the shell are running.
  • Jobs paused ("Stopped") when the shell completes are completed.

Sending signals to tasks and processes

You can send signals, including completion signals, to jobs that are launched from the current shell using job numbers, using% (JOBID) instead of process numbers (PID):

 $ kill %1 [1]+ Terminated sleep 99 

To send signals to processes or jobs that do not start from the current shell, you first need to use ps to find their process numbers (PIDs).

You can refer to this link: processes and tasks

Common job management commands on Linux:

  • jobs - list of current jobs
  • fg - resume the next job in the queue
  • fg % [number] - resume work [number]
  • bg - click the next job in the queue in the background
  • bg % [number] - click the task [number] in the background
  • kill % [number] - kill the task with the number [number]
  • kill - [signal]% [number] - send a signal [signal] to the task number [number]
  • disable % [number] - cancel the process (the owner of the terminal will no longer be), so the command will be available even after the terminal is closed.

That is almost all of them. Pay attention to the% infront job numbers in the commands - this is what says you are talking about work, not about processes.

0
source

Source: https://habr.com/ru/post/908587/


All Articles