|
|
|
Modern Operating Systems by Herbert Bos and Andrew S. Tanenb...
Modern_Operating_Systems_by_Herbert_Bos_and_Andrew_S._Tanenbaum_4th_Ed.pdf
Showing 85-86 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 85
54
INTRODUCTION
CHAP. 1
Process management
Call
Description
pid = fork( )
Create a child process identical to the parent
pid = waitpid(pid, &statloc, options)
Wait for a child to terminate
s = execve(name, argv, environp)
Replace a process’ core image
exit(status)
Terminate process execution and return status
File management
Call
Description
fd = open(file, how, ...)
Open a file for reading, writing, or both
s = close(fd)
Close an open file
n = read(fd, buffer, nbytes)
Read data from a file into a buffer
n = write(fd, buffer, nbytes)
Write data from a buffer into a file
position = lseek(fd, offset, whence)
Move the file pointer
s = stat(name, &buf)
Get a file’s status information
Directory- and file-system management
Call
Description
s = mkdir(name, mode)
Create a new directory
s = rmdir(name)
Remove an empty directory
s = link(name1, name2)
Create a new entry, name2, pointing to name1
s = unlink(name)
Remove a directory entry
s = mount(special, name, flag)
Mount a file system
s = umount(special)
Unmount a file system
Miscellaneous
Call
Description
s = chdir(dirname)
Change the working directory
s = chmod(name, mode)
Change a file’s protection bits
s = kill(pid, signal)
Send a signal to a process
seconds = time(&seconds)
Get the elapsed time since Jan. 1, 1970
Figure 1-18.
Some of the major POSIX system calls. The return code
s
is
−
1 if
an error has occurred.
The return codes are as follows:
pid
is a process id,
fd
is a
file descriptor,
n
is a byte count,
position
is an offset within the file, and
seconds
is the elapsed time. The parameters are explained in the text.
In most cases, after a
fork
, the child will need to execute different code from
the parent. Consider the case of the shell.
It reads a command from the terminal,
forks off a child process, waits for the child to execute the command, and then
reads the next command when the child terminates.
To wait for the child to finish,
Page 86
SEC. 1.6
SYSTEM CALLS
55
the parent executes a
waitpid
system call, which just waits until the child terminates
(any child if more than one exists).
Waitpid
can wait for a specific child, or for any
old child by setting the first parameter to
−
1. When
waitpid
completes, the address
pointed to by the second parameter,
statloc
, will be set to the child process’ exit
status (normal or abnormal termination and exit value). Various options are also
provided, specified by the third parameter.
For example, returning immediately if
no child has already exited.
Now consider how
fork
is used by the shell. When a command is typed, the
shell forks off a new process. This child process must execute the user command.
It does this by using the
execve
system call, which causes its entire core image to
be replaced by the file named in its first parameter.
(Actually, the system call itself
is
exec
, but several library procedures call it with different parameters and slightly
different names. We will treat these as system calls here.)
A highly simplified shell
illustrating the use of
fork
,
waitpid
, and
execve
is shown in Fig. 1-19.
#define TRUE 1
while (TRUE) {
/
*
repeat forever
*
/
type
prompt( );
/
*
display prompt on the screen
*
/
read
command(command, parameters);
/
*
read input from terminal
*
/
if (fork( ) != 0) {
/
*
fork off child process
*
/
/
*
Parent code.
*
/
waitpid(
−
1, &status, 0);
/
*
wait for child to exit
*
/
} else {
/
*
Child code.
*
/
execve(command, parameters, 0);
/
*
execute command
*
/
}
}
Figure 1-19.
A stripped-down shell. Throughout this book,
TRUE
is assumed to
be defined as 1.
In the most general case,
execve
has three parameters: the name of the file to
be executed, a pointer to the argument array, and a pointer to the environment
array. These will be described shortly.
Various library routines, including
execl
,
execv
,
execle
, and
execve
, are provided to allow the parameters to be omitted or
specified in various ways. Throughout this book we will use the name
exec
to
represent the system call invoked by all of these.
Let us consider the case of a command such as
cp file1 file2
used to copy
file1
to
file2
.
After the shell has forked, the child process locates and
executes the file
cp
and passes to it the names of the source and target files.
Ace your assessments! Get Better Grades
Browse thousands of Study Materials & Solutions from your Favorite Schools
Concordia University
Concordia_University
School:
Operating_Systems
Course:
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
lab 18.docx
lab_18.docx
Course
Course
3
Module5QuizSTA2023.d...
Module5QuizSTA2023.docx.docx
Course
Course
10
Week 7 Test Math302....
Week_7_Test_Math302.docx.docx
Course
Course
30
Chapter 1 Assigment ...
Chapter_1_Assigment_Questions.docx.docx
Course
Course
5
Week 4 tests.docx.do...
Week_4_tests.docx.docx
Course
Course
23
Week 6 tests.docx.do...
Week_6_tests.docx.docx
Course
Course
106