Handed out Monday, March 2, 2015
Due 10:00 AM, Monday, March 9, 2015
These problems should be done on your own. However, in a departure from our usual policy, you can post public questions about code to Piazza, provided that you do not post solutions or code excerpts of more than, say, two lines. You can and should answer each other's questions (again following the guidelines). The high-level point here is to facilitate conscientious collaboration through Piazza.
As usual, we're not going to grade these strictly (we'll mainly look at whether you attempted them). But they reinforce knowledge and skills that the remaining labs will assume, so you ought to work through them carefully.
If you are not yet fully comfortable with C, this homework may take more time than the others; please plan accordingly and please use Piazza as a resource. As usual, you must write code on your own.
There are many metrics that a scheduler has to balance: turnaround time, response time, throughput, and various definitions of fairness. We will consider the following types of systems in this question:
Next, we will quickly walk through how to write and compile a program in C, and then you will write and compile several programs of your own.
Using a text editor, create a file called fun.c. In this file, type:
#include <stdio.h> int main(int argc, char** argv) { char* first_arg; char* second_arg; /* this checks the number of arguments passed in */ if (argc != 3) { printf("usage: %s <arg1> <arg2>\n", argv[0]); return 0; } first_arg = argv[1]; second_arg = argv[2]; printf("My program was given two arguments: %s %s\n", first_arg, second_arg); return 0; }This is a C program. The
#include
at the top tells the
compiler to use the header files of "standard I/O" (the standard
input/ouput functions of the C library; these functions include
printf). Also, argc contains the number of arguments
passed to the program (including the program name itself) while
argv[0] contains the name of the
program that was invoked.
You can compile this program using:
$ gcc -g -Wall -o fun fun.cYou can now run this program using:
$ ./funYou should see:
usage: ./fun <arg1> <arg2>You can also do:
$ ./fun abc def My program was given two arguments: abc defNote the pattern here:
$ gcc -g -Wall -o <output-file-name> <input-file-name>(We are not using makefiles for the time being.)
You will use this pattern in the exercises below; these exercises ask you to write several programs from scratch.
int my_strcmp(char* a, char* b)and then prints out the output of this function. my_strcmp should return 1 if a and b are not the same string and 0 if they are equal. You should implement my_strcmp without calling the standard C function strcmp! (You may thank us later; potential employers have been known to ask this question, or variants of it, in interviews.) To do this problem, remember that C strings end with a NULL character (the byte 0).
Consider the following code:
#include <stdlib.h> #include <stdio.h> // A box. Each box has an ID and a pointer to the box that resides inside // of it. If the box has nothing inside of it, inner_box should be equal // to NULL. struct box { int id; struct box *inner_box; }; // Insert box: places the box "inner" inside of the box "outer". // Since "outer" is being modified, we pass a pointer to "outer". // Since "inner" is not being modified, we pass in "inner" directly. void insert_box(struct box* outer, struct box inner) { printf("insert box: placing id %d inside id %d\n", inner.id, outer->id); outer->inner_box = &inner; } // Print box: prints a box and the box inside of it. This function // is recursive and will end once a box is empty. void print_box(struct box* first, int level) { int i; if (!first) return; for (i=0; i < level; ++i) { printf("- "); } printf("id: %d\n", first->id); print_box(first->inner_box, level+1); } int main() { // Create three boxes. struct box box1 = { .id = 37, .inner_box = NULL }; struct box box2 = { .id = 12, .inner_box = NULL }; struct box box3 = { .id = 19, .inner_box = NULL }; // The box ordering should be box1 -> box2 -> box3 insert_box(&box1, box2); insert_box(&box2, box3); // Print the boxes starting from the outside box. print_box(&box1, 0); return 0; }
Last updated: Mon May 04 11:24:46 -0400 2015 [validate xhtml]