English 中文(简体)
Process Groups, Sessions & Job Control
  • 时间:2024-12-22

Process Groups, Sessions & Job Control


Previous Page Next Page  

In this chapter, we will get famipar with Process Groups, Sessions and Job Control.

Process Group − Process group is a collection of one or more processes. A process group constitutes of one or more processes sharing the same process group identifier (PGID). A process group ID (PGID) is of the same type (pid_t) as the process ID. A process group has a process group leader, which is the process that creates the group and whose process ID becomes the process group ID of the group.

Sessions − It is a collection of various process groups.

Job Control − This permits a shell user to simultaneously execute multiple commands (or jobs), one in the foreground and all remaining in the background. It is also possible to move the jobs from the foreground to the background and vice-versa.

Let us understand this with the help of example program/s using shell (BASH).

    Shell script (in BASH) to perform basic commands (date, echo, sleep and cal) named basic_commands.sh

    Shell script (in BASH) to perform basic commands (ps, echo)

#!/bin/bash
#basic_commands.sh

date
echo "Now sleeping for 250 seconds, so that testing job control functionapty is smooth"
sleep 250
cal

#!/bin/bash
#process_status.sh

ps
echo "Now sleeping for 200 seconds, so that testing job control functionapty is smooth"
sleep 200
ps

Use chmod command to give the file the execute permissions. By default, the normal file would get only read and write permissions and not execute permissions.

To stop the current running process, you need to enter CTRL+Z. This gives you a job number. The job can be resumed either in the foreground or the background. If needed, to resume the job in the foreground use ‘fg’ command. If needed, to resume the job in the background, use ‘bg’ command. By using this, it would run only the last stopped process. What if you want to start other than the last stopped process? Just use the job number after fg or bg (say bg %2 or bg %3, etc). If the running job is in the background, you can run any other tasks in the foreground. To get the pst of jobs, use command, jobs. It is also possible to terminate the process either with CTRL+C or kill command. You can pass the job number while using the kill command.

Check the following output which demonstrates stopping the jobs, moving the jobs from the foreground to the background and vice versa, terminating the jobs, etc.

chmod u+x basic_commands.sh
chmod u+x process_status.sh

./basic_commands.sh
Wed Jul 5 18:30:27 IST 2017
Now sleeping for 250 seconds, so that testing job control functionapty is smooth
^Z
[1]+ Stopped ./basic_commands.sh
./process_status.sh
PID   TTY   TIME     CMD
2295  pts/1 00:00:00 bash
4222  pts/1 00:00:00 basic_commands.
4224  pts/1 00:00:00 sleep
4225  pts/1 00:00:00 process_status.
4226  pts/1 00:00:00 ps
Now sleeping for 200 seconds, so that testing job control functionapty is smooth
^Z
[2]+ Stopped      ./process_status.sh
jobs
[1]- Stopped      ./basic_commands.sh
[2]+ Stopped      ./process_status.sh
fg
./process_status.sh
^Z
[2]+ Stopped      ./process_status.sh
fg %2
./process_status.sh
^Z
[2]+ Stopped      ./process_status.sh
fg %1
./basic_commands.sh
^Z
[1]+ Stopped      ./basic_commands.sh

jobs
[1]+ Stopped      ./basic_commands.sh
[2]- Stopped      ./process_status.sh

bg %2
[2]- ./process_status.sh &
fg
./basic_commands.sh
^Z
[1]+ Stopped      ./basic_commands.sh
jobs
[1]+ Stopped      ./basic_commands.sh
[2]- Running      ./process_status.sh &
fg %2
./process_status.sh
^Z
[2]+ Stopped      ./process_status.sh
jobs
[1]- Stopped      ./basic_commands.sh
[2]+ Stopped      ./process_status.sh
kill %1 %2
[1]- Stopped      ./basic_commands.sh
[2]+ Stopped      ./process_status.sh

[1]- Terminated   ./basic_commands.sh
[2]+ Terminated   ./process_status.sh
Advertisements