Вы находитесь на странице: 1из 2

CSCE 313 Introduction to Computer Systems Fall 2011

Homework 1 (25 points)

This homework assignment requires the course textbook and the Programs located on the book
website: http://usp.cs.utsa.edu/usp/.

1. Download the programs for Chapter 3. You’ll find a Program 3.1 simplechain.c and its
executable simplechain in the tar file. Run simplechain three times with a parameter of 4.
Turn in the output of these three runs. Explain why the output appears intermingled.

2. For each of the three runs, what is the chain of process IDs created? Eg. System Process
5117 creates process 10152 corresponding to the program simplechain. When i=1,
process 10152 (the original parent) creates child process 10153, breaks the loop, executes
the fprintf statement and terminates. When i=2, process 10152 becomes a parent and
creates process 10153, breaks, executes the fprintf statement and terminates. When i=3,
10153 becomes a parent and creates 10154, breaks, executes the fprintf statement and
terminates. Process 10154 finds that i=4 and terminates the for loop then it executes the
fprintf statement and terminates.

Did you ever see a strange parent process ID printed? If a child becomes an orphan what
is the process ID of its parent? What is the name of this process in a Unix system?

3. Edit the simplechain.c file and insert a #include <sys/wait.h> statement and insert a call
to wait(NULL) statement before the fprintf statement. Run make using the makefile
statements below. Then run the resulting program simplechaintest with a parameter of 4.
Turn in the output. Explain why the output appears as it does.

makefile:
CC = cc
COPS =
LINT = lint
LOPS = -x -u
all: simplechaintest
simplechaintest: simplechain.c
$(CC) $(COPS) -o simplechaintest simplechain.c
lintall: lint
lint:
$(LINT) $(LOPS) simplechaintest.c
clean:
rm -f *.o core simplechaintest

4. In the programs for Chapter 3, you’ll also find Program 3.2 simplefan.c and its
executable simplefan in the tar file. Run simplefan three times with a parameter of 4.
Turn in the output of these three runs. For each of the three runs, what is the chain of
process IDs created? Why is the childpid=0 on each of the runs?

1
5. Download the programs for Chapter 12. Run make, which builds all the executables. Run
the program badparameters. Read the code in badparameters.c and describe how the
threads must have been scheduled based on the output of your run (be sure and turn in
your run results along with your explanation).

6. In order to fix the code problem, how would you suggest re-coding the reference to i
(&i)? {You don’t have to make the change, but tell how you would fix the problem.}

7. Look at the two if statements in the last for loop (use the code you downloaded since the
textbook has a typo in the code). What does the continue statement do if the first if
statement is true? Write what you think would be a good “// comment” for both if
statements in the loop.

Вам также может понравиться