Chapter Review


[Page 528 (continued)]

Checklist

In this chapter, I described:

  • all of the common file management system calls

  • the system calls for duplicating, terminating, and differentiating processes

  • how a parent may wait for its children

  • the terms orphan and zombie

  • threaded processes

  • how signals may be trapped and ignored

  • the way to kill processes

  • how processes may be suspended and resumed

  • IPC mechanisms: unnamed pipes, named pipes, and sockets

  • the client/server paradigm

  • local domain and Internet domain sockets

Quiz

1.

How can you tell when you've reached the end of a file?

2.

What is a file descriptor?

3.

What's the quickest way to move to the end of a file?

4.

Why is the system call to delete a file called unlink () instead of delete ()?

5.

Describe the way that shells implement I/O redirection.

6.

What is an orphaned process?

7.

Under what circumstances do zombies accumulate?

8.

How can a parent find out how its children died?

9.

What's the difference between execv () and execvp ()?

10.

Why is the name of the system call kill () a misnomer?

11.

If a process does not handle a specific signal, what are the default reactions the process may have upon receiving that signal?

12.

How can you protect critical code?

13.

Must you be the super-user to lower your process's nice value (i.e., to raise the process's priority)?

14.

What is the purpose of process groups?


[Page 529]
15.

What happens when a process attempts to read from its controlling terminal but it is not a member of the same process group as the terminal's control process?

16.

What happens when a writer tries to overflow a pipe?

17.

How can you create a named pipe?

18.

What is meant by "network byte order"?

19.

Describe the client/server paradigm.

20.

Describe the stages that a client and a server go through to establish a connection.

Exercises

1.

Write a program to catch all signals sent to it and print out which signal was sent. Then issue a "kill -9" command to the process. How is SIGKILL different from the other signals? [level: easy]

2.

Write a program that takes a single integer argument n from the command line and creates a binary tree of processes of depth n. When the tree is created, each process should display the phrase "I am process x" and then terminate. The nodes of the process tree should be numbered according to a breadth-first traversal. For example, if the user entered this:

$ tree 4              ...build a tree of depth 4.


then the process tree would look like this:

and the output would be:

I am process 1 I am process 2 ... I am process 15 


Make sure that the original parent process does not terminate until all of its children have died. This is so that you can terminate the parent and its children from your terminal with a Control-C. [level: medium]


[Page 530]
3.

Write a program that creates a ring of three processes connected by pipes. The first process should prompt the user for a string and then send it to the second process. The second process should reverse the string and send it to the third process. The third process should convert the string to uppercase and send it back to the first process. When the first process gets the processed string, it should display it to the terminal. When this is done, all three processes should terminate. Here's an illustration of the process ring:

Here's an example of the program in action:

$ ring                             ...run the program. Please enter a string: ole Processed string is: ELO $ _ 


[level: medium]

4.

Rewrite the "ghoul" exercise of Chapter 6, "The Bourne Again Shell," using the C language. [level: medium]

5.

Write a program that uses setuid () to access a file that he/she could not normally access. [level: medium]

Projects

1.

Write a suite of programs that run in parallel and interact to play the "Paper, Scissors, Rock" game. In this game, two players secretly choose either paper, scissors, or rock. They then reveal their choice. A referee decides who wins as follows:

  • Paper beats rock (by covering it).

  • Rock beats scissors (by blunting it).

  • Scissors beats paper (by cutting it).

  • Matching choices draw.


[Page 531]

The winning player gets a point. In a draw, no points are awarded. Your program should simulate such a game, allowing the user to choose how many iterations are performed, observe the game, and see the final score. Here's an example of a game:

$ ./play 3                       ...play three iterations. Paper, Scissors, Rock: 3 iterations Player 1: ready Player 2: ready Go Players [1]  Player 1: Scissors  Player 2: Rock Player 2 wins Go Players [2]  Player 1: Paper  Player 2: Rock Player 1 wins Go Players [3]  Player 1: Paper  Player 2: Paper Players draw. Final score:  Player 1: 1  Player 2: 1 Players Draw $ _ 


You should write three programs, which operate as follows:

  1. One program is the main program, which fork/execs one referee process and two player processes. It then waits until all three terminate. It should check that the command-line parameter that specifies the number of turns is legal and pass it to the referee process as a parameter to exec ().

  2. One program is a referee program, which plays the role of the server. This program should prepare a socket and then listen for both players to send the string "READY", which means that they're ready to make a choice. It should then tell each player to make a choice by sending them both the string "GO". Their responses are then read, and their scores are calculated and updated. This process should be repeated until all of the turns have been taken, at which point the referee should send both players the string "STOP", which causes them to terminate.

  3. One program is a player program, which plays the role of the client. This program is executed twice by the main program, and should start by connecting to the referee's socket. It should then send the "READY" message. When it receives the "GO" message back from the referee, it should make a choice and send it as a string to the referee. When it receives the string "STOP", it should kill itself.


[Page 532]

These programs will almost certainly share some functions. To do a good job, create a makefile that separately compiles these common functions and links them into the executables that use them. Do not avoid sending strings by encoding them as one-byte numberssending strings is part of the exercise. [level: medium]

2.

Rewrite Exercise 1 using unnamed pipes instead of sockets. Which do you think was easier to write? Which is easier to understand? [level: medium]

3.

Rewrite Exercise 1 to allow the players to reside on different machines on the network. Each component of the game should be able to start separately. [level: hard]

This is an example of how 3 will work.

...execute this command on vanguard. $ ./referee 5000             ...use local port 5000. ...execute this command on csservr2. $ ./player vanguard.5000    ...player is on a remote port. ...execute this command on wotan. $ ./player vanguard.5000    ...player is on a remote port. 





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net