ECE344 - Operating Systems

Assignment 2: Processes

Due Date: Sunday, February 4, 2001, 11:59:59pm.


Objectives

In this lab, you are to write the following nine C programs and experiment with them. The key is to understand why they behave the way they do. The purpose of this lab is to understand how concurrent processes can be created and how they behave: With this knowledge, you are then to write a handy little tool that can determine how long it takes to obtain service from specified Web sites. You can use this utility, for example, to see how long it takes to reach Web sites located in different parts of the world, or which of n mirror sites of is most responsive.


Preparation

For programs 1-5: Do "man 2 fork" and "man 2 wait".
For programs 6-8: Do "man 3t pthreads" and "man 3t pthread_create".
For program 9: Do "man 3 intro" focusing on those parts that describe safety and reentrency issues.


Specification

  1. Write a minimal program, prog1.c that has 2 processes that are created with fork. One process should print out the string ``hi'' 10 times at the rate of one per second, while the other should print out the string ``ho'' 10 times at the rate of one per second. (Use the sleep system call to wait for one second between outputs.) Run this program and observe and understand its behavior.

  2. Copy prog1.c to prog2.c and modify it so all the ``ho''s are printed out first. In doing this, you are not to remove or add or change the sleep statements. (Hint: contemplate the sequence of execution relative to the fork call.)

  3. Copy prog1.c to prog3.c and modify it so all the ``hi''s are printed out first. In doing this, you are not to remove or add or change the sleep statements. (Do not simply reorder the printf statements.)

  4. Copy prog1.c to prog4.c and modify it so that the one process that output the ``ho''s, instead first reads in from the standard input (i.e., terminal) a string, stores it in a global variable called msg, and then prints out that message 10 times at a rate of one per second. Experiment with this program, waiting various lengths of time before inputing the message.

  5. Copy prog4.c to prog5.c and modify it so the global variable msg is initialized to ``hi,'' and the process that prints out ``hi'' instead of printing out a constant, prints out the value of the msg variable. (Note that this ``hi'' process is not to read anything into msg -- only the ``ho'' process should do that. Experiment with this program, waiting various lengths of time before inputing the message.

  6. Copy prog1.c to prog6.c. Modify prog6.c to use the Posix thread library to create two processes. (Do "man pthread_create"). One process should print out the "hi"'s, the other the "ho"'s. That is, the behavior should be similar to that of prog1.c, but implemented using Posix threads. However, there should only be one for-loop that outputs texts and sleeps in each iteration. That loop should be in a procedure that is called from each thread. The procedure can decide whether to print out ``hi'' or ``ho'' either based on a suitable argument that might be passed in when it starts executing, or based on its own Id, which it can query. Also, whatever variable you are using to index through the for-loop (for the output), should be declared local to the procedure containing the for-loop.

     
  7. Copy prog6.c to prog7.c. Make the variable you use to index the output for-loop global; that is, remove the declaration of the loop index from the procedure and instead declare it globally. Experiment with this program. When you run it, something goes wrong. What exactly?

  8. Copy prog6.c to prog8.c. Modify it so that it behaves like prog4.c. Experiment with prog8 as you did with prog4.c.

  9. For prog9, you are to implement a tool that periodically pings n Web sites, measures the latency to reach them and get a response, and displays the sites and the latencies obtained. Each ping is to be done by its own thread so that you can have multiple pings going on concurrently at the same time. Also, you will find that by using threads, the programming will be much simplified.

  10. There are several components to this program:

    Link ping.o  to your program so that you can call the ping routines. You will also need to link to a number of libraries: "-lpthread -lsocket -lnsl". The interfaces are defined as follows:

    int ping_host_by_name( char * hostname, int *errnop )
    int ping_host_by_addr( char * hostaddr, int *errnop )
     

    Both functions return -1 if unsuccessful (in which case the cause of error is identified in *errnop), and 0 otherwise. You can also look at the sources of those routines in ping.c  if you wish. (To understand the source code, make extensive use of man.) Note that this code will only work on Web hosts and not any Internet host.

    To see how the program could behave, download the SPARC executable multiping and run it. To find which site of a set of mirror sites is the most responsive, find an internet site with 5 to 10 mirror sites around the world and type in either the site host names (e.g. www.toronto.edu) or the site IP addresses (128.100.132.30), or just type in any name you wish."

    Optionally, you could make this program behave like the Unix top command, where the hosts are sorted by their (latency) distance.

IMPORTANT: Your program 9 MUST sleep for 10 seconds between pinging hosts. You also MUST clean up copies of your program that you suspend: do a "man ps" and a "man kill" if you are unfamiliar with the proper commands. Let's try not to crash any major websites this year! (The entire ugsparc network was banned from accessing Microsoft websites after we crashed one of their servers two years ago...)


Optional

Some computationally intensive programs take days, weeks, and even years to complete; (for example, computing the largest Mersenne Primes). As far as the user is concerned, these programs just sit there and appear to be doing nothing, although they are quietly computing away. To show forward progress, these programs sometimes periodically print out a dot or some other information. For prog10.c you are to do better, by writing a program with two threads:


Submission

In your home directory, create a directory called ece344; create a subdirectory called as2 and then in that directory create files with your code. You should only submit source files. To submit the assignment you should use the submission procedure described in the policies section.