Previous Contents Next


The three proposed exercises manipulate file descriptors, processes, respectively pipes and signals. The first two exercises stem from Unix system programming. The Objective CAML code can be compared with the C code in the Unix or Linux distributions.

Counting Words: the wc Command

We want to (re)program the Unix wc command, which counts the number of lines, words or characters contained in a text file. Words are separated by a space character, a tab, or a carriage return. We do not count the separators.
  1. Write a first version (wc1) of the command, which only handles a single file. The name of the file is passed as an argument on the command line.
  2. Write a more elaborated version (wc2), which can handle the three options -l, -c, -w as well as several file names. The options indicate if we want to count the number of lines, characters or words. The output of each result shall be preceded by the name of the file.

Pipes for Spell Checking

This exercise uses pipes to concatenate a suite of actions. Each action takes the result of the preceding action as argument. The communication is realized by pipes, connecting the output of one process to the input of the following, in the style of the Unix command line symbol | .

  1. Write a function pipe_two_progs of type string * string list -> string * string list -> unit such that pipe_two_progs (p1,[a1; ...; an]) (p2,[b1; ...; bp]) starts the programs p1 a1 ... an and p2 b1 ... bp, redirecting the standard output of p1 to the standard input of p2. ai and bi are the command line arguments of each program.

  2. We revisit the spell checker function from the exercise on page ?? to write a first program. Modify it so that the list of faulty words is sent without treatment in the form of one line per word to the standard output.

  3. The second program takes a sequence of character strings from its standard input and sorts it in lexicographical order. The function Sort.list can be used, which sorts a list in an order defined by a given predicate. The sorted list is written to the standard output.

  4. Test the function pipe_two_progs with the two programs.

  5. Write a function pipe_n_progs to connect a list of programs.

  6. Write a program to suppress multiple occurrences of elements in a list.

  7. Test the function pipe_n_progs with these three programs.

Interactive Trace

In a complex calculation it may be useful to interact with the program to verify the progression. For this purpose we revisit the exercise on page ?? on the computation of prime numbers contained in an interval.
  1. Modify the program so that a global variable result always contains the last prime number found.

  2. Write a function sigint_handle which handles the signal sigint and writes the content of result to the output.

  3. Modify the default signal handling of sigint by associating with it the preceding function sigint_handle.

  4. Compile the program, then start the executable with an upper bound for the computation time. During the computation, send the signal sigint to the process, by the Unix kill command as well as by the key combination CTRL-C.

Previous Contents Next