CSE 451: Operating Systems Section 1. Why are you here? 9/30/102.

Slides:



Advertisements
Similar presentations
Data Structure & Abstract Data Type
Advertisements

Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Dynamic Memory Allocation I Topics Simple explicit allocators Data structures Mechanisms Policies CS 105 Tour of the Black Holes of Computing.
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
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:
Informática II Prof. Dr. Gustavo Patiño MJ
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
CSE 451: Operating Systems Section 3 Project 0 recap, Project 1.
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.
Pointers and Arrays C and Data Structures Baojian Hua
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
C and Data Structures Baojian Hua
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Memory Layout C and Data Structures Baojian Hua
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CSE 451: Operating Systems Section 1 Intro, C programming, project 0.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2011.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
C Programming - Lecture 6 This lecture we will learn: –Error checking in C –What is a ‘wrappered function’? –What is a clean interface? –How to earn your.
Exploiting Buffer Overflows on AIX/PowerPC HP-UX/PA-RISC Solaris/SPARC.
Computer Science Detecting Memory Access Errors via Illegal Write Monitoring Ongoing Research by Emre Can Sezer.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Stack and Heap Memory Stack resident variables include:
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.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
Lecture Contents Arrays and Vectors: Concepts of pointers and references. Pointer declarations and initialization. Pointer Operators. Dynamic Memory Allocation.
Topic 2d High-Level languages and Systems Software
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.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
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.
CSE 451: Operating Systems Section 1 Intro, C programming, project 0.
Computer Graphics 3 Lecture 1: Introduction to C/C++ Programming Benjamin Mora 1 University of Wales Swansea Pr. Min Chen Dr. Benjamin Mora.
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.
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 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the.
Carnegie Mellon 1 Malloc Lab : Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.
CSE 333 – SECTION 1 C, Introduction to and Working with.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
CSE 451 Section 1 C Refresher (or introduction?) Project 0.
Object Oriented Programming Lecture 2: BallWorld.
Heap Overflows. What is a Heap? malloc(), free(), realloc() Stores global variables Automatic memory allocation/deallocation Allocated at runtime Implemented.
Homework Notes from Sean
Memory-Related Perils and Pitfalls in C
YongChul Kwon CSE451 Section 1: Spring 2006 YongChul Kwon
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
CSE 374 Programming Concepts & Tools
Day 03 Introduction to C.
CSE 451 C refresher.
malloc(): Dynamic Memory Management
Day 03 Introduction to C.
Checking Memory Management
C Basics.
CSCI206 - Computer Organization & Programming
The Hardware/Software Interface CSE351 Winter 2013
Memory Management III: Perils and pitfalls Mar 13, 2001
Memory Allocation CS 217.
CSE451 Fall 2008 Section 1 Roxana Geambasu
Pointers, Dynamic Data, and Reference Types
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

CSE 451: Operating Systems Section 1

Why are you here? 9/30/102

Why are you here?  Because you want to work for Microsoft and hack on the Windows kernel? 9/30/103

Why are you here?  Because you want to work for Microsoft and hack on the Windows kernel?  Because it fulfills a requirement and fits your schedule? 9/30/104

Who cares about operating systems?  Operating systems techniques apply to all other areas of computer science  Data structures; caching; concurrency; virtualization… 9/30/105

Who cares about operating systems?  Operating systems techniques apply to all other areas of computer science  Data structures; caching; concurrency; virtualization…  Operating systems support all other areas of computer science 9/30/106

Who are we?  Peter Hornyack  Abdul Salama 9/30/107

Who are we?  Peter Hornyack  Abdul Salama  What do we know? 9/30/108

Who are we?  Peter Hornyack  Abdul Salama  What do we know?  Why are we here? 9/30/109

What is this section for?  Projects  Questions!  Extensions beyond lecture / textbook material 9/30/1010

Office Hours 9/30/1011

Outline  Introduction  C vs. Java  C language “features”  C pitfalls  Project 0 9/30/1012

Motivation: Why C?  Why not write OS in Java? 9/30/1013

Motivation: Why C?  Why not write OS in Java?  Interpreted Java code runs in VM; what does VM run on? 9/30/1014

Motivation: Why C?  Why not write OS in Java?  Interpreted Java code runs in VM; what does VM run on?  Precision:  Instructions  Timing  Memory 9/30/1015

C vs. Java: Compilation Java  Packages “import java.xyz” .class files  jar program .jar files C  Header files “#include xyz.h” .o files  linker program  Executable files  libc 9/30/1016

