Homework #1

Assigned: January 23
Due: January 30, 3:00pm

  1. (16 points) Write a C (not C++) program /home/<logname>/homeworks/hw1/words.c to read a stream of text from standard input until EOF and write to standard output only the total number of words read and the average number of words per line, in that order, where a word is defined as any string of characters except whitespace. For instance,
    $ hostname
    $ ./a.out < /etc/mime.types
    1132 2.28

    Keep your program to approximately 30 lines of code.

  2. (16 points) Write a C (not C++) program /home/<logname>/homeworks/hw1/operating.c which spawns and synchronizes 20 processes to print the following to stderr (of course, with different process and parent process ids):
    PPID: 310, PID: 497, operating
    PPID: 497, PID: 498, systems
    PPID: 498, PID: 499, operating
    PPID: 499, PID: 500, systems
    PPID: 500, PID: 501, operating
    PPID: 501, PID: 502, systems
    PPID: 502, PID: 503, operating
    PPID: 503, PID: 504, systems
    PPID: 504, PID: 505, operating
    PPID: 505, PID: 506, systems
    PPID: 506, PID: 507, operating
    PPID: 507, PID: 508, systems
    PPID: 508, PID: 509, operating
    PPID: 509, PID: 510, systems
    PPID: 510, PID: 511, operating
    PPID: 511, PID: 512, systems
    PPID: 512, PID: 513, operating
    PPID: 513, PID: 514, systems
    PPID: 514, PID: 515, operating
    PPID: 515, PID: 516, systems

    The first process must print operating to stderr and its child must print systems to stderr, then the child of that child must print operating to stderr and its child must print systems to stderr, and so on.

    No fancy C library functions or system calls, beyond what has been covered in class, is necessary for this program. Do not use any C constructs not presented in class unless you have discussed their use in your approach with the instructor first. Do not use the system call wait in your program because it is unecessary and, of course, sleep cannot be used to synchronize the processes because it does not guarantee the order in which the processes will run.

    Keep your program to approximately 25 lines of code.

Return Home