Review Session 1 CS 202 2 Feb 2015 1. Lab overview 2. Processes 3. C review 4. Git introduction 5. Lab Q&A --------------------------------------------- 1. Lab overview - Lab overview - building and simulating a basic operating system - QEMU - virtual machine - Virtual machine (VM) - software that emulates hardware - Runs software that directly targets hardware (operating systems) - Devbox/SSH overview - Devbox - another virtual machine - running a VM on another VM - SSH - method to access remote UNIX servers - creates a secure tunnel and forwards commands/output 2. Processes - Instance of a running program - Processes have the illusion of controlling a complete machine - contiguous memory, unrestricted access to the CPU, etc. - Processes communicate with the OS through system calls which allow access to resources the OS controls, such as files and devices - Process state diagram: create, ready, running, blocked, completed - Creating new processes - fork(), exec() 3. C Review - Procedural, unlike Java - no objects - Allows for direct memory addressing - speed/flexibility at the cost of safety - Pointers - Pointers contain an address in memory - Dereference/indirection operator (*) - Reference/address operator (&) - [Example 1 - Homework 1, Problem 2 1 void op1(int a, int b) { 2 a = a + b; 3 } 4 5 void op2(int* a, int* b) { 6 *a = *a + *b; 7 } 8 9 int main() { 10 int a = 1, b = 1, c = 1, d = 1; 11 op1(a, b); 12 op2(&c, &d); 13 return 0; 14 } ] - Arrays - Allocate a contiguous block in memory - Variable name is actually a pointer to the first index of an array - Bracket operator [] dereferences at a certain offset - Reference operator (&) provides the address to the array - [Example 2 - Homework 1, Problem 4 1 int a[5]; 2 3 a[0] = 0; 4 a[1] = 10; 5 a[2] = 100; 6 7 //COMMENT 1 8 9 a[3] = *a + 2; 10 a[4] = *(a + 2); 11 12 //COMMENT 2 ] - Structs - Named collection of fields - Unlike in C++ or Java - cannot contain functions/methods, no inheritance - Fields in a struct are accessed using the dot operator (.) - Arrow operator (->) is used to access values from a pointer to a struct - it behaves like the dot operator, but deferences the pointer first (a->b is shorthand for (*a).b) - [Example 3 1 struct bar { 2 int k; 3 }; 4 5 struct foo { 6 int a; 7 struct bar b; 8 }; 9 10 int main() { 11 const int NFOO = 5; 12 struct foo foo_array[NFOO]; 13 struct foo* foo_p = 0; 14 int i = 0; 15 16 for (; i < NFOO; ++i) { 17 foo_array[i].a = i; 18 foo_array[i].b.k = 0; 19 foo_p = &foo_array[i]; 20 if (foo_p->a > 2) { 21 foo_p->b.k = 10; 22 } 23 printf("%d %d\n", foo_p->a, foo_p->b.k); 24 } 25 26 return 0; 27 } ] - Java/C call by value semantics - Java variables implicitly store references to objects - functions have references to their arguments - In C, the actual values themselves are stored - when a function is called argument data is copied. With pointer arguments, the address is copied - but since the address is the same, data can be modified by the function. - While C++ supports pass-by-reference, C does not - but it can be simulated by passing and dereferencing pointers - [Example 4 1 struct item { 2 int price; 3 int quantity; 4 }; 5 6 int compute_total_price(struct item i) { 7 return i.price * i.quantity; 8 } 9 10 void double_quantity(struct item i) { 11 i.quantity *= 2; 12 } 13 14 void double_price(struct item* i) { 15 i->price *= 2; 16 } 17 18 int main() { 19 struct item my_item; 20 my_item.price = 25; 21 my_item.quantity = 100; 22 printf("%d\n", compute_total_price(my_item)); 23 24 double_quantity(my_item); 25 printf("%d\n", compute_total_price(my_item)); 26 27 double_price(&my_item); 28 printf("%d\n", compute_total_price(my_item)); 29 30 return 0; 31 } ] - Compilation process - Typical linux toolchain - gcc - C compilation process - .h/.c files -> assembly -> machine code -> executable - Makefiles - specify compilation dependencies and ordering 4. Git introduction - Git is a revison control system (also known as source control, version control...) that will be used during this class - Not enough time today to cover Git in detail - There are plenty of git tutorials online - experimentation is the best way to learn. - Below is a basic outline of what Git does and the simple commands that are used: - Git - a distributed revision control system - Revision control - timeline of changes to files, stored as a graph - Distributed - no centralized client/server. Users maintain their own repositories; patches (commits) are created and distributed peer-to-peer - Git repository - Git repositories are local and self contained - Usually are set up to receive change patches from other repositories (fetch/pull) and send local changes to other repositories when ready (push) - Typical git workflow: users pull changes from a remote repository, make local changes, create a record of these changes (a commit), and push commits to everyone else - Commands to create repositories - Repositories are created locally - Init - create a new, empty repository - Clone - create a new repository; copying the commit tree from a URL - Steps to add files and make commits - Git keeps track of files that are different from the previous commit - Add - specify files that should be included in the next commit - Commit - create a patch of changes to the specified files and store it locally - Interacting with other repositories - Pull - receive new commits from a remote server - Push - send new commits to a remote server - Other main features of git that are worth learning about - Reverting to/checking out previous commits - Multiple branches - Resolving conflicts - merge/rebase - How to receive code for the labs - git clone with the provided URL 5. Lab 1 Q&A