Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 5: Locking and Interprocess Communication


either writes the required machine code instructions to execute the system call directly onto the
stack, or uses some glue that is available in userspace.^15 This routine is responsible for restoring
the process context so that the application can continue to run when the next switch is made to
user mode.

Figure 5-10 illustrates the chronological flow and the various switches between user and kernel mode
during signal handler execution.

Kernel

User Handler
Function

Continue program
execution

do_signal

handle_signal

sys_sigreturn

Figure 5-10: Signal handler execution.

5.4.2 Pipes and Sockets


Pipes and sockets are popular interprocess communication mechanisms. I provide only an overview of
how both concepts work because both make intense use of other kernel subsystems. Pipes use objects of
the virtual filesystem, while sockets use various network functions and also the virtual filesystem.

Shell users are familiar with pipes in command lines such as

wolfgang@meitner>prog | ghostscript | lpr -

which use the output of one process as input for another process, the pipe being responsible for data
transport. As the name suggests, apipeis a connection used to exchange data. One process feeds data
into one end of the pipe, and another takes the data out at the other end for further processing. Several
processes can be joined together by a series of individual pipes.

When pipes are generated via the shell, there is always one read and one write process. Applications
must invoke thepipesystem call to generate pipes. The call returns two file descriptors — one for the
read end and one for the write end of the pipe. Because both descriptors exist in the same process, the
process can initially only send messages to itself, and this is not very practical.

Pipes exist as a data object in process address space — and are retained when a process is duplicated
withforkorclone. It is this very characteristic that programs exploit. Once theexecsystem call has
replaced the child process with another program, there is a communication link between two different
applications (the pipe descriptors must be directed to standard input and output or thedupsystem call
must be invoked to ensure that the file descriptors are not closed whenexecis called).

Sockets are also objects that return a file descriptor when initialized in the kernel and can then be handled
as normal files. However, unlike pipes, they can be used bidirectionally and to communicate with remote

(^15) On IA-32 machines, this glue code can, for instance, be found in thevsyscallpage, which is mapped into every user address
space. It assists the C standard library in finding the fastest way to perform system calls on a given machine by providing the
required machine code instructions. The kernel decides at boot time which method is used best, and maps the page into the address
space of each userland process. The page also includes the required code to execute the aforementionedsigreturnsystem call.

Free download pdf