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

CS 471
Operating Systems


Spring 2012
Midterm Exam

Time 2 & 1/2 hours


Open Book & Notes

 

 

 

Name:

Unix Login:

 

Each Question is 20 points.

 

 


Question 1

What is the print out of the following programs:

A.

int main()

{

        fork();

        fork();

        fork();

        printf("Q1a cs471w\n");

       

}

 

 

 

 

 

 

 

 

 

 

 

 

B.   

int main()

{

        if (fork())

        if (fork())

        if (fork())printf("Q1b cs471w \n");

}

 


 

 

C.

int main()

{

        if (fork()) execl("Q1cexec", "Q1cexec", 0);

        if (fork()) execl("Q1cexec", "Q1cexec", 0);

        if (fork()) execl("Q1cexec", "Q1cexec", 0);

        sleep(1);

        printf("Q1c cs471w\n");

       

}

 

Q1cexec:

int main()

{

  printf("Q1cexec cs471w\n");

  

}

 


 

 

Question 2

 

Consider the following three processes submitted in the order:

P1, P2 and P3 with estimated CPU times: 8, 6 and 2. 

Compute the average waiting time if these 3 processes are scheduled according to:

1)      FCFS:

 

 

 

 

  

 

 

2)      SJF:

 

 

 

 

 

 

 

 

 

3)      RR with Quantum time = 2. How many context switches are needed?

 


 

Question 3 

 

What is the output of the following programs?

A.

#define SIZE 1000

int main(void)

{

      int fd[2];

      char buf[SIZE];

      char *ParentMsg="How are you my child";

      char *ChildMsg="I am fine my parent";

      int i = SIZE;

 

      pipe(fd);

 

      if (fork()) {

            write(fd[1], ParentMsg, strlen(ParentMsg));

           wait(NULL);

            read(fd[0], buf, 100);

            printf("Parent got: %s \nand  Parent i = %d\n",buf, i);

           }

      else {

           sleep(1);

            read(fd[0], buf, 100);

            printf("Child got: %s\n",buf);

           sprintf(buf,"%s Child i = %d", ChildMsg, ++i);

            write(fd[1], buf, strlen(buf));

      }

}

 


 

B.

#define SIZE 3

int

main()

{

   int   segment_id = shmget(IPC_PRIVATE, SIZE * sizeof(int), S_IRUSR | S_IWUSR);

   int   *svar = (int *) shmat(segment_id, NULL, 0);

   int   *nvar = (int *) malloc(SIZE * sizeof(int));

   int   i;

 

   for (i = 0; i < SIZE; i++) {

      svar[i] = SIZE;

      nvar[i] = SIZE;

   }

 

   pid_t  pid = fork();

 

   if (pid == 0) {

      for (i = 0; i < SIZE; i++) {

         (svar[i])++;

         (nvar[i])--;

         printf("I am the child: %d, %d\n", svar[i], nvar[i]);

      }

      exit(0);

   }

 

   wait(NULL);

 

   for (i = 0; i < SIZE; i++) {

      (svar[i])--;

      (nvar[i])++;

      printf("I am the parent: %d, %d\n", svar[i], nvar[i]);

   }

}

 


 

C.

int             sfd;

void            One(), Two(), Three();

int main()

{

   sfd = open("tmpfile", O_RDWR | O_TRUNC | O_CREAT, 0600);

   if (fork() == 0) { 

      One();

      exit(0);

   } else {

      if (fork() == 0) {

         Two();

         exit(0);

      } else {

         wait(NULL);

         wait(NULL);

         Three();

      }

   }

}

void One()

{

   write(sfd, "123 ", 4);

   sleep(2);

   exit(0);

}

void Two()

{

   sleep(1);

   write(sfd, "\n321", 4);

   sleep(1);

   exit(0);

}

void Three()

{

   char buf[10];

   int one, two;

   lseek(sfd, 0, SEEK_SET);

   read(sfd, buf, sizeof(buf));

   printf("%s\n", buf);

   sscanf(buf, "%d %d", &one, &two);

   printf("%d\n", one+two);

   exit(0);

}

 


 

Question 4 

 

The following programs can serve either as Echo Server or Chat Server

depending on how the function HandleClient is implemented.

 

#define SA      struct sockaddr

#define SIZE 10

 

void           HandleClient(int);

int   *connfd ;

 

int main(int argc, char *argv[])

{

      int             len, listenfd;

      struct sockaddr_in servaddr, cliaddr;

      int             i;

 

      int   segment_id1 = shmget(IPC_PRIVATE, SIZE * sizeof(int), S_IRUSR | S_IWUSR);

      connfd = (int *) shmat(segment_id1, NULL, 0);

 

      for (i = 0; i <= SIZE; i++)   connfd[i] = -1;

 

      listenfd = socket(AF_INET, SOCK_STREAM, 0);

 

      bzero(&servaddr, sizeof(servaddr));

      servaddr.sin_family = AF_INET;

      servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

      servaddr.sin_port = htons(atoi(argv[1]));

 

      if (bind(listenfd, (SA *) & servaddr, sizeof(servaddr)) < 0) {

            perror("bind");

            exit(-1);

      }

      listen(listenfd, 0);

 

      for (;;) {

            for (i = 0; i <= SIZE; i++) {

                  if (connfd[i] == -1) {

                        len = sizeof(cliaddr);

                        connfd[i] = accept(listenfd, (SA *) & cliaddr, &len);

                       if (fork() == 0) HandleClient (i);

                  }

            }

      }

}

 

 


 

A.     Implement the  HandleClient to make the server act  as an  Echo Server:

 

void   HandleClient(int myIndex)

{

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

     

}

 

B.     Implement the  HandleClient to make the server act  as a  Chat Server:

 

void   HandleClient(int myIndex)

{

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

     

}

 

Question 5 

What is the output of the following program?

A:

#define MAX 1

void           *function1();

void           *function2();

pthread_mutex_t mutexA = PTHREAD_MUTEX_INITIALIZER;

pthread_mutex_t mutexB = PTHREAD_MUTEX_INITIALIZER;

pthread_t       thread1, thread2;

int             A = MAX;

int             B = 0;

main(int argc, char *argv[])

{

      pthread_create(&thread1, NULL, &function1, NULL);

      pthread_create(&thread2, NULL, &function2, NULL);

      pthread_join(thread1, NULL);

      pthread_join(thread2, NULL);

}

void  *function1()

{

      int CONTINUE = 1;

      while (CONTINUE) {

            pthread_mutex_lock(&mutexA);

                sleep(1);

            pthread_mutex_lock(&mutexB);

                A--;

                B++;

            if ((A <= 0) &&  (B >= MAX) ) CONTINUE = 0;

                printf("Thread 1: A=%d, B=%d\n", A, B);

                fflush (stdout);

            pthread_mutex_unlock(&mutexB);

            pthread_mutex_unlock(&mutexA);

      }

      printf("Thread 1 is done\n");

      pthread_exit(0);

}

void  *function2()

{

int CONTINUE = 1;

      while (CONTINUE) {

            pthread_mutex_lock(&mutexB);

                sleep(1);

            pthread_mutex_lock(&mutexA);

                A--;

                B++;

            if ((A <= 0) &&  (B >= MAX) ) CONTINUE = 0;

                printf("Thread 2: A=%d, B=%d\n", A, B);

                fflush (stdout);

            pthread_mutex_unlock(&mutexA);

            pthread_mutex_unlock(&mutexB);

      }

      printf("Thread 2 is done\n");

      pthread_exit(0);

}

 


 

             

B.

What is the output of the above program if

     sleep(1);

is moved up  before the first lock statement in both  function1 and function2.