Ocaml Unix.pdf

(1304 KB) Pobierz
unix system programming in
objective caml
Xavier Leroy and Didier Rémy
April 4, 2010
© 1991, 1992, 2003, 2004, 2005, 2006, 2008, 2009, 2010
Xavier Leroy and Didier Rémy,
inria
Rocquencourt.
Rights reserved. Distributed under the Creative Commons Attribution – Non commercial –
Share alike 2.0 France license. See
http://creativecommons.org/licenses/by-nc-sa/2.0/fr/
for
the legal terms.
Translation by
Daniel C. Bünzli, Eric Cooper, Eliot Handelman, Priya Hattiangdi, Thad Meyer,
Prashanth Mundkur, Richard Paradies, Till Varoquaux, Mark Wong-VanHaren
Proofread by
David Allsopp, Erik de Castro Lopo, John Clements, Anil Madhavapeddy, Prashanth
Mundkur
Translation coordination & layout by
Daniel C. Bünzli.
Please send corrections to
daniel.buenzl i@erratique.ch
.
Abstract
This document is an introductory course on Unix system program-
ming, with an emphasis on communications between processes. The main novelty
of this work is the use of the Objective Caml language, a dialect of the ML lan-
guage, instead of the C language that is customary in systems programming.
This gives an unusual perspective on systems programming and on the ML lan-
guage.
Contents
Introduction
1
1 Generalities
3
1.1 Modules
Sys
and
Unix
3
1.2 Interface with the calling program
1.3 Error handling
5
1.4 Library functions
6
4
2 Files
9
2.1 The file system
9
2.2 File names and file descriptors
11
2.3 Meta-attributes, types and permissions
11
2.4 Operations on directories
15
2.5 Complete example: search in a file hierarchy
16
2.6 Opening a file
18
2.7 Reading and writing
20
2.8 Closing a descriptor
22
2.9 Complete example: file copy
22
2.10 The cost of system calls and buffers
23
2.11 Complete example: a small input/output library
2.12 Positioning
28
2.13 Operations specific to certain file types
29
2.14 Locks on files
32
2.15 Complete example: recursive copy of files
32
2.16 Complete example:
T
ape
AR
chive
34
3 Processes
41
3.1 Creation of processes
41
3.2 Complete Example: the command
leave
3.3 Awaiting the termination of a process
42
3.4 Launching a program
44
3.5 Complete example: a mini-shell
46
4 Signals
49
4.1 Default behavior
49
4.2 Using signals
50
4.3 Changing the effect of a signal
4.4 How to mask signals
53
4.5 Signals and system calls
53
4.6 The passage of time
55
4.7 Problems with signals
57
25
41
51
5 Classical inter-process communication: pipes
5.1 Pipes
59
59
i
Contents
5.2
5.3
5.4
5.5
5.6
5.7
Complete example: parallel sieve of Eratosthenes
Named pipes
64
Descriptor redirections
64
Complete example: composing
N
commands
67
Input/output multiplexing
68
Miscellaneous:
write
73
61
6 Modern communication: sockets
77
6.1 Sockets
78
6.2 Socket creation
79
6.3 Addresses
80
6.4 Connection to a server
81
6.5 Disconnecting sockets
81
6.6 Complete example: the universal client
6.7 Establishing a service
83
6.8 Tuning sockets
86
6.9 Complete example: the universal server
6.10 Communication in connectionless mode
6.11 Low level reads and writes
89
6.12 High-level primitives
89
6.13 Examples of protocols
90
6.14 Complete example:
http
requests
94
81
86
88
7 Threads
101
7.1 Introduction
101
7.2 Creation and termination of threads
102
7.3 Waiting
103
7.4 Synchronization among threads: locks
106
7.5 Complete example:
http
relay
108
7.6 Conditions
110
7.7 Event-based synchronous communication
112
7.8 Implementation details
115
Going further
119
121
Exercise answers
References
Index
137
135
ii
Introduction
These course notes originate from a system programming course Xavier Leroy
taught in 1994 to the first year students of the Master’s program in fundamental
and applied mathematics and computer science at the École Normale Supérieure.
This earliest version used the Caml-Light [1] language. For a Master’s course in
computer science at the École Polytechnique taught from 2003 to 2006, Didier
Rémy adapted the notes to use the OCaml language. During these years, Gilles
Roussel, Fabrice Le Fessant and Maxence Guesdon helped to teach the course
and also contributed to this document. The new version also brought additions
and updates. In ten years, some orders of magnitude have shifted by a digit
and the web has left its infancy. For instance, the
http
relay example, now
commonplace, may have been a forerunner in 1994. But, most of all, the OCaml
language gained maturity and was used to program real system applications like
Unison [18].
Tradition dictates that Unix system programming must be done in C. For
this course we found it more interesting to use a higher-level language, namely
OCaml, to explain the fundamentals of Unix system programming.
The OCaml interface to Unix system calls is more abstract. Instead of en-
coding everything in terms of integers and bit fields as in C, OCaml uses the
whole power of the ML type system to clearly represent the arguments and re-
turn values of system calls. Hence, it becomes easier to explain the semantics of
the calls instead of losing oneself explaining how the arguments and the results
have to be en/decoded. (See, for example, the presentation of the system call
wait
, page 42.)
Furthermore, due to the static type system and the clarity of its primitives, it
is safer to program in OCaml than in C. The experienced C programmer may see
these benefits as useless luxury, however they are crucial for the inexperienced
audience of this course.
A second goal of this exposition of system programming is to show OCaml
performing in a domain out of its usual applications in theorem proving, com-
pilation and symbolic computation. The outcome of the experiment is rather
positive, thanks to OCaml’s solid imperative kernel and its other novel aspects
like parametric polymorphism, higher-order functions and exceptions. It also
shows that instead of applicative and imperative programming being mutually
exclusive, their combination makes it possible to integrate in the same program
complex symbolic computations and a good interface with the operating system.
These notes assume the reader is familiar with OCaml and Unix shell com-
mands. For any question about the language, consult the Objective Caml System
documentation [2] and for questions about Unix, read section 1 of the Unix
man
ual
or introductory books on Unix like [5, 6].
This document describes only the programmatic interface to the Unix sys-
tem. It presents neither its implementation, neither its internal architecture.
The internal architecture of
bsd
4.3 is described in [8] and of System
v
in [9].
Tanenbaum’s books [13, 14] give an overall view of network and operating system
architecture.
The Unix interface presented in this document is part of the Objective Caml
System available as free software at
http://caml.inria.fr/ocaml/
.
1
Zgłoś jeśli naruszono regulamin