Operating Systems Lecture 12: Process Management and Threads, Study notes of Operating Systems

An overview of process management commands in unix/linux operating systems, specifically fg, bg, jobs, and kill commands, as well as the concept of threads. How to move processes between the foreground and background, display job statuses, suspend and terminate processes, and the advantages and disadvantages of using threads.

Typology: Study notes

2011/2012

Uploaded on 11/06/2012

ahsen
ahsen 🇵🇰

4.6

(88)

84 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
65
Operating Systems--[CS-604] Lecture No. 12
Operating Systems
Lecture No. 12
Reading Material
UNIX/Linux manual pages for fg, bg, jobs, and kill commands
Chapter 5 of the textbook
Lecture 12 on Virtual TV
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
pf3
pf4
pf5

Partial preview of the text

Download Operating Systems Lecture 12: Process Management and Threads and more Study notes Operating Systems in PDF only on Docsity!

Operating Systems--[CS-604] Lecture No. 12

Operating Systems

Lecture No. 12

Reading Material

ƒ UNIX/Linux manual pages for fg, bg, jobs, and kill commands ƒ Chapter 5 of the textbook ƒ Lecture 12 on Virtual TV

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
  2. SIGTRAP 6) SIGABRT 7) SIGEMT 8) SIGFPE
  3. SIGKILL 10) SIGBUS 11) SIGSEGV 12) SIGSYS
  4. SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGUSR ... $

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.

Figure 12.1 Single- and multi-threaded processes

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.

Figure 12.2 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.

Figure 12.3 Code structure of a multi-threaded process

main()

f1(…);

f2(…);

f1(…)

f2(…)

Thread

Process Terminated

f

f

F

main() { … thread(t1,f1); … thread(t2,f2); … } f1(…) { … } f2(…) { … }

main (^) t1 t

Process Address Space

PC

PC PC