<!doctype html public "-//w3c//dtd html 4.0
transitional//en">
CS 476/576
Systems
Programming
Fall
2008
Final
Exam
Time
2 & 1/2 hours
Open
Book & Notes
Name:
Unix Login:
Each Question is 20 points.
Question
1:
Consider
the following program, called Q1.c and its corresponding code is called Q1
int main(int argc, char **argv)
{
int n, in,
out;
char buf[1024];
mode_t mode
= S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
in = open(argv[1],
O_RDONLY);
out = open(argv[2],
O_CREAT | O_WRONLY | O_APPEND, mode);
while ((n = read(in, buf, sizeof(buf)))
> 0)
write(out, buf,
n);
lseek(in,
0, SEEK_SET);
while ((n = read(3, buf, sizeof(buf)))
> 0)
write(4, buf,
n);
close(0);
close(1);
dup2(3,0);
dup2(4,1);
lseek(in,
0, SEEK_SET);
while ((n = read(0, buf, sizeof(buf)))
> 0)
write(1, buf,
n);
}
%
Q1 Q1.c Q1Out
% wc Q1.c
30
87 657 Q1.c
What is the output
of:
% wc
Q1Out
Question
2:
Consider
the following program, called Q2.c and its corresponding code is called Q2
int count = 0;
void handler(int);
int main(int argc, char *argv[])
{
char buf[BUFSIZ];
signal(SIGALRM, handler);
signal(SIGINT, handler);
alarm(2);
while (count < 2) pause();
}
void handler(int sig)
{
printf("Got
%d: ", sig);
if (sig == SIGALRM) {
signal(SIGALRM, handler);
printf("Timeout\n");
kill(getpid(),
SIGINT);
alarm(2);
}
else {
signal(SIGINT, handler);
printf("Interrupt\n");
count++;
}
}
% 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:
% Q2
Question 3:
A.
Consider the following program, called Q3.c and its corresponding code is
called Q3
int I = 0;
int
main (int argvc, char **argv)
{
printf("I
is %d\n", I++);
fork();
printf("I
is %d\n", I++);
fork();
printf("I
is %d\n", I++);
}
What is the output
of:
% Q3
B.
Consider the following program, called Q4.c and its corresponding code is
called Q4
int I
= 0;
int
main (int argvc, char **argv)
{
if (argvc > 1) exit(0);
printf("I is %d\n", I++);
if(fork()==0)
execl
("./q4","q4","1", NULL);
printf("I is %d\n", I++);
if(
fork()==0)
execl
("./q4","q4", "2",NULL);
printf("I is %d\n", I++);
}
What is the output of:
% Q4
Question 5:
Change
the following program to use TCP connection instead of pipe.
int
main(void)
{
int pfd[2];
char
line[64];
pipe(pfd);
if (fork()
== 0) {
close(pfd[0]);
dup2(pfd[1],
1);
close(pfd[1]);
execl("/bin/date",
"date", 0);
}
close(pfd[1]);
read (pfd[0], line, sizeof(line)) ;
printf("date
from child is: %s\n", line);
}