CS 476/576
Systems Programming
Fall 1999
Midterm Exam
Time 2 & 1/2 hours
Open Book & Notes


Question 1: (20 points)

Consider the following subdirectory structure:
 

~wahab
|
Q1
|
_____________
|     |      |      |     |
D1 D2 D3 D4 Dn
|     |      |      |     |
f    f     f     f     f


Directory Q1 has n subdirectories: D1, D2, Dn, where n is an integer number > 1.
Each Di, i=1, ...,n has exactly one file called f
 

Part 1 (7 points): Write a shell script to construct the above tree, starting from Q1.
The script has one argument: n, the number of D subdirectories.
 Solution

Part 2 (6 points):Explain what might happens if some one did the following:
 

% cd ~wahab/Q1
% mv D?/* .
Solution: Since all files under Ds have the same name (f), only one file
will be copied.

Part 3 (7 points):Write a shell script to convert the above structure to be as follows:
 

~wahab
|
Q1
|
____________________
|         |         |          |         |
D1f   D2f    D3f    D4f    Dnf
This means that under Q1 there are n files named: D1f, D2f, ... Dnf and there are no other subdirectories.
Solution
Question 2: (20 points)

Write an Xlib program with the following description:

SendMsg <Msg> <Disp> <Timeout>

The program creates a window on Display <Disp > and writes <Msg> centered inside the window. The of size of the window is (3W)x(3H) , where W is the width and H is the height of the message <Msg>

* If the user on display <Disp> clicks the right (left) button, the program writes the message "agrees" ("refuses") to stdout and terminates.

* If the user on display <Disp> did not click any button for a time period equals <Timeout>, the program writes the message "no response" to stdout and terminates.

NOTE: In order to make the program short , please do NOT write any include, comments or error statements.
Solution

Question 3: (20 points)

Repeat Question 2 but use a motif label widget to display the message <Msg>

NOTE: Here again, please do NOT write any include, comments or error statements.
Solution

Question 4: (20 points)

Write a program that creates two children Processes C1 and C2 and a Pipe P and then terminates. C1 waits until it gets a signal (USR1) from C2 then writes the message:"hi my little brother" to the pipe P and terminates.
C2 reads the message from P and writes it to the stdout then terminates.
Solution

Question 5: (20 points)

What is the output of the following program:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

main()
{

int fd1, fd2;
system ("touch infile; rm infile");
system ("touch outfile; rm outfile; touch outfile");
system("echo A1B2D3E4L5W6A7H8A9B > infile;);

fd1=open("infile", O_RDONLY);
fd2=open("outfile",O_WRONLY);

dup2(fd1,0);
dup2(fd2,1);

inout(fd1,fd2);
inout(0,1);

if (fork() ==0){

inout(fd1,fd2);
inout(0,1);
}

inout(fd1,fd2);
inout(0,1);

system("cat outfile>/dev/tty");

}

inout(in, out)
int in, out;
{

char buf[1];
read(in, buf, 2);
write(out, buf, 2);
}

Solution: A1B2D3E4L5W6A7H8A1B2D3E4L5W6A7H8