讲解CSE3100、辅导C/C++程序语言、辅导virtual machine、C/C++设计讲解

- 首页 >> C/C++编程

CSE3100 - System Programming Apr 06, 2018

Midterm 2

This is a 90 minutes exam. Please read every question carefully before answering

and express your answers as completely as you can.

Materials allowed include books, lecture slides, notes, and printed materials.

You can access (read, copy and reuse) your code from homework, lab, and the

demo code. You can use your own laptop.

During the exam, you can only access your virtual machine (VM) provided by

UITS, the lab computer assigned to you, your own laptop, and the git server

(cse3100.engr.uconn.edu). You cannot access any other computers.

The software you can use include PDF viewers, text editors, ssh clients, git,

and packages available on your VM. Particularly, you are not allowed to open

any Internet browsers (with or without GUI), FTP clients, messaging applications,

file sharing/synchronization application (like Dropbox, Onedrive,

Google Drive, etc.).

There are three questions on the exam. Two of them (Q2 and Q3) must be

done on your UITS provided VM in your git repository. Start by running the

usual git pull to retrieve the source handout. Do not forget to commit

and push your changes to the folder mt2.

I pledge my honor that I have not violated and will not violate the exam policy of this course

and the Student Conduct Code during this examination.

Signature: Date:

Printed Name: NetID:

Section:

Question Max Score

1 10

2 30

3 60

Total 100

1

1. Understanding C (10 points).

Consider the following C code. The numbers at the beginning of each line are line

numbers. Assume the size of an integer (of int type) is 4 bytes.

1 int a [300][100];

2 char c [] = " 1234567 " ;

3 int x [100];

4

5 int r = sizeof ( c ) == strlen ( c ) ;

6

7 // initialize all elements in x to 0

8 for ( int i = 0; i <= sizeof ( x ) ; i ++)

9 x [ i ] = 0;

Fill the blanks in questions (a), (b), and (c) with decimal numbers.

(a) sizeof(a) is .

(b) If a[0][0] is located at address 1000, &a[10][20] is .

(c) After the assignment on line 5, r is .

(d) Identify and fix the bug(s) in the for loop (lines 8 and 9).

2

2. Socket (30 points)

Read the entire problem first and plan accordingly.

In this problem, you will implement a server program that communicates with a

client program via the Internet sockets. The protocol is described below.

You are *NOT* required to implement the client program.

Protocol.

The server listens on port 8025 and communicates with the client program over the

TCP (byte stream) transport layer IPv4 protocol.

The client sends one integer at a time to the server and, in response, the server sends

back the sum of all the integers it has received from the client so far. The server uses

an integer of int type to keep track of the sum. There is no need to consider overflow.

The messages exchanged between the client and server are lines. Every line must end

with an LF('\n'). A line can have at most 30 characters, excluding LF.

A session starts with a client being connected to the server. Note that when the server

sends the sum to the client, the line is a string that denotes the sum in decimal. For

example, if the sum is 3100, the server sends "3100\n".

1. Once a client is connected to the server, the server sets the sum to 0 and replies

with the sum ("0\n").

2. Client sends a line to the server. The line should start with an integer. White

spaces before the integer are acceptable. For example, both "40\n" and " 40x13\n"

specify the value 40. Note that the input may contain other characters. Implementation

note: You can rely on sscanf() function to determine if a valid integer

is at the beginning of a line.

3. The server reads a line from the client, and do the following.

i. If reading the socket results in an error, the server disconnects.

ii. If the line is too long, the server replies with two lines: "Error LL\n", followed

by the current sum.

iii. If the line is "exit\n", the server replies with the current sum and disconnects.

iv. The server then tries to read an integer from the line. If it fails, the server

replies with two lines: "Error NaN\n", followed by the current sum.

v. If an integer is read from the line, the server adds it to the sum, and replies

with the current sum.

4. The client waits for the reply from the server.

5. If the client wants to continue, it goes back to Step 2.

6. The client sends "exit\n" to the server, and disconnects.

3

Deliverables. You should submit a makefile and a C file, namely, server.c for the

server. Both files should be located in the ex2 directory under mt2. The compiled

executable for the server should be server.

A makefile and a simple template is provided. You can use the code from examples or

programs you have worked on before. Some test vectors are provided.

Again, you are NOT required to implement a client program.

Implementation.

Your server should listen for inbound requests to TCP port 8025 on localhost. Your

server should be able to communicate with multiple clients at the same time. Upon

reception of a request from a client, the server spawns a child process responsible

for working with the client. Note that you do not promote the child into another

executable. The parent process should collect the dead processes regularly.

The server uses sscanf() to read an integer from the line it receives. The return value

of sscanf() indicates if a valid integer is present in the line.

Since you do NOT implement the client program, use nc localhost 8025 command

to test your server.

A sample session is shown below. The first command starts the server.

$./server &

$nc localhost 8025

0

100

100

abc

Error NaN

100

Error LL

100

exit

100

Tips.

You may want to focus on dealing with correct input first.

Parsing lines should not take much time. Use sscanf() and string library functions.

Run the server program in a separate terminal if it is faster for you to terminate and

restart it.

Use ps ux command to make sure all child processes terminate properly.

4

3. Process and pipe (60 points)

This problem has two parts. You should implement Part A first and then extend it to

implement Part B. However, read the entire problem first and plan accordingly. You

CANNOT use pipe2() in your implementation.

Provided Code. The template provided in this problem, under the ex3 directory, has

two programs, master and kid. You will be working on master.c. kid is a program

that you will use to test master. It reads from stdin, processes the input, and writes

to stdout. Do not change the kid program. You may inspect kid.c, but do not have to.

Problem Description. The master program starts one (in case of Part A) or two

programs (in case of Part B) specified at the command line. It creates two pipes for

each program it starts. master sends input to the child program by writing to a pipe

and reads the output of the child program from the other pipe. From the program’s

point of view, it uses stdin and stdout as usual. Note that even if two programs are

started (in case of Part B), they do not communicate with each other. They only

communicate with master through pipes.

After starting the programs, master enters a loop, in which it does the following.

1. Read a line from stdin. The line ends with an LF('\n').

2. Send the line to the programs it has started by writing to proper pipes. If two

programs are started (Part B), it writes the same line to two pipes.

3. Read the result from the programs. The result is one line that ends with an LF.

If two programs are started (Part B), it reads from two pipes.

4. Print the result from the programs to stdout. If two programs are started (Part

B), two lines are printed.

master exits from the loop if a line is not read properly (e.g., on EOF).

Although master can work with many programs, it will be tested with kid only.

(a) Part A. Supporting one program (50 points)

Implement the functions in master.c so that it can start and work with one kid.

A sample session is listed below. The process ID changes for different sessions.

When you press Ctrl+D, your program should exit and you can get back to shell.

$./master ./kid

Started child 3695

./kid starting ...

aB cD EF g

Child 0( 3695): Ab Cd ef G

5

(b) Part B. Supporting two programs (10 points)

Copy your master.c to master2.c. Improve master2.c so it can start and work

with two kids. Change Makefile, if necessary, so master2.c can be compiled to

master2 by typing make. An important task in Part B is to close pipes properly

in three processes.

Note that you may implement part B in master.c. In that case, you do not need

to implement a separate program called master2.c.

A sample session is listed below. The process ID changes for different sessions.

Please pay attention to --, which separates two commands.

$./master2 ./kid -- ./kid u

Started child 3705

Started child 3706

./kid starting ...

./kid starting ...

aB cD EF g

Child 0( 3705): Ab Cd ef G

Child 1( 3706): AB CD EF G


站长地图