Operating Systems 2021F: Assignment 1

From Soma-notes
Jump to navigation Jump to search

Please submit the answers to the following questions via Brightspace by October 1, 2021. There are 20 points in 10 questions.

Submit your answers as a plain text file following this template. Name your answer file "<username>-comp3000-assign1.txt" (where username is your MyCarletonOne username).

Your answers will be parsed by a script in order to help with grading so please preserve the format of the template. No other formats will be accepted.

Don't forget to include what outside resources you used to complete each of your answers, including other students, man pages, and web resources. You do not need to list help from the instructor, TA, or information found in the textbook.

Questions

  1. [2] By default, what answers will give you a perfect score in 3000quiz? Why?
  2. [2] How is the second question in 3000quiz represented in assembly language? How does this compare to the string on line 57, in main()?
  3. [2] Where are the_questions, the_answers, and response (user input) stored: the stack, heap, or elsewhere? Can you verify this by looking at their relative addresses? Explain for each.
  4. [2] How can you remove the constant numQuestions and replace it with a runtime calculated count of the number of questions? Does anything in the answers data structure help with this calculation? Explain briefly.
  5. [2] How can you change 3000quiz so that if you run "./3000quiz Sally" the answer to question 4 is Sally? Only change the data passed to askQuestions(), don't change the code in askQuestions(). Can you do this without copying the string? Explain briefly.
  6. [2] How can you change 3000quiz so that if you set the environment variable DOGNAME to Anil's dog's name the answer to question 2 will be correct? (Again, just change the data passed to askQuestions(), not the code of askQuestions().) Can you do this without copying the string? Explain briefly.
  7. [2] Run 3000quiz using setarch -R: setarch -R ./3000quiz. How does this change the execution of 3000quiz? How can you tell?
  8. [2] Does adding environment variables change where things are stored in memory? How do you know?
  9. [2] Are there system calls (i.e., machine language instructions for making system calls) in the 3000quiz binary? How do you know? Why or why not?
  10. [2] How can you create a static binary of 3000quiz (i.e., one that loads no libraries at runtime)? Does this version have system calls in its binary? How do you know? Why or why not?

Code

3000quiz.c

 1 /* 3000quiz.c */
 2 
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 #include <unistd.h>
 6 #include <string.h>
 7 
 8 char * the_questions[] = {
 9         "What is the capitol of Canada?",
10         "What is the name of Anil's dog?",
11         "How many feet in a mile?",
12         "What is your name?",
13         NULL
14 };
15 
16 char * the_answers[] = {
17         "Ottawa",
18         "ENV: DOGNAME",
19         "5280",
20         "Bob",
21         NULL
22 };
23 
24 int askQuestions(char **question, char *answer[],
25                  char *argv[], char *envp[])
26 {
27         int score = 0;
28         int q = 0;
29         int c;
30         int res;
31         char response[100];
32         
33         while (question[q] && answer[q]) {
34                 printf("%s ", question[q]);
35                 c = scanf("%100s", response);
36                 if (c != EOF) {
37                         if (strcmp(answer[q], response) == 0) {
38                                 printf("Correct!\n");
39                                 score++;
40                         } else {
41                                 printf("Sorry, the answer is %s.\n", answer[q]);
42                         }
43                 }
44                 q++;
45         }
46         
47         return score;
48 }
49 
50 int main(int argc, char *argv[], char *envp[])
51 {
52         int score;
53         int numQuestions = 4;
54 
55         score = askQuestions(the_questions, the_answers, argv, envp);
56 
57         printf("\nYour score is %d out of %d.\n", score, numQuestions);
58 
59         return 0;
60 }

Solutions

Assignment 1 Solutions