C vs. Java: Constructs Java  Classes  Public or private members  Methods  Instantiated with class, or may be static  References C  Structures  All members “public”  Functions  Implicitly “static”  Pointers 9/30/1017

C Language Features  Pointers  Pass-by-value vs. pass-by-reference  Structures  Typedefs  Explicit memory management 9/30/1018

Pointers int a = 5; int b = 6; int *pa = &a;// declares a pointer to a // with value as the // address of a *pa = b;// changes value of a to b // (a == 6) pa = &b;// changes pa to point to // b’s memory location (on // stack) 9/30/1019

Function Pointers int some_fn(int x, char c) {... } // declares and defines a function int (*pt_fn)(int, char) = NULL; // declares a pointer to a function // that takes an int and a char as // arguments and returns an int pt_fn = &some_fn; // assigns pointer to some_fn()’s // location in memory int a = (*pt_fn)(7, ‘p’); // sets a to the value returned by // some_fn(7, ‘p’) 9/30/1020

Pointer Arithmetic  Array variables are really just pointers: int foo[2];// foo is a pointer to the // beginning of the array *(foo+1) = 5;// the second int in the // array is set to 5  Don’t use pointer arithmetic unless you have a good reason to 9/30/1021

Pass-By-Value vs. Pass-By- Reference int doSomething(int x) { return x+1; } void doSomethingElse(int *x) { *x += 1; } void foo() { int x = 5; int y = doSomething(x); // x==5, y==6 doSomethingElse(&x); // x==6, y==6 } 9/30/1022

Structures struct foo_s {// defines a type that int x;// is referred to as a int y;// “struct foo_s”. };// don’t forget this ; struct foo_s foo;// declares a struct // on the stack foo.x = 1;// sets the x field // of the struct to 1 9/30/1023

Typedefs typedef struct foo_s *foo_t; // creates an alias “foo_t” for // pointer to foo_s struct foo_t new_foo = (foo_t)malloc(sizeof(struct foo_s)); // allocate a foo_s struct on the // heap; new_foo points to it new_foo->x = 2; // “->” operator dereferences the // pointer and accesses the field x 9/30/1024

Explicit Memory Management  Allocate memory on the heap: void *malloc(size_t size);  Note: may fail!  Use sizeof() operator to get size  Free memory on the heap: void free(void *ptr);  Pointer argument comes from previous malloc() call 9/30/1025

Common C Pitfalls (1)  What’s wrong and how to fix it? char* city_name(float lat, float long) { char name[100];... return name; } 9/30/1026

Common C Pitfalls (1)  Problem: returning pointer to local (stack) memory  Solution: allocate on heap char* city_name(float lat, float long) { char* name = (char*)malloc(100*sizeof(char));... return name; } 9/30/1027

Common C Pitfalls (2)  What’s wrong and how to fix it? char* buf = (char*)malloc(32); strcpy(buf, argv[1]); 9/30/1028

Common C Pitfalls (2)  Problem: potential buffer overflow  Solution: int buf_size = 32; char* buf = (char*)malloc(buf_size*sizeof(char)); strncpy(buf, argv[1], buf_size);  Why are buffer overflow bugs important? 9/30/1029

Common C Pitfalls (3)  What’s wrong and how to fix it? char* buf = (char*)malloc(32); strncpy(buf, “hello”, 32); printf(“%s\n”, buf); buf = (char*)malloc(64); strncpy(buf, “bye”, 64); printf(“%s\n”, buf); free(buf); 9/30/1030

Common C Pitfalls (3)  Problem: memory leak  Solution: char* buf = (char*)malloc(32); strncpy(buf, “hello”, 32); printf(“%s\n”, buf); free(buf); buf = (char*)malloc(64); … 9/30/1031

Common C Pitfalls (4)  What’s wrong (besides ugliness) and how to fix it? char foo[2]; foo[0] = ‘H’; foo[1] = ‘i’; printf(“%s\n”, foo); 9/30/1032

Common C Pitfalls (4)  Problem: string is not NULL-terminated  Solution: char foo[3]; foo[0] = ‘H’; foo[1] = ‘i’; foo[2] = ‘\0’; printf(“%s\n”, &foo);  Easier way: char *foo = “Hi”; 9/30/1033

Project 0  Description is on course web page now  Due Friday October 8, 11:59pm  Work individually  Remaining projects in groups of 3 9/30/1034

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/30/1035

Project 0 Tips  Try these tools:  man pages  valgrind  gdb  Write your test cases first 9/30/1036

Project 0 Tips  Part 1: queue  To find bugs, try valgrind, then gdb  Part 2: hash table  Perform memory management carefully  Check using valgrind 9/30/1037

9/30/1038