



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Operating Systems--[CS-604] Lecture No. 12
UNIX/Linux manual pages for fg, bg, jobs, and kill commands Chapter 5 of the textbook Lecture 12 on Virtual TV
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)
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
There are two main issues with processes:
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
Thread
Process Terminated
f
f
F
main() { … thread(t1,f1); … thread(t2,f2); … } f1(…) { … } f2(…) { … }
main (^) t1 t
Process Address Space
PC
PC PC