CSE 451: Operating Systems Section 1 Intro, C programming, project 0.

Slides:



Advertisements
Similar presentations
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Advertisements

David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Joel Winstead CS201j: Engineering Software University of Virginia Computer Science Lecture 16: Pointers and Memory Management.
CSE 451: Operating Systems Section 1. Why are you here? 9/30/102.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
CS414 C Programming Tutorial Ben Atkin
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
CSSE221: Software Dev. Honors Day 27 Announcements Announcements Projects turned in? Projects turned in? The 2 required Angel surveys are due by 9 pm tonight.
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
CS 61C L03 C Pointers (1)Garcia / Patterson Fall 2002 © UCB CS61C - Machine Structures Lecture 3 C pointers  Dan Garcia (
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
CS 61C L04 C Pointers (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
CS 61C L03 C Arrays (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c/ CS61C : Machine Structures Lecture #3: C Pointers & Arrays
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CSE 131 Computer Science 1 Module 1: (basics of Java)
CSE 451: Operating Systems Section 1 Intro, C programming, project 0.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Review C Language Features –control flow –C operators –program structure –data types –I/O and files Problem Solving Abilities.
Computer Science Detecting Memory Access Errors via Illegal Write Monitoring Ongoing Research by Emre Can Sezer.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
Lecture Contents Arrays and Vectors: Concepts of pointers and references. Pointer declarations and initialization. Pointer Operators. Dynamic Memory Allocation.
1 CSE1301 Computer Programming: Revision. 2 Topics Type of questions What do you need to know? About the exam Exam technique Staff consultation Revision.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
SPL – Practical Session 2 Topics: – C++ Memory Management – Pointers.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
CSE 451: Operating Systems Section 1. Why are you here?  Because you want to work for Microsoft and hack on the Windows kernel? 3/29/  Because.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Agenda Attack Lab C Exercises C Conventions C Debugging
CS 261 – Data Structures Introduction to C Programming.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 CSC2100B Data Structure Tutorial 1 Online Judge and C.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
PHY 107 – Programming For Science. Today’s Goal  Learn how arrays normally used in real programs  Why a function returning an array causes bugs  How.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 C Basics Monday, August 30, 2010 CS 241. Announcements MP1, a short machine problem, will be released today. Due: Tuesday, Sept. 7 th at 11:59pm via.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
Carnegie Mellon 1 Malloc Lab : Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
CSE 333 – SECTION 2 Memory Management. Questions, Comments, Concerns Do you have any? Exercises going ok? Lectures make sense? Homework 1 – START EARLY!
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
CSE 451 Section 1 C Refresher (or introduction?) Project 0.
Homework Notes from Sean
Dynamic Allocation in C
Object Lifetime and Pointers
YongChul Kwon CSE451 Section 1: Spring 2006 YongChul Kwon
CSE 374 Programming Concepts & Tools
CSE 451 C refresher.
CSE 303 Concepts and Tools for Software Development
Checking Memory Management
C Basics.
Recitation: C Review TA’s 19 Feb 2018.
Pointers, Dynamic Data, and Reference Types
CSE451 Fall 2008 Section 1 Roxana Geambasu
Warmup Write a function to add two integer parameters and return the result.
CSE 303 Concepts and Tools for Software Development
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

CSE 451: Operating Systems Section 1 Intro, C programming, project 0

Introduction  My name is Jeff and I am a first-year Ph.D. student in CSE  I graduated from Princeton last year with a B.A. in Computer Science and a minor in Musical Performance  My background is primarily in Systems, but at UW I will work at the intersection of HCI and ML (Visualization, Accessibility)  My office hours are Tuesday 1:30-2:20 and Thursday 2:30-3:20, or by appointment/when I’m in 002  Contact: discussion board or by 9/25/132

Why are you here?  You need a 400-level elective and this course fit your schedule  You have heard that Ed is an exceptional lecturer  You want a job / to go to graduate school in CS  You want to understand what goes on “under the hood” 9/25/133

Far-reaching implications  Concepts and techniques learned in lecture / through projects apply to all other areas of computer science  Data structures  Caching  Concurrency  Virtualization  OSes support all other areas of computer science 9/25/134

Course tools  Assn 0: Any computer with C development tools (002, attu, your *nix box)  Assn 1: Use the course VM inside an emulator (VMware, Qemu etc.) on your computer or a lab computer  Can compile on forkbomb.cs.washington.edu (faster) 9/26/135

Course tools  We’ll be using the GNU C Compiler (gcc) for compiling C code in this course, which is available on every platform except Windows (Cygwin lovers proceed at your own risk)  For an editor, use whatever you are most comfortable with; emacs, vim, gedit, and Eclipse are good choices (ed and butterflies also options) 9/26/136

Discussion board  The discussion board is an invaluable tool; use it!  Sean (my TA partner in crime) and I both receive alerts whenever there is a new post. Response time should be very fast.  For anything non-personal use the discussion board. 9/26/137

Collaboration  If you talk or collaborate with anybody, or access any websites for help, name them in your project submission  See the course policy for more restrictionscourse policy  Okay: discussing problems and techniques to solve them with other students  Not okay: looking at/copying other students’ code. Googling solutions. Using code from Wikipedia.  We will pass your code through plagiarism detection software (MOSS, Deckard, etc.) 9/26/138

C programming  Most modern operating systems are still written in C  Why not Java?  Interpreted Java code runs in a virtual machine, so what language is the VM built in?  C is precise in terms of  Instructions (semantics are clear)  Timing (can usually estimate number of cycles needed to execute code)  Memory (allocations/de-allocations are explicit) 9/26/139

C language features  Pointers  Pass-by-value vs. pass-by-reference  Structs  Typedefs (aliasing)  Malloc/free 9/26/1310

Pointers int iX = 5; int iY = 6; int* piX = &iX; // declare a pointer to iX // with value as the // address of iX *piX = iY; // change value of iX to iY // (iX == 6) piX = &iY; // change piX to point to // iY’s memory location // For more review, see the CSE 333 lecture // and section slides 9/26/1311

Function pointers int functionate(int iHerp, char cDerp) {... } // declare and define a function int (*pfFoo)(int, char) = NULL; // declare a pointer to a function // that takes an int and a char as // arguments and returns an int pfFoo = functionate; // assign pointer to functionate()’s // location in memory iX = pfFoo(7, 'p'); // set iX to the value returned by // functionate(7, 'p') 9/26/1312

Case study: signal() extern void (*signal(int, void(*)(int)))(int);  What is going on here?  signal() is ”a function that takes two arguments, an integer and a pointer to a function that takes an integer as an argument and returns nothing, and it ( signal() ) returns a pointer to a function that takes an integer as an argument and returns nothing.” (from StackOverflow) 9/26/1313

Case study: signal  We can make this a lot clearer using a typedef: // Declare a signal handler prototype typedef void (*SigHandler)(int iSignum); // signal could then be declared as extern SigHandler signal( int iSignum, SigHandler pfHandler); 9/26/1314

Arrays and pointer arithmetic  Array variables can often be treated like pointers, and vice-versa: int aiFoo[2]; // foo acts like a pointer to // the beginning of the array *(aiFoo + 1) = 5;// the second int in the // array is set to 5  Don’t use pointer arithmetic unless you have a good reason to do so 9/26/1315

Passing by value vs. reference int doSomething(int iFoo) { return iFoo + 1; } void doSomethingElse(int* piFoo) { *piFoo += 1; } void example(void) { int iX = 5; int iY = doSomething(iX); // iX==5, iY==6 doSomethingElse(&iX); // iX==6, iY==6 } 9/26/1316

Returning addl. information int initialize(int iArg1, int iArg2, int* piErrorCode) { // If initialization fails, set an error // code and return false to indicate // failure. if (...) { *piErrorCode =...; return EXIT_FAILURE; } //... Do some other initialization work return EXIT_SUCCESS; } 9/26/1317

Structs // Define a struct referred to as // "struct s2DPoint" struct s2DPoint { int iX; int iY; }; // Don’t forget the trailing ‘;’! // Declare a struct on the stack struct s2DPoint foo; // Set the two fields of the struct foo.iX = 1; foo.iY = 2; 9/26/1318

Typedefs typedef struct s2DPoint 2DPoint; // Creates an alias “2DPoint” for // “struct s2DPoint” 2DPoint* poBar = (2DPoint*) malloc( sizeof(2DPoint)); // Allocates space for a 2DPoint struct // on the heap; poBar points to it poBar->iX = 2; // “->” operator dereferences the // pointer and accesses the field iX; // equivalent to (*poBar).iX = 2; 9/26/1319

Memory management  Allocate memory on the heap: void* malloc(size_t size);  Note: malloc may fail!  But not necessarily when you would expect…  Use sizeof() operator to get the size of a type/struct  Free memory on the heap: void free(void* ptr);  Pointer argument comes from previous malloc() call 9/26/1320

Common C pitfalls (1)  What’s wrong and how can it be fixed? char* city_name(float fLat, float fLong) { char sName[100];... return sName; } 9/26/1321

Common C pitfalls (1)  Problem: returning pointer to local (stack) memory (also: using floats)  Solution: allocate on heap char* city_name(double fLat, double fLong) { // Preferably allocate a string of // just the right size char* sName = (char*) malloc(100*sizeof(char));... return sName; } 9/26/1322

Common C pitfalls (2)  What’s wrong and how can it be fixed? char* sBuf = (char*) malloc(32*sizeof(char)); strcpy(sBuf, argv[1]); 9/26/1323

Common C pitfalls (2)  Problem: potential buffer overflow  Solution: static const int BUFFER_SIZE = 32; char* sBuf = (char*) malloc(BUFFER_SIZE); strncpy(sBuf, argv[1], BUFFER_SIZE);  Why are buffer overflow bugs dangerous? 9/26/1324

Common C pitfalls (3)  What’s wrong and how can it be fixed? char* sBuf = (char*) malloc(BUFFER_SIZE); Strncpy(sBuf, sHello, BUFFER_SIZE); printf("%s\n", sBuf); sBuf = (char*) malloc(2*BUFFER_SIZE); strncpy(sBuf, sLongHello, 2*BUFFER_SIZE); printf("%s\n", sBuf); free(sBuf); 9/26/1325

Common C pitfalls (3)  Problem: memory leak  Solution: char* sBuf = (char*) malloc(BUFFER_SIZE); strncpy(sBuf, sHello, BUFFER_SIZE); printf("%s\n", sBuf); free(sBuf); buf = (char*) malloc(2*BUFFER_SIZE);... 9/26/1326

Common C pitfalls (4)  What’s wrong (besides ugliness) and how can it be fixed? char sFoo[2]; sFoo[0] = 'H'; sFoo[1] = 'i'; printf("%s\n", sFoo); 9/26/1327

Common C pitfalls (4)  Problem: string is not NULL-terminated  Solution: char sFoo[3]; sFoo[0] = 'H'; sFoo[1] = 'i'; sFoo[2] = '\0'; printf("%s\n", sFoo);  Easier way: char* sFoo = "Hi”; 9/26/1328

Common C pitfalls (5)  Another bug in the previous examples?  Not checking the return value of system calls / library calls! char* sBuf = (char*) malloc(BUFFER_SIZE); if (sBuf == 0) { fprintf(stderr, "error!\n"); return EXIT_FAILURE; } strncpy(sBuf, argv[1], BUFFER_SIZE);... 9/26/1329

Project 0  Description is on course web page  Due Friday October 4, 11:59pm  Work individually  Remaining projects are in groups of 2. When you have found a partner, one of you should fill out the survey on Catalyst (forthcoming by ) 9/26/1330

Project 0 goals  Get re-acquainted with C programming  Practice working in C / Linux development environment  Create data structures for use in later projects 9/26/1331

Valgrind  Helps find all sorts of memory problems  Lost pointers (memory leaks), invalid references, double frees  Simple to run:  valgrind./myprogram  Look for “definitely lost,” “indirectly lost” and “possibly lost” in the LEAK SUMMARY  Manual:  9/26/1332

Project 0 memory leaks  Before you can check the queue for memory leaks, you should probably add a queue destroy function: void queue_destroy(queue* q) { queue_link* cur; queue_link* next; if (q != NULL) { cur = q->head; while (cur) { next = cur->next; free(cur); cur = next; } free(q); } 9/26/1333

Project 0 testing  The test files in the skeleton code are incomplete  Make sure to test every function in the interface (the.h file)  Make sure to test corner cases  Suggestion: write your test cases first 9/26/1334

Project 0 tips  Part 1: queue  First step: improve the test file  Then, use valgrind and gdb to find the bugs  Part 2: hash table  Write a thorough test file  Perform memory management carefully  You’ll lose points for:  Leaking memory  Not following submission instructions  Use the discussion board for questions about the code 9/26/1335