<!doctype html public
"-//w3c//dtd html 4.0 transitional//en">
CS 476/576
Systems Programming
Fall 2012
Final Exam
Time 2 & 1/2 hours
Open Book & Notes
Name:
Unix Login:
Question
1: 15 points
Consider the following program:
int main(int argc, char **argv)
{
int n, in;
char buf[1024];
system ("rm Q1File");
system ("echo Q1File > Q1File");
in = open("Q1File", O_RDONLY) ;
close(0);
dup(3);
n = read(0, buf, sizeof(buf));
write(1, buf, n);
write(2, buf, n);
}
What is the
output of the following?
% Q1
Question
2: 15 points
Consider the following program:
int main(void)
{
int i;
for (i = 1; i
< 50; i++) {
signal(i, handler) ;
sleep(1);
kill(getpid(), i);
}
}
void handler(int
sig)
{
psignal(sig);
}
% 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
Question
3: 20 points
Consider the
following program:
int main()
{
int pip[2];
FILE
*fp0;
FILE
*fp1;
char buffer[100];
pipe(pip);
fp0 = fdopen(3,
"r");
fp1 = fdopen(4,
"w");
fprintf(fp1,
"%s", "HI");
fclose(fp1);
fscanf(fp0,
"%s\n", buffer);
fprintf(stdout, buffer);
fprintf(stderr, buffer);
printf("\n");
}
What is the
output of the following?
% Q3
Question 4: 20
points
The
following java program creates a swing interface for 10 counting threads and two
bottoms: start and suspend.
Modify
the program to add a third button to
resume all
threads counting. Show your
modifications in the provided spaces:
// <applet code=Q4 width=200
height=250></applet>
public class Q4 extends JApplet
{
private CounterThread[] s;
private JButton startB = new JButton("Start");
private JButton suspendB = new JButton("Suspend");
class CounterThread extends Thread
{
private JTextField t = new JTextField(10);
private int count = 0;
public CounterThread() {
JPanel p = new JPanel();
p.add(t);
getContentPane().add(p);
}
public synchronized void funInterrupt()
{
interrupt();
}
public void run() {
while (true) {
t.setText(Integer.toString(count++));
synchronized (this) {
try {
sleep(500);
} catch(InterruptedException e) {
try{
wait();
} catch(InterruptedException x) { }
} }
}
}
}
class StartL implements ActionListener
{
public void actionPerformed(ActionEvent e) {
startB.setBackground(Color.red);
startB.setEnabled(false);
for (int i
= 0; i < s.length; i++) s[i].start();
suspendB.setBackground(Color.green);
suspendB.setEnabled(true);
}
}
class SuspendL implements ActionListener {
public void actionPerformed(ActionEvent e) {
suspendB.setBackground(Color.red);
suspendB.setEnabled(false);
for (int i
= 0; i < s.length; i++) s[i].funInterrupt();
}
}
public void init() {
Container cp
= getContentPane();
cp.setLayout(new
FlowLayout());
s = new CounterThread[10];
for (int i
= 0; i < s.length; i++)
s[i] = new CounterThread();
startB.addActionListener(new StartL());
startB.setBackground(Color.green);
startB.setEnabled(true);
cp.add(startB);
suspendB.addActionListener(new SuspendL());
suspendB.setBackground(Color.red);
suspendB.setEnabled(false);
cp.add(suspendB);
}
public static void main(String[] args)
{
Q4 applet =
new Q4();
}
}
Question 5: 30 points
In
the following the child sends the date to the parent via a pipe.
Rewrite
this program either in C or Java so
that the two entities (processes or threads) communicate via a tcp socket instead of pipe.
main()
{
int pid, pfd[2];
char line[100];
pipe(pfd);
pid = fork();
if (pid == 0) {
close(pfd[0]);
dup2(pfd[1], 1);
close(pfd[1]);
execl("/bin/date",
"date", 0);
}
else {
close(pfd[1]);
read (pfd[0], line, sizeof(line));
printf("date from
child is: %s\n", line);
close(pfd[0]);
waitpid(NULL);
}
}