Modern Operating Systems by Herbert Bos ...
Modern_Operating_Systems_by_Herbert_Bos_and_Andrew_S._Tanenbaum_4th_Ed.pdf-M ODERN O PERATING S YSTEMS
Showing 769-770 out of 1137
Modern Operating Systems by Herbert Bos and Andrew...
Modern_Operating_Systems_by_Herbert_Bos_and_Andrew_S._Tanenbaum_4th_Ed.pdf-M ODERN O PERATING S YSTEMS
Modern Operating Systems by Herbert...
Modern_Operating_Systems_by_Herbert_Bos_and_Andrew_S._Tanenbaum_4th_Ed.pdf-M ODERN O PERATING S YSTEMS
Page 769
738
CASE STUDY 1: UNIX, LINUX, AND ANDROID
CHAP. 10
while (TRUE) {
/
*
repeat forever /
*
/
type
prompt( );
/
*
display prompt on the screen
*
/
read
command(command, params);
/
*
read input line from keyboard
*
/
pid = fork( );
/
*
fork off a child process
*
/
if (pid < 0) {
printf("Unable to fork0);
/
*
error condition
*
/
continue;
/
*
repeat the loop
*
/
}
if (pid != 0) {
waitpid (
1, &status, 0);
/
*
parent waits for child
*
/
} else {
execve(command, params, 0);
/
*
child does the work
*
/
}
}
Figure 10-7.
A highly simplified shell.
to the two-character string ‘‘cp’’. Similarly,
argv
[1] would point to the five-charac-
ter string ‘‘file1’’ and
argv
[2] would point to the five-character string ‘‘file2’’.
The third parameter of
main
,
envp
, is a pointer to the environment, an array of
strings containing assignments of the form
name = value
used to pass information
such as the terminal type and home directory name to a program.
In Fig. 10-7, no
environment is passed to the child, so that the third parameter of
execve
is a zero in
this case.
If
exec
seems complicated, do not despair; it is the most complex system call.
All the rest are much simpler.
As an example of a simple one, consider
exit
, which
processes should use when they are finished executing. It has one parameter, the
exit status (0 to 255), which is returned to the parent in the variable
status
of the
waitpid
system call. The low-order byte of
status
contains the termination status,
with 0 being normal termination and the other values being various error condi-
tions. The high-order byte contains the child’s exit status (0 to 255), as specified in
the child’s call to
exit
.
For example, if a parent process executes the statement
n = waitpid(
1, &status, 0);
it will be suspended until some child process terminates.
If the child exits with,
say, 4 as the parameter to
exit
, the parent will be awakened with
n
set to the child’s
PID and
status
set to 0x0400 (0x as a prefix means hexadecimal in C).
The low-
order byte of
status
relates to signals; the next one is the value the child returned in
its call to
exit
.
If a process exits and its parent has not yet waited for it, the process enters a
kind of suspended animation called the
zombie state
—the living dead.
When the
parent finally waits for it, the process terminates.


Page 770
SEC. 10.3
PROCESSES IN LINUX
739
Several system calls relate to signals, which are used in a variety of ways. For
example, if a user accidentally tells a text editor to display the entire contents of a
very long file, and then realizes the error, some way is needed to interrupt the edi-
tor. The usual choice is for the user to hit some special key (e.g., DEL or CTRL-
C), which sends a signal to the editor. The editor catches the signal and stops the
print-out.
To announce its willingness to catch this (or any other) signal, the process can
use the
sigaction
system call. The first parameter is the signal to be caught (see
Fig. 10-5).
The second is a pointer to a structure giving a pointer to the signal-han-
dling procedure, as well as some other bits and flags. The third one points to a
structure where the system returns information about signal handling currently in
effect, in case it must be restored later.
The signal handler may run for as long as it wants to.
In practice, though, sig-
nal handlers are usually fairly short.
When the signal-handling procedure is done,
it returns to the point from which it was interrupted.
The
sigaction
system call can also be used to cause a signal to be ignored, or to
restore the default action, which is killing the process.
Hitting the DEL key is not the only way to send a signal. The
kill
system call
allows a process to signal another related process. The choice of the name ‘‘kill’’
for this system call is not an especially good one, since most processes send signals
to other ones with the intention that they be caught. However, a signal that is not
caught, does, indeed, kill the recipient.
For many real-time applications, a process needs to be interrupted after a spe-
cific time interval to do something, such as to retransmit a potentially lost packet
over an unreliable communication line.
To handle this situation, the
alarm
system
call has been provided. The parameter specifies an interval, in seconds, after which
a SIGALRM signal is sent to the process.
A process may have only one alarm out-
standing at any instant. If an
alarm
call is made with a parameter of 10 seconds,
and then 3 seconds later another
alarm
call is made with a parameter of 20 sec-
onds, only one signal will be generated, 20 seconds after the second call. The first
signal is canceled by the second call to
alarm
.
If the parameter to
alarm
is zero,
any pending alarm signal is canceled.
If an alarm signal is not caught, the default
action is taken and the signaled process is killed. Technically, alarm signals may be
ignored, but that is a pointless thing to do.
Why would a program ask to be sig-
naled later on and then ignore the signal?
It sometimes occurs that a process has nothing to do until a signal arrives. For
example, consider a computer-aided instruction program that is testing reading
speed and comprehension.
It displays some text on the screen and then calls
alarm
to signal it after 30 seconds. While the student is reading the text, the program has
nothing to do.
It could sit in a tight loop doing nothing, but that would waste CPU
time that a background process or other user might need.
A better solution is to
use the
pause
system call, which tells Linux to suspend the process until the next
signal arrives. Woe be it to the program that calls
pause
with no alarm pending.


Ace your assessments! Get Better Grades
Browse thousands of Study Materials & Solutions from your Favorite Schools
Concordia University
Concordia_University
School:
Operating_Systems
Course:
Great resource for chem class. Had all the past labs and assignments
Leland P.
Santa Clara University
Introducing Study Plan
Using AI Tools to Help you understand and remember your course concepts better and faster than any other resource.
Find the best videos to learn every concept in that course from Youtube and Tiktok without searching.
Save All Relavent Videos & Materials and access anytime and anywhere
Prepare Smart and Guarantee better grades

Students also viewed documents