Pipes Exercise

From COMP15212 Wiki
Revision as of 10:22, 9 August 2019 by gravatar W81054ch [userbureaucratinterface-adminsysopPHRhYmxlIGNsYXNzPSJ0d3BvcHVwIj48dHI+PHRkIGNsYXNzPSJ0d3BvcHVwLWVudHJ5dGl0bGUiPkdyb3Vwczo8L3RkPjx0ZD51c2VyPGJyIC8+YnVyZWF1Y3JhdDxiciAvPmludGVyZmFjZS1hZG1pbjxiciAvPnN5c29wPGJyIC8+PC90ZD48L3RyPjwvdGFibGU+] (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
On path: Exercises 0: Exercises • 1: Pointer Exercise • 2: Arguments Exercise • 3: Malloc Exercise • 4: Structs Exercise • 5: Processes Exercise • 6: Shared memory Exercise • 7: Pipes Exercise • 8: Exceptions Exercise • 9: Synchronisation Exercise • 10: Files Exercise • 11: Threads Exercise • 12: Unix proc Exercise
Depends on Pipes

Download exercise files

The purpose of this exercise is:

  • to demonstrate inter-process communication through a ‘pipe

Before trying this demonstration you should be happy with the basic fork() function. This program splits into two processes having set up a pipe for them to communicate with. One process then sends some information (a string, in this case) and the other reads it and prints it out.

Compile and run pipe.c.

Before the fork() call the call to pipe() creates the communications pipe. A descriptor is returned (in a small array pipefd) with values identifying the output and input ends of this pipe.

After the fork the processes follow different branches of the if ... else ....

The first branch pauses for a second – just for demonstration purposes – then sends a string (‘message’) using the write() call to the new pipe input end (‘1’).

The second branch immediately tries to read() from the output (‘0’) end of the pipe; it will wait until there is something there. The input is directed to a ‘buffer’ and has been limited to 20 bytes in this case to avoid a buffer overflow. If the input arrives successfully this process prints it out.

Caveat: if you try a longer string, remember that the “%s” in printf() will expect a string terminator.


Exercise

Part 1

If you’ve followed that you should be able to send a reply from the child process to the parent. Unix pipes are unidirectional so you’ll need to create another on to pass data in the other direction.

Try it.

Note that the data in the pipe is an unstructured stream of bytes. Experiment with sending a second (short! – or increase the receiver buffer size) string: if you do this before the read() is rescheduled then the data will be concatenated. (This may be hard to see in a print-out because, of course, the first string terminator is still present; check the number of bytes moved.) If the read() runs before a second write() then another read() call will be needed to read everything out.

When using such a communications mechanism there may be the need for the application to impose some control protocol to structure and give meaning to the bytes. Play!

Part 2

Go back to the main pipes page and work through the section labelled “Named pipes” within a shell window.