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

CS 471
Operating Systems


Spring 2010
Midterm Exam

Time 2 & 1/2 hours


Open Book & Notes

 

 

 

 Name:

 Unix Login:

 

Each Question is 25 points.

 

 


Question 1

 

A.  

UNIX has many systems calls. List  two  of UNIX system  calls that deals with the following components of  the operating system:

 

1.  Process Control

 

 

2.  Files Manipulation

 

 

3.  Interprocess Communications.

 

 

B.  

In operating systems a process is created and then eventually is terminated.

In between creation and termination it changes states. 

List the names of 3 such states.


 

 

C.  

Consider the following three processes submitted in the order:

P1, P2 and  P3 with estimated CPU times:

4, 3  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:

 

 


 

 

Question 2 

 

 

A.  

 

What is the output of the following program?

 

int main()

{

        pid_t pid = fork();

 

        if (pid == 0) {

            printf("I am the child\n");

            exit(0);

        }

        wait(NULL);

        printf("I am the parent\n");

}

 

 

 

 

 

 

 

 

B.  

What is the output of the above program if we remove the exit (0) statement?

 


 

 

 

C. What is the output of the following program?

 

int main()

{

        int* var = (int *) malloc(sizeof(int));

        *var = 5;

 

        pid_t pid = fork();

 

        if (pid == 0) {

            (*var)++;

            printf("I am the child, var=%d\n", *var);

            exit(0);

        }

        wait(NULL);

        printf("I am the parent, var=%d\n", *var);

}

 

 

 

D. What is the output of the following program?

 

int main()

{

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

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

     *var = 5;

 

     pid_t pid = fork();

 

     if (pid == 0) {

             (*var)++;

              printf("I am the child, var=%d\n", *var);

              exit(0);

     }

     wait(NULL);

     printf("I am the parent, var=%d\n", *var);

}


 

Question 3 

 

A. In the following program the parent send the message “HI my child” to the child. Modify this program so that the parent creates a second pipe for the child to send the message “HI my parent” to the parent. Thus the program   output should be:

 

HI my child

HI my parent

 

int main(void)

{

        pid_t pid;

        int fd1[2];

       

        char buf[100];

        pipe(fd1);

       

 

        pid = fork();

        if (pid > 0) {

                close(fd1[0]);

                write(fd1[1], "HI my child\n", 12);

                close(fd1[1]);

 

 

 

 

 

 

 

        }

        else {

                close(fd1[1]);

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

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

                close(fd1[0]);

 

 

 

 

 

 

 

 

        }

}

 

 


 

 

B. In the following program the parent send the message “HI my child” to the child. Modify this program so that the child send the message “HI my parent” to the parent. Thus the program   output should be:

 

HI my child

HI my parent

 

int main(void)
{
    int             len, listenfd, connfd;
    struct sockaddr_in servaddr, cliaddr;
    char            buff[512];


    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(2345);

    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    bind
(listenfd, (SA *) & servaddr, sizeof(servaddr));

    listen(listenfd, 0);
    if (fork() == 0) {
         myClient();
    }
    len = sizeof(cliaddr);
    connfd = accept(listenfd, (SA *) & cliaddr, &len);
    sprintf(buff, "%s", "HI my child\n");
    write(connfd, buff, strlen(buff));
   

 

 

    close(connfd);
    exit(0);
}


 

void myClient()
{
    int             sockfd, n;
    char            recvline[512];
    struct sockaddr_in servaddr;
    struct hostent *hp, *gethostbyname();

    bzero(&servaddr, sizeof(servaddr));
    hp = gethostbyname("localhost");
   
bcopy(hp->h_addr, &(servaddr.sin_addr.s_addr), hp->h_length);
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(2345);

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    connect
(sockfd, (SA *) & servaddr, sizeof(servaddr));  

   
    read(sockfd, recvline, 512);
    printf("%s", recvline);
   

 

 

    close(sockfd);
    exit(0);
}

 


 

Question 4 

 

 

What is the output of the following four programs?

A.
void           *functionC();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_t       thread1, thread2;
int             counter = 5;

main(int argc, char *argv[])
{
      
pthread_create(&thread1, NULL, &functionC, NULL);
     pthread_create(&thread2, NULL, &functionC, NULL);

       pthread_join(thread1, NULL);
       pthread_join(thread2, NULL);
}
void    *
functionC()
{
      int             i, work;     
     
for (i = 1; i <= 2; i++) {
           work = counter;
           work = work + 1;
           counter = work;
           printf("Thread (%d): i is: %d, counter is: %d \n",

                       pthread_self(), i, counter);
     }
}

 

 

 

 

B.
void           *functionC();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_t       thread1, thread2;
int             counter = 5;
main(int argc, char *argv[])
{      same as
A.  
}
void     *
functionC()
{
      int             i, work ;
     
for (i = 1; i <= 2; i++) {
           work = counter;
          
sleep(2);
           work = work + 1;
           counter = work;
           printf("Thread (%d): i is: %d, counter is: %d \n",

                  pthread_self(), i, counter);
     }
}


 

 

 

 

C.
void           *functionC();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_t       thread1, thread2;
int             counter = 5;
main(int argc, char *argv[])
{ ame as
A.
}
void           *
functionC()
{
      int             i, work;     
     
pthread_mutex_lock(&mutex1);
     for (i = 1; i <= 2; i++) {
           work = counter;
          
sleep(2);
           work = work + 1;
           counter = work;
           printf("Thread (%d): i is: %d, counter is: %d \n",

                 pthread_self(), i, counter);
     }
    
pthread_mutex_unlock(&mutex1);
}

 

 

 

 

 

 

D.
void           *functionC();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_t       thread1, thread2;
int             counter = 5;
main(int argc, char *argv[])
{ same as
A.
}
void    *
functionC()
{
      int             i,  work;
     
for (i = 1; i <= 2; i++) {
          
pthread_mutex_lock(&mutex1);
           work = counter;
           work = work + 1;
           counter = work;
           printf(" Thread (%d): i is: %d and counter is: %d \n",

                  pthread_self(), i, counter);
          
pthread_mutex_unlock(&mutex1);
          
sleep(2);
     }
}