just a ((somewhat) safer) dialect.

Similar documents
just a ((somewhat) safer) dialect.

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

PRINCIPLES OF OPERATING SYSTEMS

Processes. Johan Montelius KTH

0x0d2C May your signals all trap May your references be bounded All memory aligned Floats to ints round. remember...

Kurt Schmidt. October 30, 2018

A process. the stack

Procedures, Parameters, Values and Variables. Steven R. Bagley

Class Information ANNOUCEMENTS

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Final CSE 131B Spring 2004

Programming refresher and intro to C programming

F28HS2 Hardware-Software Interface. Lecture 1: Programming in C 1

CSE 374 Programming Concepts & Tools

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

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

Arrays and Pointers (part 2) Be extra careful with pointers!

My malloc: mylloc and mhysa. Johan Montelius HT2016

Memory Management: The process by which memory is shared, allocated, and released. Not applicable to cache memory.

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Topic 6: A Quick Intro To C

CS 61C: Great Ideas in Computer Architecture Introduction to C

Supporting Class / C++ Lecture Notes

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

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Lecture 03 Bits, Bytes and Data Types

QUIZ. What is wrong with this code that uses default arguments?

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

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

Computer Systems Assignment 2: Fork and Threads Package

CS201 - Introduction to Programming Glossary By

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Arrays and Pointers (part 2) Be extra careful with pointers!

A brief introduction to C programming for Java programmers

CSE 124 Discussion (10/3) C/C++ Basics

Short Notes of CS201

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

Lecture 2, September 4

CSE 303 Lecture 8. Intro to C programming

COMP 2355 Introduction to Systems Programming

Threads Tuesday, September 28, :37 AM

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

CS Programming In C

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

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

A Fast Review of C Essentials Part I

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Problem Set 1: Unix Commands 1

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

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. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

C++ for Java Programmers

C Programming Review CSC 4320/6320

Programming. Data Structure

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

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

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Lectures 5-6: Introduction to C

Lecture 04 Introduction to pointers

BLM2031 Structured Programming. Zeyneb KURT

The C language. Introductory course #1

Fundamental of Programming (C)

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Summer 2016 Programming Assignment 1 Introduction The purpose of this

Lectures 5-6: Introduction to C

CS61, Fall 2012 Section 2 Notes

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

Programming in C First meeting

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

211: Computer Architecture Summer 2016

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20


C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

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

CS349/SE382 A1 C Programming Tutorial

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Programming Tools. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore

Dynamic memory allocation (malloc)

CS3157: Advanced Programming. Outline

ECE 250 / CPS 250 Computer Architecture. C Programming

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Lecture 2: C Programm

CS240: Programming in C

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

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

5) Attacker causes damage Different to gaining control. For example, the attacker might quit after gaining control.

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

ECE 250 / CS 250 Computer Architecture. C Programming

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

Process life and death. Tuesday, September 28, 2010

Transcription:

Intro_to_C Page 1 Intro to C Tuesday, September 07, 2004 5:30 PM C was developed specifically for writing operating systems Low level of abstraction. "Just above machine language." Direct access to the machine when desired. Highly efficient. Has two predecessors and one successor: A: assembler. B: block program structure (but no block data). C: block structure and block data. (D: adds memory descriptors) C++ is not "more powerful" than C; it is just a ((somewhat) safer) dialect.

An old adage: Tuesday, September 15, 2009 3:27 PM C programmers know the cost of everything but the value of nothing. Lisp programmers know the value of everything but the cost of nothing. Intro_to_C Page 2

Intro_to_C Page 3 Speed and danger Tuesday, September 07, 2004 5:30 PM Speed and danger C is an "unsafe" language. No bounds checking on arrays No type checking during subroutine calls. Pointers (at whatever cost) Computed gotos (using function pointers). Why? Speed is more important than safety.

Intro_to_C Page 4 C design principles Tuesday, September 15, 2009 1:40 PM C design principles Trust the programmer. Speed over correctness. No run-time checking. Information hiding by not exposing structure.

Intro_to_C Page 5 A typical C program Tuesday, September 15, 2009 1:45 PM #include <stdio.h> int main(int argc, char **argv, char **envp) { printf("hello\n"); exit(0); }

Intro_to_C Page 6 Annotation of the program Thursday, September 09, 2004 3:13 PM #include <stdio.h> /* include a header file "stdio.h" containing reusable declarations */ int main(int argc, char **argv, char **envp) /* program is always called "main". argc is number of arguments on the command line, argv is an array of arguments (as strings). envp is an array of environment variables. */ { printf("hello\n"); /* call the library function "printf" */ } exit(0); /* stop running with "success" code: system call! */

