Dr. Niall McMahon
Lecture Notes
Table of Contents
CA644, System Software
Dr. Niall McMahon
2022-11-01
If you print these slides, think about using two pages per sheet although don't worry too much about it!
Dr. Niall McMahon
Drawing on previous work by:
Dr. Michael Scriney
Dr. Long Cheng
With help from Mark Humphrys. And sources credited in the references.
Autumn 2022.
In these notes, I draw on material in Structured Computer Organisation by Andrew S. Tanenbaum as well as C++, How to Program by Deitel and Deitel, among other sources.
gcc -E hello_world.c > hello_world.i
. The -E
flag explicitly runs the preprocessor.
gcc -S hello_world.i
. The -S
flag explicitly creates assembly code output, .s file.
gcc -c hello_world.s
. The -c
flag explicitly creates an object file, .o file.
gcc -o hello_world hello_world.c
. The -o
flag explicitly creates the executable by finding the missing functions, for example, printf()
and scanf()
from the standard C library, libc.
gcc -o hello_world hello_world.c
.
select
is used when waiting for input or output.
These notes are drawn heavily from Section 13.9, Waiting for Input or Output in the The GNU C Library manual. Other useful references include IBM's select() reference.
read
for this purpose.
read
blocks the program until input is available on one particular file descriptor; input on other channels won't interrupt it.
select
.
sys/types.h
.
accept
blocks and interacts with select
in the same way that read
does for normal input.
select
function are specified as fd_set
data type is a bit array that represents file descriptor sets for the select function. (A bit array is a simple array of bits, i.e. 1 or 0, that represent some information about another set of objects arrayed in the same order.)
fd_set
object can hold information about.
FD_SETSIZE
is at least that number.
fd_set
.
filedes
parameter must not have side effects since it is evaluated more than once. (A side effect is a change in state of a program, i.e. its variable or I/O values, caused by an expression or function call or the modification of a global variable. Unexpected side effects cause confusion and are hard to debug. The file descriptor here must have a stable value that is free from side effects.)
filedes
from the file descriptor set set.
filedes
parameter must not have side effects since it is evaluated more than once.
filedes
is a member of the file descriptor set set, and zero (false) otherwise.
filedes
parameter must not have side effects since it is evaluated more than once.
int select (int nfds, fd_set *read-fds, fd_set *write-fds, fd_set *except-fds, struct timeval *timeout)
select
function blocks the calling process until there is activity on any of the specified sets of file descriptors, or until the timeout period has expired.
read-fds
argument are checked to see if they are ready for reading; the write-fds
file descriptors are checked to see if they are ready for writing; and the except-fds
file descriptors are checked for exceptional conditions. You can pass a null pointer for any of these arguments if you are not interested in checking for that kind of condition.
read
call will not block. This usually includes the read offset being at the end of the file or there is an error to report. A server socket is considered ready for reading if there is a pending connection which can be accepted with accept; see Accepting Connections. A client socket is ready for writing when its connection is fully established.
select
function checks only the first nfds
file descriptors. The usual thing is to pass FD_SETSIZE
as the value of this argument.
struct timeval
format. Specify zero as the time (a struct timeval
containing all zeros) if you want to find out which descriptors are ready without waiting if none are ready.
select
is the total number of ready file descriptors in all of the sets. Each of the argument sets is overwritten with information about the descriptors that are ready for the corresponding operation. Thus, to see if a particular descriptor desc has input, use FD_ISSET (desc, read-fds)
after select
returns.
If select
returns because the timeout
period expires, it returns a value of zero.
select
to return immediately. So if your program uses signals, you can't rely on select
to keep waiting for the full time specified. If you want to be sure of waiting for a particular amount of time, you must check for EINTR
and repeat the select
with a newly calculated timeout based on the current time.
If an error occurs, select
returns -1 and does not modify the argument file descriptor sets. The following errno
error conditions are defined for this function:
EBADF
: one of the file descriptor sets specified an invalid file descriptor.
EINTR
: the operation was interrupted by a signal.
EINVAL
: the timeout argument is invalid; one of the components is negative or too large.