Spread Knowledge

CS604 - Operating Systems - Lecture Handout 12

User Rating:  / 0
PoorBest 

Related Content: CS604 - VU Lectures, Handouts, PPT Slides, Assignments, Quizzes, Papers & Books of Operating Systems

Summary

  • Process Management commands and key presses: fg, bg, jobs, and kill commands and <Ctrl-Z> and <Ctrl-C> command presses
  • Thread Concept (thread, states, attributes, etc)

Process Management commands

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.

Moving a process into foreground

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.

Moving a process into background

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.

Displaying status of jobs (background and suspended processes)

You can use the jobs command to display the status of suspended and background processes.

Suspending a process

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 ]
$

Terminating a process

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
...
$

The Thread Concept

There are two main issues with processes:

  1. The fork() system call is expensive (it requires memory to memory copy of the executable image of the calling process and allocation of kernel resources to the child process)
  2. An inter-process communication channel (IPC) is required to pass information between a parent process and its children 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.

The Thread Concept

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.

Code structure of a single-threaded (sequential) process

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.

Code structure of a multi-threaded process

The Advantages and Disadvantages of Threads

Four main advantages of threads are:

  1. Responsiveness: Multithreading an interactive application may allow a program to continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user.
  2. Resource sharing: By default, threads share the memory and the resources of the process to which they belong. Code sharing allows an application to have several different threads of activity all within the same address space.
  3. Economy: Allocating memory and resources for process creation is costly.
    Alternatively, because threads share resources of the process to which they belong, it is more economical to create and context switch threads.
  4. Utilization of multiprocessor architectures: The benefits of multithreading of multithreading can be greatly increased in a multiprocessor environment, where each thread may be running in parallel on a different processor. A single threaded process can only run on one CPU no matter how many are available.
    Multithreading on multi-CPU machines increases concurrency.

Some of the main disadvantages of threads are:

  1. Resource sharing: Whereas resource sharing is one of the major advantages of threads, it is also a disadvantage because proper synchronization is needed between threads for accessing the shared resources (e.g., data and files).
  2. Difficult programming model: It is difficult to write, debug, and maintain multithreaded programs for an average user. This is particularly true when it comes to writing code for synchronized access to shared resources.