Intro_to_C Page 7 Compiling a C program Tuesday, September 15, 2009 1:43 PM To compile a C program "hello.c", on comp111 gcc -g -c hello.c # creates hello.o, a relocatable object file gcc -o hello hello.o # creates hello from hello.o./hello # run the program Or, alternatively gcc -g hello.c # means gcc -o a.out -g hello.c

Intro_to_C Page 8 List of things to learn from 40 Monday, October 2, 2017 12:08 PM Using the debugger: gdb or xxgdb to diagnose a core dump. to single-step a program. How to debug memory allocation problems (malloc and free) what can happen? how do you find it? How caches work, and how they affect performance

Intro_to_C Page 9 Using gdb Monday, October 2, 2017 12:11 PM gcc -g file.c gdb a.out run run arg-list break location print variable exit gdb a.out core.something exhibits the situation during a core dump. Allows you to print values

Caches Monday, October 2, 2017 12:27 PM The big example from 40 that we will constantly use: the effects upon caches upon runtime A cache is a copy of memory used because it can be accessed more quickly. Facts about caches limited size updated at page level. pages can be "clean" or "dirty" A "dirty page" must be written back to memory. A "clean page" can be deleted from cache without writing to memory. The performance of a program is based upon its data locality: the extent to which it manipulates data in a local region so that it stays in cache. Intro_to_C Page 10

Intro_to_C Page 11 Debugging memory allocation Monday, October 2, 2017 12:16 PM valgrind: simulates malloc and free keeps detailed notes about what is legal. stops a program if it tries to do something illegal with memory.

Intro_to_C Page 12 Printing Tuesday, September 15, 2009 1:59 PM printf("format", arguments ) is general printing statement. "format" represents a string, which is an array of characters. "string" a string 's' a character "format" can contain: %d: print an integer %f: print a floating point number %s: print a string %%: print a percent character(!) \n: print a carriage return printf("one is %d\n",1); => "one is 1\n" printf("two point zero is %f\n",2.0); => "two point zero is 2.0" printf("these are %d, %f\n", 1, 2.0); man printf tells all.

Printing one's arguments Tuesday, September 15, 2009 2:19 PM #include <stdio.h> int main(int argc, char **argv, char **envp) { int i; printf("arguments\n"); for (i=0; i<argc; i++) printf("argument %d is '%s'\n",i,argv[i]); printf("environment\n"); for (i=0; envp[i]!=null; i++) printf( "environment variable %d is '%s'\n",i,envp[i]); } argv[0]-argv[argc-1] are the command-line arguments. envp[0]-... are the environment variables. envp[i]==0 means stop listing! Intro_to_C Page 13

Intro_to_C Page 14 The linux manual pages Tuesday, September 15, 2009 2:03 PM printf is not part of C. Instead, it is a library function that is documented in the linux manual pages. From the linux command line, type man printf or man 3 printf # check in section 3: the C library to learn about it.

Intro_to_C Page 15 Another simple program Tuesday, September 15, 2009 2:28 PM #include <stdio.h> int main(int argc, char **argv, char **envp) { int pid = fork(); /* a system call */ if (pid!=0) { printf("i'm the parent\n"); sleep(5); /* library */ printf("killing pid %d\n", pid); kill(pid,9); /* kill my child! system call */ printf("killed pid %d\n", pid); } else { /* I'm the child: wait to die! */ int count = 0; printf("i'm the child!\n"); while(1) { sleep(1); ++count; printf("i've been around for %d seconds\n",count); } } exit(0); }

Explanation of the program Tuesday, September 15, 2009 2:28 PM Explanation of the program fork(): split into two processes. sleep(5): sleep for five seconds. kill(pid,9): send a kill signal (9) to the process numbered pid. For more details man 2 kill man 2 fork man 3 sleep kill is a system call: these are documented in section 2 of the manual. Note that man 1 kill or man kill describes the kill command, which is different. Really useful: man -k thing returns a list of all manual pages whose title mentions thing. Intro_to_C Page 16

General caveats Tuesday, September 15, 2009 2:42 PM There are three things to know in programming in C: the language C itself how to use the available system calls (man 2 x) how to use the available library functions (man 3 x) Using a function read the man page. #include the appropriate headers (e.g., <stdio.h> for printf) Call the function with the appropriate arguments (e.g., printf("%d\n",i)) Intro_to_C Page 17

Intro_to_C Page 18 Watch out Tuesday, September 15, 2009 2:45 PM C is not type-safe Note that printf("%f",2.1); prints what you would expect, while printf("%d",2.1); prints (predictable but useless) garbage. Why? The second tries to print (the first half of) a double-precision number as an integer, which doesn't work the way one might expect!

