CS 476/576 Systems Programming Open Book & Notes

advertisement
CS 476/576
Systems Programming
Fall 2003
Midterm Exam
Time 2 & 1/2 hours
Open Book & Notes
Name:
Login:
2
Question 1: (30 points)
Write a shell program, called findstring with following syntax:
findstring <string>
The program looks at each file under the current directory (it does not search
recursively under any subdirectories) to see if it contains the <string>.
Try as best as you can to make your script output similar to the output displayed by
my solution as depicted from the following examples.
Examples:
> findstring "pid = fork"
looking for "pid = fork" in files under "/home/cs476/public_html/fall03/midterm/.wahab"
"pid = fork" is in "fileshare.c"
"pid = fork" is in "sigfork.c"
"pid = fork" is in "sigshare.c"
"pid = fork" is in "3" files under "/home/cs476/public_html/fall03/midterm/.wahab"
> findstring "pid=fork"
looking for "pid=fork" in files under "/home/cs476/public_html/fall03/midterm/.wahab"
"pid=fork" is not in any file under "/home/cs476/public_html/fall03/midterm/.wahab"
> findstring include
looking for "include" in files under "/home/cs476/public_html/fall03/midterm/.wahab"
"include" is in "fileshare.c"
"include" is in "sigexec.c"
"include" is in "sigfork.c"
"include" is in "sigshare.c"
"include" is in "4" files under "/home/cs476/public_html/fall03/midterm/.wahab"
> findstring inclde
looking for "inclde" in files under "/home/cs476/public_html/fall03/midterm/.wahab"
"inclde" is not in any file under "/home/cs476/public_html/fall03/midterm/.wahab"
3
Solution (Q1)
4
Question 2: (30 points)
Write a Motif/Xlib program to create a interface similar to the one shown below.
The interface has a drawing area and three buttons: Draw, Erase and Quit.
 When the user clicks on the Draw button, a circle and two cross lines are
drawn in the drawing area as shown in the figure.
 When the user clicks on the Erase button the drawing area is cleared.
 When the user clicks on the Quit button the program exits.
5
Solution (Q2)
6
Question 3: (20 points)
What is the output of the following program?
main (int argc, char *argv[])
{
FILE *fp;
pid_t pid;
char buf[1024];
int fd, b=4, c=0, d=0, e=0, f=0, n, g, status;
fd = open ( argv[1], O_RDWR | O_CREAT , 0666 );
fp = fdopen(fd, "w+");
pid = fork();
if (pid == 0) {
write(fd, &fd, sizeof(int));
write(fd, &b, sizeof(int));
sleep(1);
lseek(fd, 0, SEEK_SET );
read(fd, &f, sizeof(int));
printf("%d, %d, ", e, f);
}
else {
lseek ( fd, 0, SEEK_SET );
read(fd, &c, sizeof(int));
read(fd, &d, sizeof(int));
e = c + d;
lseek ( fd, 0, SEEK_SET );
write(fd, &e, sizeof(int));
wait(&status);
lseek ( fd, 0, SEEK_SET );
while ((n = read (fd, &g, sizeof(int))) > 0)
printf("%d, ", g);
}
printf("DONE\n");
}
Solution (Q3):
7
Question 4: (20 points)
What is the output of the following program?
main (int argc, char *argv[])
{
FILE *fp;
char buf[1024];
int b=4, c=0, d=0, e=0, f=0, n, g, status, fd;
pid_t pid;
signal(SIGUSR1, handler);
signal(SIGCHLD, handler);
fd = open ( argv[1], O_RDWR | O_CREAT , 0666 );
fp = fdopen(fd, "w+");
pid = fork();
if (pid == 0) {
execl ("./sigexec", "sigsharexec", NULL);
}
else {
pause();
lseek ( fd, 0, SEEK_SET );
read(fd, &c, sizeof(int));
read(fd, &d, sizeof(int));
e = c + d;
lseek ( fd, 0, SEEK_SET );
write(fd, &e, sizeof(int));
kill (pid, SIGUSR2);
pause();
lseek ( fd, 0, SEEK_SET );
while ((n = read (fd, &g, sizeof(int))) > 0)
printf("parent: %d\n", g);
kill (pid, SIGUSR2);
wait(&status);
printf("DONE.\n");
}
}
void handler(int sig)
{
printf("I am parent: ");
fflush(stdout);
psignal(sig, "Received signal");
signal(sig, handler);
}
The content of sigexec is:
main (int argc, char *argv[])
{
char buf[1024];
int fd=3, b=4, c=0, d=0, e=0, f=0, n, g;
signal(SIGUSR2, handler);
write(fd, &fd, sizeof(int));
write(fd, &b, sizeof(int));
kill (getppid(), SIGUSR1);
pause();
lseek(fd, 0, SEEK_SET );
read(fd, &f, sizeof(int));
printf("child: %d, %d\n", e, f);
sleep(1);
kill (getppid(), SIGUSR1);
pause();
}
void handler(int sig)
{
printf("I am child: ");
fflush(stdout);
psignal(sig, "Received signal");
signal(sig, handler);
}
Solution (Q4):
Download