<!doctype html public "-//w3c//dtd html 4.0 transitional//en">

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

 

 

 

Name:   

                          
Login:

 

A total of  five questions, each question is 20 points

 

Ø     Do not use networked computer.

Ø     To make it easy to read your answer, please do NOT write:

·        include statements

·        error checking statements

·        comment statements


Question 1:

 

Write a shell script that have the following description:

RunAndMail <cmd>

The program runs the  <cmd>   then  email  to the user the message:

You have run:

 <cmd>

Example:

% RunAndMail   ls   -lt

 

If the user is cs476, then he will get the following email message:

 

From cs476@cs.odu.edu  Sat Oct 14 00:45:01 2006

Date: Sat, 14 Oct 2006 00:45:00 -0400 (EDT)

From: cs476 grader <cs476@cs.odu.edu>

To: cs476@cs.odu.edu

 

You have run:

ls -lt

Ansewer:

Question 2:

Write a shell script  with following description:

getnames  <file>

where the <file> is a file  contains a list of emails.

The program produces another file called:  <file>_Names that maps each email to one name.

Example:

% cat    FacultyEmails

wahab

maly

ajay

 

% getnames   FacultyEmails

% cat   FacultyEmails_Names

wahab :          Dr. wahab

maly :           Kurt Maly

ajay :           Ajay Gupta

 

Ansewer:

Question 3:

Write a Motif program with following description:

q3   cm1  cmd2 …. cmdN

to create an interface with N+1 push buttons, where button i,   <=  i <=N,  is labeled with cmdi and button N+1 is labeled quit.

Clicking on  cmdi  button executes cmdi  and displays its output to stdout.   

The program exits by  clicking on the quit button.

 

Example:

% q3 ls  date who ps date

 

Creates the following interface:

For example clicking on date produces the following on the stdout:

 

Mon Oct 16 20:27:37 EDT 2006

 

Ansewer:

Question 4:

Consider the following program where the parent creates two children processes.

Modify this program such that:

The parent create a file specified as argv[1] and write the message: “Hello Kids” in that file.

It then sends SIGUSR1 to child1 and SIGUSR2 to child2 and exits.

When a child receives signal from the parent, it reads the line from the file, prints it to the  stdout and exits.

Example:

% q4  q4file

% cat q4file

Hello Kids

----------------q4.c ---------------------------------

int main (int argvc, char **argv)

{

        pid_t pid1=0, pid2=0;

 

 

 

 

 

 

 

 

 

 

 

 

 

        pid1 = fork();

        if (pid1 == 0)   {

                printf("First Child: pid %d\n", getpid());

                pause();

 

 

               

 

 

 

 

 

 

 

        }

        else {

                pid2 = fork();

                if ( pid2 == 0 ) {

                        printf("Second Child: pid %d\n", getpid());

                        pause();

 

 

 

 

 

 

 

 

 

 

 

                }

                else {

                        printf("Parent: pid %d, pid1 %d, pid2 %d\n",

                                          getpid(),  pid1, pid2);

                        pause();

 

 

 

 

 

 

 

 

 

 

 

                }

        }

 

}

 

 

Ansewer:

Question 5:

Like Question 4, in the following program, the parent creates two children processes.

Modify this program such that:

1.      The parent dies after a random amount of time T between 1 and 100 seconds.

2.      Each child sleeps a random amount of time T between 1-10 seconds and then checks to see if the parent is dead. 

3.      When a child discovers that the parent is dead, it kills the other child (may be to inherit all the wealth!), and then exits.

You may assume that the PIDs of the two children are consecutive.

 

int main (int argvc, char **argv)

{

 

        pid_t pid1=0, pid2=0;

 

 

 

 

 

 

 

 

 

 

 

 

 

        pid1 = fork();

 

        if (pid1 == 0)   {

                printf("First Child: pid %d\n", getpid());

                pause();

 

 

               

 

 

 

 

 

 

 

 

 

        }

        else {

                pid2 = fork();

                if ( pid2 == 0 ) {

                        printf("Second Child: pid %d\n", getpid());

                        pause();

 

 

 

 

 

 

 

 

 

 

 

                }

                else {

                        printf("Parent: pid %d, pid1 %d, pid2 %d\n",

                                          getpid(),  pid1, pid2);

                        pause();

 

 

 

 

 

 

 

 

 

 

 

                }

        }

 

}

Ansewer: