CSC C69: OPERATING SYSTEMS

Similar documents
CS61C : Machine Structures

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

EL2310 Scientific Programming

In Java we have the keyword null, which is the value of an uninitialized reference type

EL2310 Scientific Programming

CS61C : Machine Structures

EL2310 Scientific Programming

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Section Notes - Week 1 (9/17)

CSC 1600 Memory Layout for Unix Processes"

Intermediate Programming, Spring 2017*

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

Page 1. Today. Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right? Compiler requirements CPP Volatile

[0569] p 0318 garbage

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Computer Organization & Systems Exam I Example Questions

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Important From Last Time

Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right?

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Quiz 0 Review Session. October 13th, 2014

Important From Last Time

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

CS61C : Machine Structures

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Programming refresher and intro to C programming

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Lecture 8 Dynamic Memory Allocation

A brief introduction to C programming for Java programmers

Fall 2018 Discussion 2: September 3, 2018

CS 11 C track: lecture 5

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

CMSC 341 Lecture 2 Dynamic Memory and Pointers

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Arrays and Memory Management

CS349/SE382 A1 C Programming Tutorial

Arithmetic and Bitwise Operations on Binary Data

Limitations of the stack

High Performance Computing in C and C++

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

DAY 3. CS3600, Northeastern University. Alan Mislove

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

CS61C : Machine Structures

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Data III & Integers I

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

377 Student Guide to C++

Lectures 13 & 14. memory management

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

PRINCIPLES OF OPERATING SYSTEMS

CS61C : Machine Structures

Lecture Notes on Types in C

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

CA31-1K DIS. Pointers. TA: You Lu

Lecture 3: C Programm

o Code, executable, and process o Main memory vs. virtual memory

18-642: Code Style for Compilers

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

Recitation #11 Malloc Lab. November 7th, 2017

Contents of Lecture 3

Writing Functions in C

Hacking in C. Memory layout. Radboud University, Nijmegen, The Netherlands. Spring 2018

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

HW1 due Monday by 9:30am Assignment online, submission details to come

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

Programming in C. Lecture 9: Tooling. Dr Neel Krishnaswami. Michaelmas Term

CS 61c: Great Ideas in Computer Architecture

Heap Arrays. Steven R. Bagley

CS 11 C track: lecture 6

COMP 2355 Introduction to Systems Programming

Lectures 5-6: Introduction to C

Lecture 07 Debugging Programs with GDB

EL6483: Brief Overview of C Programming Language

More C Pointer Dangers

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

ECE 15B COMPUTER ORGANIZATION

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

Data III & Integers I

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

CS61C : Machine Structures

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Heap Arrays and Linked Lists. Steven R. Bagley

Data III & Integers I

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Lecture 2: C Programm

Transcription:

CSC C69: OPERATING SYSTEMS Tutorial 1 Thursday, Jan 17, 2013 TA: Ioan Stefanovici (ioan@cs.toronto.edu)

