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

Similar documents
PRINCIPLES OF OPERATING SYSTEMS

Lectures 5-6: Introduction to C

C Programming Review CSC 4320/6320

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

Lectures 5-6: Introduction to C

Kurt Schmidt. October 30, 2018

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Short Notes of CS201

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

CS201 - Introduction to Programming Glossary By

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes

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

CSE 333 SECTION 3. POSIX I/O Functions

CSCI 171 Chapter Outlines

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

Recitation: Cache Lab & C

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

C Syntax Out: 15 September, 1995

CSE 333 Lecture 7 - final C details

Model Viva Questions for Programming in C lab

Course organization. Part I: Introduction to C programming language (Week 1-12) Chapter 1: Overall Introduction (Week 1-4)

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

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

CSE 333 SECTION 3. POSIX I/O Functions

CS3157: Advanced Programming. Outline

Computational Methods of Scientific Programming Fall 2007


SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

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

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

CS 261 Fall Mike Lam, Professor. Structs and I/O

Processes. Johan Montelius KTH

CSE 374 Programming Concepts & Tools

A process. the stack

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

CS240: Programming in C

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

CMPSC 311- Introduction to Systems Programming Module: Introduction to C

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

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

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

ECE 250 / CS 250 Computer Architecture. C Programming

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

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

edunepal_info

Have examined process Creating program Have developed program Written in C Source code

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

C programming basics T3-1 -

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

Contents. A Review of C language. Visual C Visual C++ 6.0


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

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

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Introduction to C. Contents. David Chisnall. 1 Introduction Brief History of C Key Features Why Learn C?...

Section 3: File I/O, JSON, Generics. Meghan Cowan

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)

Programming in C First meeting Tiina Niklander

The C Pre Processor ECE2893. Lecture 18. ECE2893 The C Pre Processor Spring / 10

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

Topic 6: A Quick Intro To C

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

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

PROGRAMMAZIONE I A.A. 2017/2018

CSCI 2132 Final Exam Solutions

Lecture 03 Bits, Bytes and Data Types

MPATE-GE 2618: C Programming for Music Technology. Syllabus

Chapter 11 Introduction to Programming in C

ECE264 Spring 2014 Exam 2, March 11, 2014

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

Programming in C week 1 meeting Tiina Niklander

A Crash Course in C. Steven Reeves

CSE 333 Lecture 2 - gentle re-introduction to C

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

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

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

The Waite Group's. New. Primer Plus. Second Edition. Mitchell Waite and Stephen Prata SAMS

Princeton University COS 333: Advanced Programming Techniques A Subset of C90

ECE 250 / CPS 250 Computer Architecture. C Programming

Final CSE 131B Spring 2004

Programming in C First meeting

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

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

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

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

EL2310 Scientific Programming

Chapter 1 Getting Started Structured Programming 1

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Approximately a Test II CPSC 206

CS240: Programming in C

A Fast Review of C Essentials Part I

Chapter 11 Introduction to Programming in C

Transcription:

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

Why C? Fast (good optimizing compilers) Not too high-level (Java, Python, Lisp) Not too low-level (Assembly) Easy manipulation of bits, bytes, words, and pointers Explicit memory allocation Most OS kernels written in C with some assembly 2

C Basics Comments /* comment here */ // comment to end of line File structure Header files (.h) C files (.c) Blocks enclosed in braces {} Statements terminated with semicolon ; 3

C Types and Functions Types char, unsigned char, int unsigned int float, double arrays, structs, pointers, enum Functions Arguments Local variables Return value 4

C Operators Bitwise operators &,,~ Relational operators <,>,<=,>=,==,!= Use parenthesis to force evaluation order if ( (~(a&b)) == c) then { x = 1; } 5

Control If / else / else if for (x = 1, x < N, x++) { /* code */ } while (x > 1) { /* code */ } switch / case / default goto 6

C Practice All arguments to functions are passed by value Use pointers to pass by reference Explicit memory allocation with malloc() and free() Structs with pointers to build linked data structures Pointers to functions Macros for speed Globals variables are not always bad New types with typedef 7

The C Library The standard C library (libc) Strings (strcpy, strlen, etc.) Buffered I/O (fopen, fread, fwrite, etc.) Math (sin, cos, pow, etc.) Others Use man 3 name to get documentation 8

See lec01/src/args.c 9 Command Line Arguments C and UNIX define a way to pass arguments to a program int main(int argc, char *argv[]) argc is the number of args (including the command itself) argv is an array of strings (actually it is an array of pointers to strings)

Basic Output with printf printf is a sophisticated function for generating output printf can output the values of C variables printf can help format output See lec/01/printf.c 10

Working with Strings In C, strings are an array of characters with a null termination \0 character at the end The length of a string does not include the termination character The storage for a string must include space for the termination character Arrays and pointers can be used to access strings Be sure to know where the string is stored See lec01/src/strings.c 11

Buffered I/O There are two ways to read/write data Directly use system calls open(), close(), read(), write() Use buffered I/O fopen(), fclose(), fread(), fwrite(), printf() Buffered I/O is used for better performance See lec01/src/linenums.c 12

The C Preprocessor Before compilation, C source files are preprocessed (e.g., translated) The preprocessor (cpp) handles Include files (#include<foo.h>) Macro expansion (#define NMAX 3) Conditional compilation Most C programs use the preprocessor, especially OS kernels 13

Include Files System include files (where are they?) #include<stdio.h> Program local include files #include foo.h General rule: do not declare storage within and include file, only data types, function prototypes and macros Add an include directory to search path: gcc -o main main.c -I/home/benson/src Now you can use #include<foo.h> 14

Preprocessor Macros Simple #define MAXCOUNT 3 Parameterized #define ADD(x,y) x + y Complex #define insert(e, l) { \ e->next =l->head; \ l->head = e; \ } while(0) 15

Conditional Compilation Used for portability and testing #ifdef LINUX printf( Linux\n ); #else printf( Unknown\n ); #endif /* LINUX */ 16

Include File Management Conditional compilation can be used to make sure only one instance of an include file is preprocessed #ifndef _FOO_H_ #define _FOO_H_... header contents... #endif /* _FOO_H_ */ 17

Linked Lists and Structures Pointers and linked structures are used extensively inside an OS kernel Basic idea: allocate memory, create links between allocated memory blocks Used for lists, trees, hash tables, etc. Can be tricky to debug, so we need to have a good understanding to avoid common mistakes See lec01/src: Makefile, namelist.h namelist.c, nametest.c 18