






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Programming Assignment 1: Sockets, Pipes, Processes and File Descriptors Puzzle
In this assignment you will use UNIX sockets to communicate between two processes, which may (but need not) be running on separate machines. The assignment is constructed in two stages: the first makes you establish a socket between two processes and use it to exchange information. The second requires you to establish additional communication between the server and a process which is its child using pipes, and to properly connect all the file descriptors so the data will flow through the intended path. Remember, though, that the point of the assignment is not what the code does. What the code does is dumb. The point of the assignment is that the intended structure is a puzzle where the pieces are processes, pipes, sockets, and file descriptors. Ideally, this assignment should be easy if you think carefully as you go through all the operations step by step and concentrate on how the processes, sockets, pipes and file descriptors work. It should be difficult or impossible if you are trying to follow instructions or examples without thinking about how the pieces work and fit together.
The basic socket calls, the role they play in the client and the server, and the order in which they are called was covered in class. However, this document will summarize the information to give you a starting place. Also, study the example code provided with the project, as well as the tutorial documents about sockets, IPC, and process control related system calls. However, while these documents will help, there is no substitute for reading the manual pages describing each system call. They are easily found using the man command or âM-x manual-entryâ under xemacs. The first important point is that the socket interface assumes that the client-server relation- ship holds between two processes. One is the server which waits for requests at a fixed port address. The fixed address is the equivalent of waiting at a known phone number. The client requests service from the server by calling its number from anywhere it likes. The telephone analogy is not perfect, though, since the processes involved also execute system calls which are the equivalent of building and destroying the telephones, creating additional extensions, as well as assigning the telephonesâ numbers! The first system call of concern is the socket call, which creates a file descriptor attached to a socket structure. This creates the communication point in the process which calls it. Both the client and the server call socket. Next, the server uses the bind call to establish its port number, and listen to define the number of pending calls which may wait for attention from the server. The server then calls accept when it wishes to accept a request for service. Note that accept returns a new connected socket (file descriptor), independent of the original socket created by the server. The new socket provides the connection to the client making the request which has just been accepted. The reason is that this leaves the original socket undisturbed, and available
for use by the server to accept future requests for service by other clients. This is important when the server uses fork to create a child process to serve this request since the parent and child processes have to be careful about which file descriptors they use, and which they close. After the client creates its socket using the socket call, it needs to make a connection to the server. The client can first bind its socket to a specific port number, although it need not do so. Whether it picks its port number or not, it requests a connection to the server using the connect call. When the connect call returns successfully the client and server are in communication. Each process reads and writes using the socket file descriptors as they would read and write file descriptors associated with files. When finished, the client and server discard the socket file descriptors issuing the close system call. The example code illustrates this in greater detail, but to summarize with compact pseudo- code:
Server Client
fd = socket() fd = socket() bind(fd, server address) [bind(fd, optional explicit client address)] listen(fd, queue-length)
nfd = accept(fd, client-address) connect(fd, server-address);
read(nfd) requests write(fd) requests write(nfd) answers read(fd) answers
close(nfd) close(fd) close(fd)
Note that the example code is substantially longer than this pseudo-code because the return values of system calls are checked, and comments have been included.
You are given two programs, a server program (process) and a client program (process); the programs should be compiled separately, so that they each have their own executable image. The two processes will communicate with each other in a connection-oriented manner using sockets. This stage of development will ensure that you have the basic socket parts of the puzzle working before you progress on to the child process and the pipes. The client process should first connect() to the server process (after creating the socket, of course). The client process should then call the gettimeofday() system call and print out the
endianness of the supporting machines is all the same, you should use the htonl (host- to-network long) and ntohl (network to host long) macros to insure that all machines interpret the byte orderings in the same manner.
#include <errno.h> ...... if ( (sockid = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("error creating client socket, error%d\n",errno); perror(ââmeaning:ââ); exit(0); }
See the manual pages errno(2) and perror(2) for a description of the error codes and the use of perror.
This is the project code that you will write up and hand in for evaluation, unless you have difficulties so bad that you have to hand in Stage I. It is an extension of Stage I, in that it uses the same client-server framework to organize its operation, but it differs in that the work
int main(int argc, char *argv[]) {
/*
}
Figure 1: Code Fragment from conv.c
it does is somewhat more complicated. In many real network-based servers, rather than having the server itself handle the incoming requests (e.g., to return a time of day, as above), the server process will fork() another process (a worker process), which will be the process using the socket returned by accept for communication back to the client process. The role of the server then becomes one of simply accepting connections from clients, creating child processes to service each request, and then awaiting completion of the worker processes. In this project we will have the server process maintain communication with the client process, but have it fork off a subordinate process to do some of the work associated with each request. We do this to make the file descriptor and process puzzle more educational. The basic structure of the project is thus a client process requesting service from the server process, which forks a child process to do some of the work. The server creates pipes before forking as conduits of communication to the child process. One pipe will support communication from the parent to the child (P2C), and one from the child to the parent (C2P). The child process should then close file descriptors it will not use (both sockets and the unused ends of each pipe), and manipulate the remaining pipe descriptors using dup2 to attach them to the file descriptor entries in the process open file table for standard in (0) and standard out (1). The reason for this is that the child process then uses exec to become a process running the conv code which knows nothing of pipes, and simply reads standard in and standard out.
You will be handing in a document describing your project, as well as the source code of the project. You should create a small document describing your project. This document should begin with approximately two pages describing the overall program design, a verbal description of âhow it worksâ including the basics of what the system is doing underneath, and design tradeoffs considered and made. Also describe possible improvements and extensions to your program (and sketch how they might be made). The next section should describe the tests you ran on your program to convince yourself that it is indeed correct. Also describe any cases for which your program is known not to work correctly. The appendix to this document should include the source code for your final client and server. The source code of the programs you hand in should work correctly and be documented. Note that the in-line documentation of your program is worth 15% of your grade. Take this seriously. When the TA reads your code the comments in it should form a clear, informative, and well structured narrative. Occasional cryptic comments will not be viewed favorably. You should package your source code into a TAR file using the instructions provided on the class web-page. The TAR file should consist of four (4) files:
Mail the TAR file along with the project description document in PDF (preferred) or DOC format to [email protected] before class on Thursday, February 19, 2009. You should also print out your project description, write your Name and KU ID on the report and hand it in to me on the day of the class. You may wish to leave the description in my mailbox before the deadline by handing it to the EECS front-desk.
Grading
Design Document 20% Thoroughness of test cases 05% Program works correctly 50% in-line documentation 15% quality of design 10%
Projects submitted late by one class period will be accepted with a deduction of 10% of your grade. I will not accept projects that are later than one class period past the stated submission date. START EARLY. THIS IS HARDER THAN IT LOOKS!
DataChunk a, b; ... readLineAsChunk( 0 , & a); ... write( socketFd , & a , sizeof( DataChunk ) ); ... read( socketFd , & b , sizeof( DataChunk ) ); ... writeChunk( 1 , & b );
Figure 2: Code Fragment exemplifying DataChunk operations
For a DataChunk that is the size of the fixed-array plus the four bytes for the size-tracking integer. You will not be able to read and write the entire structure to the pipes like you did with the socket. Note that the conv program does not expect DataChunk sâit expects to read a file and write a file. Consider how well this matches the usage of readLineAsChunk and writeChunk to read from file and write to a file...