HOW DO YOU SUCCEED IN THIS COURSE? Show up to lectures & tutorials (way too much material) Work on assignments evenly (fill in the other partner...) Compiler warnings! (-10% automatic deduction since B09) svn add files to the repository! Do a complete, clean check-out (especially for OS/161...) Read assignments very closely Lots of details, corner cases, and design decisions to make Keep things modular (especially in OS/161) Use tools & resources available to you and be proactive (good for industry as well) Design documents (more than line-by-line description of your code; explain, don't regurgitate)

SOME C REVIEW Go through these slides (and try the exercises...) at home! Brush up / learn what you don t know now! Assignments are work-intensive enough as it is... Topics: Bit manipulations, pointers, argument- passing, arrays, pointer arithmetic, memory allocation, error handling, etc.

C: BIT MANIPULATION Sometimes we need to alter bits in a byte or word of memory directly A 32-bit int is a very compact way to represent 32 different booleans C provides bitwise boolean operators & : AND : OR ~ : NOT ^ : XOR (exclusive OR) These are NOT the same as the logical operations

PRACTICE WITH BIT OPS a 0110 1001 b 0101 0101 ~a ~b a & b a b a ^ b

BIT MASKS A mask is a bit pattern that indicates a set of bits in a word e.g.: 0xFF would represent the least significant byte of a word Happily, this is true regardless of byte ordering For a mask of all 1s, the best way is ~0 Portable, not dependent on word size For 32-bit machines, 0xFFFFFFFF will work You may also see -1 used (2s complement, -1 is a bit pattern will all bits set to 1)

PRACTICE WITH BIT MASKS Given an integer x, write C expressions for: Least significant byte of x, all other bits set to 1 int y = Complement of the least significant byte of x, all other bytes unchanged int y = All but least significant byte of x, with least significant byte set to 0 int y =

BIT SHIFTING x << k: shift the bits of x by k bits to the left, dropping the k most significant bits and filling the rightmost (least significant) k bits with 0s What if k is >= size of object? (e.g., for int s, on 32-bit machine, k >= 32) UNDEFINED! Don t assume result will be 0 x >> k: right shift, logical or arithmetic Logical: fill left end with k 0s (unsigned types) Arithmetic: fill left end with k copies of the most significant bit (sign bit) C does not define when arithmetic shifts are used! Typically used for signed data, but not portable

POINTERS Every variable has a memory address Can be accessed with address of operator: & Pointers are variables that store memory addresses int x = 42; int *x_ptr = &x; int *heap_ptr = (int *)malloc(sizeof(int)); The value a pointer refers to can be accessed with * This is dereferencing int y = *x_ptr;

NULL NULL is the 0 value for addresses. It s a good idea to initialize pointers to NULL. Much easier to catch bugs! It s often used as an error value, too.

PASS BY VALUE/REFERENCE C only allows one value (which may be a struct) to be returned. If variables are passed into a function by value, any changes to them will not be seen outside the function. Why? A copy of each parameter is made on the stack, and changes are made to the copy. If pointers are passed into a function, any changes made to the value they point to will be seen outside the function -- this is passing by reference. Note that the pointers themselves are still passed by value!

ARRAYS Arrays contain multiple variables of the same type. Each element can be accessed with the [] notation int x_arr[10]; for (i = 0; i < 10; i = i+1) x_arr[i]=i; Arrays are... just pointers. int *x_ptr = x_arr; But they re often easier to reason about. Use them!

POINTER ARITHMETIC Pointers are just values, so you can manipulate them. If x is an array, this is true: x[5] == *(x+5) The key? Constants are added to pointers scaled by the size of the type. Adding 5 to an (int *) adds 5*sizeof(int). And also, strangely, this is true (on most systems): 5[x] == x[5] (but don t use it...)

ALLOCATING MEMORY malloc allocates memory from the heap It allocates by byte, so it requires a size Its return value must be typecast int *heap_ptr = (int *)malloc(sizeof(int)*4); Don t forget to free the memory you malloc! Remember to use kernel versions of the calls if you re working inside the kernel Instead of malloc, kmalloc Instead of free, kfree

POINTERS AND STRUCTS Structs are one aggregate structure in C. A struct can contain multiple variables in a single package Structs have a syntactic quirk: If you have a struct variable, use. struct mystruct s =... ; s.myfield = 6; If you have a struct pointer, use -> struct mystruct *s_ptr =... ; s_ptr->myfield = 6; (*s_ptr).myfield = 6;

C ERROR MESSAGES Segmentation Fault A pointer has accessed a location in memory that is not in a segment you own. Maybe an infinite loop: overran an array? Adding two pointers that shouldn t be? Bus error A pointer is not properly alligned Bad casting? Incorrect pointer arithmetic?

MORE C QUIRKS TO REMEMBER Uninitialized variables have undermined value...and C will let you use them (good idea to initialize variables to default values; e.g.: pointers to NULL) Always check return values...to make sure an error has not occured Return values flag errors -- no exceptions C has no runtime checks Casts are dangerous, but you ll need some Arrays don t store their size -- you can easily run over Memory can be corrupted without the program crashing! check your bounds!

STACK ALLOCATION Stack allocate! Heap allocation isn t always necessary Also, might cause a memory leak (if not careful...) int foo(){ struct mystruct z; s.x = 1; return funcwithmystruct(&z); }...NOT int foo(){ struct mystruct* z = malloc(sizeof(struct mystruct)); int rval = -1; z->x = 1; rval = funcwithmystruct(z); free(z); return rval; }

DON T LEAK MEMORY! Make sure to free memory you allocate This example shows an error case struct mystruct* sys_mystruct(){ struct mystruct* first; first = malloc(sizeof(struct mystruct)); if (first == NULL){ return -1; } first.other = malloc(sizeof(struct otherstruct)); if (first.other == NULL){ free(first); return -1; } return first; }

GENERAL TIPS Simplify whenever possible struct mystruct myarray[10][10]; is better than struct mystruct **myarray; Declare all functions ahead of time Use a test-oriented incremental development strategy Test first and frequently

ASSIGNMENT 1 Implement a shell printf vs. write call fork & wait Linked lists