CS 476/576 Systems Programming Open Book & Notes

advertisement
CS 476/576
Systems Programming
Fall 2013
Final Exam
Time 2 & 1/2 hours
Open Book & Notes
Name:
Unix Login:
Question 1: 15 points
Consider the following program:
Q1.c
main(int argc, char **argv)
{
int n, in, out;
char buf[1024];
system ("rm Q1File1 Q1File2");
system ("echo 'This is a TEST' > Q1File1");
in = open("Q1File1", O_RDONLY) ;
out = open("Q1File2", O_CREAT | O_WRONLY | O_APPEND, 0777);
while ((n = read(3, buf, sizeof(buf))) > 0){
printf("1:");
fflush(stdout);
write(1, buf, n);
printf("2:");
fflush(stdout);
write(2, buf, n);
write(4, buf, n);
write(out, buf, n);
}
printf("\nQ1File2:\n");
system ("cat Q1File2");
}
What is the output of the following?
% Q1
My answer:
1: This is a TEST
2: This is a TEST
Q1File2:
This is a TEST
This is a TEST
2
Question 2: 15 points
Consider the following program:
Q2.c
main(void)
{
int pid;
signal(SIGINT, SIG_IGN);
signal(SIGCHLD, sig_handler);
if ( (pid = fork())== 0 ) {
signal (SIGINT, sig_handler);
kill (getppid(), SIGINT);
pause();
exit(0);
}
sleep(1);
kill (pid, SIGINT);
pause();
exit(1);
}
void sig_handler(int sig)
{
if (sig == 2)
printf ("Received INT\n");
if (sig == 18)
printf ("Received CLD\n");
}
% kill –l
HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM
USR1 USR2 CLD PWR
WINCH URG POLL STOP TSTP CONT TTIN TTOU VTALRM PROF XCPU XFSZ WAITING
LWP FREEZE THAW
CANCEL LOST RTMIN RTMIN+1 RTMIN+2 RTMIN+3 RTMAX-3 RTMAX-2 RTMAX-1
RTMAX
What is the output of the following?
% Q2
My answer:
Received INT
3
Question 3: 20 points
Consider the following program:
Q3.c
main(void)
{
pid_t
pid;
int
pfd[2];
pipe(pfd);
pid = fork();
if (pid == 0) {
dup2(pfd[1], 1);
close(pfd[1]);
close(pfd[0]);
execl("/bin/cat", "cat", 0);
}
else {
dup2(pfd[0], 0);
close(pfd[0]);
close(pfd[1]);
execl("/bin/cat", "cat", 0);
}
}
What is the output of the following?
% head -3 Q3.c | Q3
My answer:
The first three lines of the file ‘Q3.c’
4
Question 4: 20 points
The following java program create a swing interface (shown in the left ) composing of two
Boxes with Labels: Female and Male.
The program generates 10 Buttons labeled 0-9. For each button the program randomly selects its
sex and places the Females inside the left Box and the Males inside the right Box.
Modify the program to change the interface (shown in the right) such that:
1. Creates a Text field T at the bottom.
2. When a button Female ( or Male) i is pressed, T displays the message: “Female ( or
Male) i is active”
Then the button is disabled and its background changes to red.
Show your modifications in the provided spaces:
Q4.java
public class Q4 extends JApplet {
JButton[] but = new JButton[10];
// What I add:
5
JTextField
txt = new JTextField(30);
public void init() {
// What I add:
ActionListener FAL = new ActionListener() {
public void actionPerformed(ActionEvent e){
JButton b = (JButton)e.getSource();
String name = b.getText();
txt.setText("Female: "+ name+ " is active");
b.setBackground(Color.red);
b.setEnabled(false);
}
};
ActionListener MAL = new ActionListener() {
public void actionPerformed(ActionEvent e){
JButton b = (JButton)e.getSource();
String name = b.getText();
txt.setText("Male: "+ name+ " is active");
b.setBackground(Color.red);
b.setEnabled(false);
}
};
////////////////////////////////
Box fb = Box.createVerticalBox();
Box mb = Box.createVerticalBox();
fb.add(new JLabel("Female"));
fb.add(Box.createVerticalStrut(10));
mb.add(new JLabel("Male"));
mb.add(Box.createVerticalStrut(10));
for(int i = 0; i < 10; i++) {
int j = (int) (Math.random() * 2) ;
if (j==0) {
but[i] = new JButton("" + i);
////////////////////////////////
but[i].addActionListener(FAL);
fb.add(but[i]);
fb.add(Box.createVerticalStrut(10));
} else {
but[i] = new JButton("" + i);
6
but[i].addActionListener(MAL);
mb.add(but[i]);
mb.add(Box.createVerticalStrut(10));
}
}
7
Container cp = getContentPane();
cp.setBackground(Color.yellow);
cp.add(BorderLayout.WEST, fb);
cp.add(BorderLayout.EAST, mb);
cp.add(BorderLayout.SOUTH, txt);
}
public static void main(String[] args) {
Console.run(new Q4(), 200, 400);
}
} ///:~
8
Question 5: 30 points
Write either java or C server program that accepts connections from two clients at port
10123. When both clients are connected, the program sends any message received from one
client to the other.
Here is my code:
import java.io.*;
import java.net.*;
class ServeOneEcho extends Thread {
private Socket socket1;
private Socket socket2;
private BufferedReader in;
private PrintWriter out;
public ServeOneEcho(Socket s1, Socket s2)
throws IOException {
socket1 = s1;
socket2 = s2;
in = new BufferedReader( new
InputStreamReader( socket1.getInputStream()));
out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(
socket2.getOutputStream())), true);
start();
}
public void run() {
try {
while (true) {
String str = in.readLine();
if (str==null) break;
out.println(str);
}
System.out.println("closing...");
} catch(IOException e) {
System.err.println("IO Exception");
} finally {
try {
socket1.close();
socket2.close();
} catch(IOException e) {
System.err.println("Socket not closed");
}
}
}
}
public class Q5 {
public static void main(String[] args)
9
throws IOException {
ServerSocket s = new ServerSocket(10123);
System.out.println("Server Started: " + s);
try {
while(true) {
Socket socket1 = s.accept();
System.out.println("Accepted Client 1: " + socket1);
Socket socket2 = s.accept();
System.out.println("Accepted Client 2: " + socket2);
try {
new ServeOneEcho (socket1, socket2);
new ServeOneEcho (socket2, socket1);
} catch(IOException e) {
socket1.close();
socket2.close();
}
}
} finally {
s.close();
}
}
}
10
11
Download