Name: ________________________________ Login: _____________

Comp111 Midterm Exam
Oct 23, 2012 - open books and notes

No electronic devices of any kind allowed, including cellphones, calculators, pagers, etc. Please turn these off before beginning the exam.

Please show all work on these pages. Make sure that your work for each problem is clearly marked. These pages will be graded and returned electronically, like exercises.
problemscoretotal
1 20
2a 10
2b 10
2c 10
3a 10
3b 10
3c 10
4 20
5 10
total 100

Name: ________________________________ Login: _____________

  1. (20 points) Consider the following code:
     
    main()
    { 
        FILE *foo = fopen("foo.txt", "w"); 
        fprintf(foo, "yo there"); 
        close(1); dup(fileno(foo)); 
        if (fork()) { 
            fprintf(foo, "hi there\n"); 
        } else { 
    	printf("ho there\n"); 
        } 
    } 
    
    After this code fragment, what is printed to the user, and what are the possible contents of "foo.txt"? Why?

    Name: ________________________________ Login: _____________

  2. Suppose that two processes try to execute the following fragments:
    pthread_mutex_lock(&A); 
    pthread_mutex_lock(&C); 
    pthread_mutex_lock(&D); 
    pthread_mutex_lock(&E); 
    
    pthread_mutex_lock(&B); 
    pthread_mutex_lock(&D); 
    pthread_mutex_lock(&C); 
    pthread_mutex_lock(&E); 
    
    where A, B, C, D, E are mutexes.
    1. (10 points) Can these two fragments deadlock? If so, give a schedule that deadlocks. If not, why not?










    2. (10 points) Can the order in which mutexes are unlocked ever cause a deadlock? Why or why not?










    3. (10 points) Suppose we implement the above with semaphores and one call to semop per thread, rather than with multiple calls to pthread_mutex_lock. Can the resulting fragments deadlock? Why or why not?

    Name: ________________________________ Login: _____________

  3. Consider the code:
     
    main()
    {
        if (fork()) {
            int status; wait(&status);
        } else {
            int fd1 = open("foo.txt", O_WRONLY, 0);
            int fd2 = open("bar.txt", O_RDONLY, 0); 
            close(0); dup(fd1);
            close(1); dup(fd2);
            execl("/bin/cat", "/bin/cat", NULL);
        }  
    }
    
    1. (10 points) This code contains a rather obvious error that prevents it from doing anything useful. What is it? How would you correct it?









    2. (10 points) With the error corrected, what does the code do?









    3. (10 points) In my examples, I often omitted calls to close. Which ones did I omit here? Why is this not an error?

    Name: ________________________________ Login: _____________

  4. (20 points) Suppose I have written a threaded routine
     
    struct output *doit(struct input *stuff); 
    
    How does one have to invoke this thread inside pthread_create, and why?















  5. (Advanced; 10 points extra credit) List the things that a process does not need to know about the operating system, and explain why that knowledge is not necessary.