Issuu on Google+

CMSC 421: Principles of Operating Systems

Homework 1 (15 points)

Homework 1 will deal with basic concepts in C (memory allocation in particular), introduction to operating systems, the user-kernel boundary, and very basic x86 assembly to understand this userkernel boundary. The homework has two sections. The first section deals with writing very short C programs. The second section deals with two OS specific questions. What do you need to submit: The theory question should be submitted as a pdf. The C questions must be submitted as .c files and the assembly code should be a .s file. Each question has instructions on what to submit. After you are done with the problems, you should tar all files up into one file hw1.tar and submit it via Blackboard. (command for taring together multiple files called x.c, y.c, and writeup.pdf is the following tar –cvf hw1.tar x.c y.c writeup.pdf)

Part 1 (10 points) 1 Memory allocation and de-allocation is extremely important in writing OS code. Since most OSes are written in C, that does not provide garbage collection, the developer should allocate and deallocate pointers diligently. C provides library functions for allocation and deallocation. malloc is a popular function for allocating a chunk of memory on the heap. free is used to deallocate memory on the heap. The signatures of the functions are as follows void *malloc(size_t size) //size_t == int void free(void *ptr) // ptr == points to the allocated chunk of memory The goal of this problem is to get started with writing your own simple garbage collector. The problem has two parts. In the first part you will write a “leaky” string concatenation program. In the second part, you will make the code non-leaky, which implies that you will perform proper garbage collection.

Question 1: Building a leaky string concatenation program (3 points)

We have provided you with a C file called hw1_leaky.c, which has the code underlined below. Change (only) the function concatenate (), so that it returns the result of concatenating strings a and b. For example, concatenate ("fire","cracker") returns the string "firecracker". You'll need to use malloc() inside concatenate. However, since there are no calls to free(), this code is leaking

1

Acknowledgments: Question 1 is adapted from the Operating System course taught at UIC by Prof. Jakob Erikkson


memory. NOTE: You may not use existing string library functions, such as sprintf, strcpy, etc for this homework. strlen() is ok. #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAXCHAR 10 char *concatenate(char* a, char* b) { return a; // replace this with your string concatenation code } int main(int argc, char** argv) { if(argc<4) { printf("Usage: hw1_leaky <count> <firstword> <secondword>\n"); exit(1); } char *middle="cruel"; char number[MAXCHAR]; int i=0; for(i=0;i<atoi(argv[1]);i++) { sprintf(number,"%d",i); // update the number string // begin leaky char *line = concatenate( // not leaked concatenate(argv[2],number), // leaked concatenate(middle, // leaked concatenate(number, argv[3]))); // leaked if(i>0) // can't free the statically allocated "cruel" string above free(middle); // end leaky printf("%s\n",line); middle=line; } free(middle); }

Example program output: ~> ./hw1_leaky 10 hello world hello0cruel0world hello1hello0cruel0world1world hello2hello1hello0cruel0world1world2world hello3hello2hello1hello0cruel0world1world2world3world hello4hello3hello2hello1hello0cruel0world1world2world3world4world hello5hello4hello3hello2hello1hello0cruel0world1world2world3world4world5world hello6hello5hello4hello3hello2hello1hello0cruel0world1world2world3world4world 5world6world hello7hello6hello5hello4hello3hello2hello1hello0cruel0world1world2world3world 4world5world6world7world hello8hello7hello6hello5hello4hello3hello2hello1hello0cruel0world1world2world 3world4world5world6world7world8world hello9hello8hello7hello6hello5hello4hello3hello2hello1hello0cruel0world1worl


Question 2: Manual Garbage Collection (7 points)

Without garbage collection, it's difficult to write the above program both elegantly and leakfree at the same time: all those nested concatenate calls are forgetting the pointers to the malloc()ed strings before we get a chance to free() them. Without resorting to much stronger measures, the only way to fix these leaks is to keep a pointer to each string until we don't need the string any more, and then free() it. Make a copy of your hw1_leaky.c, calling it hw1_nonleaky.c. Changing only the lines between the "begin leaky" and "end leaky" comments, make sure to plug every memory leak in this program. To help with the debugging, add the following before concatenate(), change the malloc() call in concatenate to mymalloc(), and use myfree() instead of free() (you are allowed to make those changes).

void* mymalloc(int size) { void* ptr = malloc(size); fprintf(stderr,"malloc: %p\n",ptr); return ptr; } void myfree(void* ptr) { fprintf(stderr,"free: %p\n",ptr); free(ptr); }

Example output: ./a.out 2 hello world malloc: 100080 malloc: 100090 malloc: 1000a0 malloc: 1000b0 free: 100080 free: 100090 free: 1000a0 hello0cruel0world malloc: 1000a0 malloc: 100080 malloc: 1000d0 malloc: 1000e0 free: 1000a0


free: 100080 free: 1000d0 free: 1000b0 hello1hello0cruel0world1world free: 1000e0

Part 2: Operating Systems Concepts Part 2 of the homework deals with basic OS concepts. Question 1 (5 points): (a) What functionality does an Operating System provide? Enumerate any three goals of an OS like linux or Windows. (2 point) (b) In a single processor machine, either a user process is executing on a CPU or the kernel is executing on the CPU at a time. Hence, if services from the OS is required, context switches from user-mode to kernel mode should take place. Enumerate any three scenarios under which such a context switch can occur (3 points).


Test 1