Intro_to_C Page 19 Lack of polymorphism Tuesday, September 14, 2010 4:00 PM C is not polymorphic foo(int) and foo(float) are the same function. You must distinguish between argument types!

Watch out: logical equivalence Tuesday, September 15, 2009 3:14 PM Note that an expert C programmer is always conscious of speed of execution. Note that while(x!=0) { printf("x is %d\n",x); x=x-1; } means exactly the same thing as while(x) { printf("x is %d\n", x); --x; } but the second is a bit faster. An expert would not write the first version! You will commonly see very terse C in OS code. One can: test for whether a pointer is NULL (0) by treating it as boolean! test for whether an integer is 0 by treating it as boolean! etc. const char *p; p = "hello\n"; const char *q; q=p; while (*q) { printf("%c",*q); q++; } The optimizer debate: "New school" programmers contend that this is handled by the optimizer. "Old school" programmers are more explicit and do not rely upon that. Intro_to_C Page 20

Intro_to_C Page 21 Watch out: value of a statement Tuesday, September 15, 2009 3:22 PM Each statement has a value. x=1 has value 1 x=y+z has the value of x after the assignment. ++x has the value of x after increment x++ has the value of x before increment E.g. x=1 y=(x++) sets x to 2 and y to 1! Reason for this: loop optimization. Consider: x=0; while(x<20) a[x++]=0; This sets elements a[0] to a[19] to 0, but stops before element 20. Equivalent to: x=0; while (x<20) { a[x]=0; x=x+1; } but the former avoids a register reload.

Structures Thursday, September 09, 2004 3:13 PM C structures Concatenation of basic types Example: struct foo { int i; double d; } s; ANSI standard layout: "Lay out structure by ascending address, but align each type on a multiple of its length." Intro_to_C Page 22

Addressing structures Tuesday, September 15, 2009 5:29 PM struct foo x; x.i=4; x.d=5.6; struct foo *p = &x; p->i=7; Intro_to_C Page 23

Intro_to_C Page 24 C has no classes Tuesday, September 13, 2011 2:56 PM C has no classes Structs: the data part of a class. The function part requires polymorphism, which C doesn't have. You will see OS programmers "growing their own classes". Ex: struct foo { } ; struct foo *foo_create(); void foo_destroy(struct foo *); void foo_set(struct foo *, int i);.

Intro_to_C Page 25 Unions Thursday, September 09, 2004 3:13 PM Unions Allow one to create aliases for the same kind of memory. Example union g { int i; double d; } t; t.i contains the first eight bytes of t.d Very often, in programming an OS, we need to decide on the fly what kind of data a block contains. Unions allow us to define things that can represent multiple kinds of things, very efficiently. struct discriminated_union { enum {WROTE_I, WROTE_D} discriminant; }; union g { int i; double d; } t;

Intro_to_C Page 26 Information Hiding in C Wednesday, September 27, 2017 11:38 AM There are several mechanisms for information hiding in C that are different than mechanisms in C++ 1. void *: hide the whole type. 2. The "prefix rule": hide part of a type

Intro_to_C Page 27 void * Wednesday, September 27, 2017 11:40 AM Remember that a pointer type determines lots of things about the pointer: What *p means What p+1 means These are relative to the size of the type Some times, we don't want to tell the function anything at all about the data. For this reason, we point to that data with a "pointer to void" void *p This gets complicated really fast, as we will see in the next lecture: Type is void * from the point of view of the operating system. Type is something tangible from the point of view of the program Type is again void * whenever the operating system has to do something with it. There is a really good reason that malloc returns void * and free takes an argument that is void *.

They do not want to know about the actual type of the data. Intro_to_C Page 28

The prefix rule Thursday, September 09, 2004 3:13 PM The Prefix Rule If one structure is a prefix of another, the two structures are organized in memory identically for the common prefix. Example: struct foo { int i; double d; } s; struct foo2 { int i; double d; float f; } u; Intro_to_C Page 29

Intro_to_C Page 30 Information hiding using the prefix rule Thursday, September 09, 2004 3:13 PM C Information Hiding If you want to hide something about a struct, give the user a pointer to a prefix of the struct; leave other data unavailable. Example: struct foo2 h; struct foo *j = (struct foo *) &h; return j; Unfortunately, I can get around this: If j is as above, then *((float *)((double *)(((int *)j)+2))+1)) refers to the element we tried to hide.

Intro_to_C Page 31 A rather subtle fact Wednesday, September 27, 2017 11:47 AM The prefix rule is exactly what enables C++ subclassing. The prefix is the superclass. The whole structure is the subclass.