1

If I run a program from bash, then does my program belong to the same process group as the shell, or does it belong to a new process group?

As I am making a shell in C, I need to handle the SIGINT signal.

Now Ctrl+C sends a SIGINT signal to the processes in the foreground but belonging to the same process group.

I am unable to terminate the bash session.

1 Answer 1

3
bash-4.3$ ps -j | cat
  PID  PGID   SID TTY          TIME CMD
 4586  4586  4586 pts/1    00:00:00 bash
 4600  4600  4586 pts/1    00:00:00 ps
 4601  4600  4586 pts/1    00:00:00 cat
bash-4.3$ ps -j; ps -j
  PID  PGID   SID TTY          TIME CMD
 4586  4586  4586 pts/1    00:00:00 bash
 4602  4602  4586 pts/1    00:00:00 ps
  PID  PGID   SID TTY          TIME CMD
 4586  4586  4586 pts/1    00:00:00 bash
 4603  4603  4586 pts/1    00:00:00 ps

An interactive shell job is inplemented with a process group. That's what process groups have been created for: run interactive shell jobs. Interactive shells are about the only commands that actually mingle with process groups.

Above ps -j | cat is a (foreground) job. Both ps and cat are in the same process group, the shell made it the foreground process group of the terminal, so that a ^C causes a SIGINT to be sent to them.

In the second example, two consecutive jobs.

In

bash-4.3$ (ps -j; ps -j)
  PID  PGID   SID TTY          TIME CMD
 4586  4586  4586 pts/1    00:00:00 bash
 4633  4633  4586 pts/1    00:00:00 bash
 4634  4633  4586 pts/1    00:00:00 ps
  PID  PGID   SID TTY          TIME CMD
 4586  4586  4586 pts/1    00:00:00 bash
 4633  4633  4586 pts/1    00:00:00 bash
 4635  4633  4586 pts/1    00:00:00 ps

The subshell is one job. The subshell itself could be seen as a non-interactive shell.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .