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

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

 

 

Name:                              
Login:

 

 


Question 1: (10 points)

Write a shell script that have the following description:

ColumnSpilt   <separator>    <file1>   <file2>    <file3>

To split <file1> into two files: <file2> and <file3>.

Each line of <file1> has two fields separated by <separator>.

<file2> contains the first field while <file3> contains the 2nd field. of <file1>

Example:

% cat f1

Wahab : Husein Abdel Wahab

Cmo : Mike Overstreet

% ColumnSplit  :  f1  f2  f3

% cat f2

Wahab

Cmo

% cat f3

Hussein Abdel-Wahab

Mike Overstreet

Ansewer:

 


Question 2: (15 points)

The passwd file has valuable information about users, e.g.,

% ypcat passwd | grep wahab

wahab:stg/i.0xxJ1zU:51:13:Dr. wahab:/home/wahab:/usr/local/bin/tcsh

The 4th field of the above line indicates the group number of wahab  (13).

Wrie a shell program with following description:

CountGroups

To  produce a count the number of users in  each group of the passwd file.

Example:

% CountGroups

   2217   21

    173    22

     83     13

     …etc…

In this output, group 21  has are 2217  users, group  22 has  173 users, etc.

 

Ansewer:

 


Question 3: (15 points)

The group file contains information about user groups, e.g.,

% ypcat group | grep :13:

faculty:*:13: …..

Thus 3rd field of output (13)  is the  faculty group.

Write a shell script called GroupNames to translate the output of Question 2 to produce the group names beside  their numbers.

Example:

% CountGroups | GroupsNames

   2217   21    student

    173     22    grad

     83      13   faculty

   …etc…

 

 

Ansewer:

 


Question 4: (20 points)

Consider the following program that draws lines.

Modify this program (by showing only the changes, do not recopy the program) such that:

Ø      The 3rd click draws triangles (drawing two lines to the preceding two points).

Ø      Any key press clears the drawing window.

Ø      The program exits by clicking the right button (button3).

 

#define TRUE 1

#define FALSE 0

 

main(argc,argv)

int argc;

char **argv;

{

        int i,j;

        Display *display;

        Window root, window;

        long fgcolor, bgcolor;

        int screen;

        int pointx1, pointy1;

        int eventmask = KeyPressMask|ExposureMask|ButtonPressMask;

        XEvent event;

        XGCValues gcval;

        GC draw;

        int FirstPt = FALSE;

        if (!(display = XOpenDisplay(argv[1]))) {

                perror("XOpenDisplay");

                exit(1);

        }

 

        root = RootWindow(display,screen = DefaultScreen(display));

        fgcolor = BlackPixel(display,screen);

        bgcolor = WhitePixel(display,screen);

 

        window = XCreateSimpleWindow(display,root,0,0,200,200,2,

            fgcolor,bgcolor);

 

        gcval.foreground = fgcolor;

        gcval.background = bgcolor;

        draw = XCreateGC(display,window,GCForeground|GCBackground,&gcval);

 

        XSelectInput(display,window,eventmask);

        XMapWindow(display,window);

 

 

 

 

 

               for (;;) {

                XWindowEvent(display,window,eventmask,&event);

                switch (event.type) {

                case ButtonPress:

                        if ( event.xbutton.button == Button1 ) {

                                if (FirstPt==FALSE) {

                                        FirstPt=TRUE;

                                        pointx1 = event.xbutton.x;

                                        pointy1 = event.xbutton.y;

                                        XDrawPoint(display,window,draw,pointx1,pointy1);

                                        break;

                                }

                                else {

                                        FirstPt = FALSE;

                                        XDrawLine(display,window,draw,pointx1,pointy1,event.xbutton.x, event.xbutton.y);

                                        break;

                                }

                        }

 

                case KeyPress:

                        exit(0);

                }

        }

}

 

 

 

 

 

modifications:


Question 5: (40 points)

Write a program that creates a child process with following description:

% ParentChild   <input_file>    <parent_output_file>   <child_output_file>

The parent process  reads 10 chars from the <input_file> and writes into <parent_output_file> and then sends a signal to the child process  to read next 10 chars from the <input_file>. The child process reads the next 10 char and writes into <child_output_file> then signals the parent process to read the next 10 chars. This is repeated until one of the two processes reaches the end of the <input_file> then kills the other process and exits.

Example:

% ParentChild infile outfile1 outfile2

% paste infile outfile1 outile2

 

012345678       012345678       abcdefhig

abcdefhig         012345678       abcdefhig

012345678       012345678       abcdefhig

abcdefhig              

012345678              

            abcdefhig

 

Ansewer: