CS 4328 Lab 1 Since some of you haven't used unix before, I'm

advertisement
CS 4328 Lab 1
Since some of you haven't used unix before, I'm assigning this lab
as a "starter" lab for programming and using system calls in a
unix environment.
This is a multipart lab designed to ensure you understand how to do the
following:
a. Write simple c programs
b. Understand the concept of "standard input', 'standard output',
and how to use the shell to pipe programs together that use stdin,
stdout for their input/output.
c. Understand the unix system calls pipe, fork, dup, exec and how
they are used at the programming level to perform interprocess
communication.
It is intended that you will use your Linux machines to implement the lab but you may
use any unix machine anywhere.
To edit a file: use the traditional unix editors 'emacs' or 'vi'. Use man to learn
about the 5 or 6 vi commands you will need to get by in vi or get any book on Unix
utilities for the vi commands. I don't care which editor you use.
1. Write three small c programs. The first program "grade_count" should read in
grades from stdin. grade_count.c should compute the number of different grades
(>= 90, >=80, >=70, etc) and then write the numbers to stdout. The program
terminates when an EOF is encountered.
For example, if the following is input to grade_count:
90, 87, 76, 54, 91, 88, 13, 43
then grade_count produces the output:
90
80
70
60
50
40
30
20
10
0
2
2
1
0
1
1
0
0
1
0
Note: an EOF is usually 'sent' to a process by hitting a CTRL D. If you type stty -an on
your unix command line, you can get info that tells you which keyboard keys mean what.
FYI, in c to put values to standard_out use printf(). To get values from standard in use
scanf() or getchar().
The second program "average" reads in a sequence of numbers in the form of the
above grade_count output from stdin and computes the average and then prints it to
stdout. Assume there are no more than 100 numbers and the sequence is read until an
EOF is encountered.
To compile and link a program in unix type:
cc -o prog prog.c where prog.c is a c program.
The " -o prog " tells the compiler that the output file is the executable file prog.
To run the program just type
prog or (./prog ) to the shell.
The third program will take a UNIX command and fork() a child to execute it (use
execv()). The parent process simply waits for the termination of the child process. The
process id of the parent and the child should be printed out.
(Need to wait until I talk about pipes on parts 2 and 3. )
2. Use the programs written in part 1 together with the shell concepts of pipe (and
redirection) to solve problem 2: From a text file on disk, read in grades and compute the
average. Do not rewrite the programs from part
2.1. Use the very same programs but use a shell pipe for them to cooperate
and solve this different problem.
3. Write a c program that uses the unix system calls pipe, fork, dup to solve the very same
problem as problem 2. This time, do not use the shell pipe, use the pipe system call.
Your c program should set up a child-TO-grandchild pipe; the child should 'exec' to
perform a "grade_count" process and its output should be connected to the pipe
connected to the grandchild, which should exec to perform a "average" process. The
parent process should somehow indirectly wait for all children/grandchildren to
terminate.
Turn in copies of
a. All three little programs.
b. What the shell command is to solve step 2.
c. The c program that uses system calls to do step 3.
Show a sample execution of each program.
Use any text file you wish to test your answers. Redirect your input and output to/from
parts 2 and 3 and turn in copies of those files also.
Due by Jan. 31, 2003. Submit all your files electronically to the grader Zhilan Zhou at
zz1003@swt.edu. In the title of your email, put “Proj (#) for CS 4328 submitted by (Your
Name)”. You should write a readme file telling the grader how to run your programs.
Helpful c hints:
to read a character from stdin use getchar() e.g
while ( ( c = getchar() ) != EOF ) {
...
}
is a standard way to read a stream of chars ( one at a time in the
char variable c ) from the keyboard(stdin).
To read numbers from the keyboard use scanf() instead:
while ( scanf("%d", &x ) > 0 ) sum = sum+ x;
is a loop that reads integers from the keyboard, adds them to sum and
exits from the loop when an EOF is reached.
Use printf to output values to stdout:
printf("%d", sum) prints the value of sum to stdout.
Download