Docsity

Programming Assignment #1 - Introduction to Operating Systems | EECS 678, Assignments of Operating Systems

Toggle sidebar
bg1
EECS 678: Introduction to Operating Systems
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.
Information on Sockets
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
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Programming Assignment #1 - Introduction to Operating Systems | EECS 678 and more Assignments Operating Systems in PDF only on Docsity!

EECS 678: Introduction to Operating Systems

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.

Information on Sockets

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.

Stage I: Minimal Client-Server

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.

  1. In writing your code, make sure to check for an error returned from all system calls. If there is an error, the system declared global variable, errno, will give you information about the type of error that occurred:

#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.

  1. Most of you will be running both the client and server on the same machine (start the server in the background and then start the client). In this case, the time value returned to the client by the server is likely to be the same as that which the client found in its own call to gettimeofday(). Thus, it might be a little more interesting to have the client waste some time (e.g., looping and counting, or sleeping) between its own call to gettimeofday() and when it sends the returned time stamp to the server process.
  2. Make sure you close every socket (file descriptor) that you use in your program. If you abort your program, the socket may still hang around and the next time you try and bind a new socket to the port number you previously used (but never closed), you may get an error. Also, please be aware that port numbers, when bound to sockets, are system-wide values and thus other students may be using the port number you are trying to use. With this in mind, we suggest you use the last four digits of your KUID and add it to N × 10 , 000 to create your port number, where N is an integer. The reason for the large number is that port numbers below 5000 are reserved for system use, and those between 5,000–10,000 are used by lots of local applications. If you follow this rule the likelihood of colliding with another program using the same port at the same time is very low.

Stage II: Remote String Converting Server

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[]) {

/*

  • The first argument to fgets is the address where it should put the
  • characters it reads. The name of an array is the address of its first
  • element (i.e., the element with index zero). / if (fgets (string_buffer, BUFFER_SIZE, stdin) == NULL) { / This probably means end-of-file (EOF). In any case, we won’t be getting any more input from stdin, so go ahead and exit. / break; } /
  • Convert the string */ convert_string (string_buffer);

}

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.

What to Hand In

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:

  1. Server program,
  2. Client program,
  3. Makefile to build the client and server programs.
  4. README file (see coding instructions!)

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 Policy for Programs

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...