Related Content: CS604 - VU Lectures, Handouts, PPT Slides, Assignments, Quizzes, Papers & Books of Operating Systems
In the last lecture, we started discussing a few UNIX/Linux process management command. In particular, we discussed the ps and top commands. We now discuss the fg, bg, jobs, and kill commands and <Ctrl-Z> and <Ctrl-C> key presses.
You can use the fg command to resume the execution of a suspended job in the foreground or move a background job into the foreground. Here is the syntax of the command.
fg [%job_id]
where, job_id is the job ID (not process ID) of the suspended or background process. If %job_id is omitted, the current job is assumed.
You can use the bg command to put the current or a suspended process into the background. Here is the syntax of the command.
bg [%job_id]
If %job_id is omitted the current job is assumed.
You can use the jobs command to display the status of suspended and background processes.
You can suspend a foreground process by pressing <Ctrl-Z>, which sends a
STOP/SUSPEND signal to the process. The shell displays a message saying that the job
has been suspended and displays its prompt. You can then manipulate the state of this job, put it in the background with the bg command, run some other commands, and then
eventually bring the job back into the foreground with the fg command.
The following session shows the use of the above commands. The <Ctrl-Z> command is used to suspend the find command and the bg command puts it in the background. We then use the jobs command to display the status of jobs (i.e., the background or suspended processes). In our case, the only job is the find command that we explicitly put in the background with the <Ctrl-Z> and bg commands.
$ find / -name foo -print 2> /dev/null
^Z
[1]+ Stopped find / -name foo -print 2> /dev/null
$ bg
[1]+ find / -name foo -print 2> /dev/null &
$ jobs
[1]+ Running find / -name foo -print 2> /dev/null &
$ fg
find / -name foo -print 2> /dev/null
[ command output ]
$
You can terminate a foreground process by pressing <Ctrl-C>. Recall that this key press sends the SIGINT signal to the process and the default action is termination of the process. Of course, if your foreground process intercepts SIGINT and ignores it, you cannot terminate it with <Ctrl-C>. In the following session, we terminate the find command with <Ctrl-C>.
$ find / -name foo -print 1> out 2> /dev/null
^C
$
You can also terminate a process with the kill command. When executed, this command sends a signal to the process whose process ID is specified in the command line. Here is the syntax of the command.
kill [-signal] PID
where, ‘signal’ is the signal number and PID is the process ID of the process to whom the
specified signal is to be sent. For example, kill –2 1234 command sends signal
number 2 (which is also called SIGINT) to the process with ID 1234. The default action
for a signal is termination of the process identified in the command line. When executed
without a signal number, the command sends the SIGTERM signal to the process. A
process that has been coded to intercept and ignore a signal, can be terminated by sending
it the ‘sure kill’ signal, SIGKILL, whose signal number is 9, as in kill –9 1234.
You can display all of the signals supported by your system, along with their
numbers, by using the kill –l command. On some systems, the signal numbers are
not displayed. Here is a sample run of the command on Solaris 2.
$ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5) SIGTRAP 6) SIGABRT 7) SIGEMT 8) SIGFPE
9) SIGKILL 10) SIGBUS 11) SIGSEGV 12) SIGSYS
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGUSR1
...
$
There are two main issues with processes:
These problems can be overcome by using threads.
A thread, sometimes called a lightweight process (LWP), is a basic unit of CPU
utilization and executes within the address space of the process that creates it. It
comprises a thread ID, a program counter, a register set, errno, and a stack. It shares with
other threads belonging to the same process its code sections, data section, current
working directory, user and group IDs, signal setup and handlers, PCB and other
operating system resources, such as open files and system. A traditional (heavy weight)
process has a single thread of control. If a process has multiple threads of control, it can
do more than one task at a time. Figure 12.1 shows processes with single and multiple
threads. Note that, as stated above, threads within a process share code, data, and open
files, and have their own register sets and stacks.
In Figure 12.2, we show the code structure for a sequential (single-threaded) process and how the control thread moves from the main function to the f1 function and back, and from f1 to main and back. The important point to note here is that there is just one thread of control that moves around between various functions.
In Figure 12.3, we show the code structure for a multi-threaded process and how multiple threads of control are active simultaneously. We use hypothetical function thread() to create a thread. This function takes two arguments: the name of a function for which a thread has to be created and a variable in which the ID of the newly created thread is to be stored. The important point to note here is that multiple threads of control are simultaneously active within the same process; each thread steered by its own PC.
Four main advantages of threads are:
Some of the main disadvantages of threads are: