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

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

CS Basics 15) Compiling a C prog.

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

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

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

Problem Set 1: Unix Commands 1

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

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

CS240: Programming in C

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

PRINCIPLES OF OPERATING SYSTEMS

Chapter 11 Introduction to Programming in C

CMPT 300. Operating Systems. Brief Intro to UNIX and C

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Software Development With Emacs: The Edit-Compile-Debug Cycle

2 Compiling a C program

Chapter 11 Introduction to Programming in C

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

File Access. FILE * fopen(const char *name, const char * mode);

CS2141 Software Development using C/C++ Compiling a C++ Program

Lecture 03 Bits, Bytes and Data Types

Miscellaneous C-programming Issues

Introduction to C. CS2023 Winter 2004

Chapter 11 Introduction to Programming in C

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

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

How to learn C? CSCI [4 6]730: A C Refresher or Introduction. Diving In: A Simple C Program 1-hello-word.c

CSE 303, Winter 2006, Final Examination 16 March Please do not turn the page until everyone is ready.

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Laboratory 1 Semester 1 11/12

CSE 374 Programming Concepts & Tools

Follow us on Twitter for important news and Compiling Programs

CSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0.

Computer Programming. The greatest gift you can give another is the purity of your attention. Richard Moss

C Program Development and Debugging under Unix SEEM 3460

Programming in C week 1 meeting Tiina Niklander

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

Chapter 11 Introduction to Programming in C

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

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

Systems Programming. Fatih Kesgin &Yusuf Yaslan Istanbul Technical University Computer Engineering Department 18/10/2005

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

CS 107 Lecture 18: GCC and Make

Programming in C First meeting

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

Reviewing gcc, make, gdb, and Linux Editors 1

We first learn one useful option of gcc. Copy the following C source file to your

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

Programming in C First meeting Tiina Niklander

CSE 351. GDB Introduction

CS201 Lecture 2 GDB, The C Library

COMP 2355 Introduction to Systems Programming

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

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

Recitation: Cache Lab & C

A software view. Computer Systems. The Compilation system. How it works. 1. Preprocesser. 1. Preprocessor (cpp)

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

Lectures 5-6: Introduction to C

JTSK Programming in C II C-Lab II. Lecture 3 & 4

How Compiling and Compilers Work

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012

Introduction to Supercomputing

Lectures 5-6: Introduction to C

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Array Initialization

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Topic 6: A Quick Intro To C

Download, Install and Setup the Linux Development Workload Create a New Linux Project Configure a Linux Project Configure a Linux CMake Project

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Programming in C. What is C?... What is C?

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

Compilation, Disassembly, and Profiling (in Linux)

Workshop Agenda Feb 25 th 2015

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

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

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

CS 11 C track: lecture 6

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

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

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

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

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

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

UNIX Makefile. C Project Library Distribution and Installation.

Page 1. Agenda. Programming Languages. C Compilation Process

CAAM 420 Fall 2012 Lecture 15. Roman Schutski

Exercise Session 6 Computer Architecture and Systems Programming

Errors During Compilation and Execution Background Information

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

Getting Familiar with CCN

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

CSE 333 Lecture 7 - final C details

Programming. Projects with Multiple Files

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

Department of Computer Science and Engineering Yonghong Yan

Transcription:

Programs CSCI 4061 Introduction to Operating Systems C Program Structure Libraries and header files Compiling and building programs Executing and debugging Instructor: Abhishek Chandra Assume familiarity with C programming 2 C Refresher Set of variables, types and functions Variables: Global: Visible in all functions Local: Visible within a single function Static: Visible to all functions within a file Types: structs, unions, typedefs Functions: User-defined or library functions Function main int main(int argc, char *argv[]) main is the program starting point argc: number of command-line arguments argv: array of command-line arguments What are the value of argc and argv for: myprog hello world myprog hello world 3 4 1

Standard Program Elements Parse the command line Check for syntax and extract various options Report error/usage if ill-formed command line Do the desired execution Read files, run threads, write output, etc. Cleanup and exit Free all resources Exit gracefully (print error msg if abnormal exit) Program Structure Modules: Independent and interacting parts of a program implementing specific functionality Typically implemented in separate files E.g.: Web browser: Module for tracking user input Module for communicating with server Module for displaying Web pages 5 6 C Program Files Header Files (.h) Header files Source files Libraries Contain types, function prototypes, constants Contain elements that are shared across modules Contain the interface exposed to external programs E.g.: library header files Default include path: /usr/include Program 7 8 2

Source files (.c) Core of the program modules Implement the functions of the program May contain static variables that are required only in that source file Contain include statements for header files to include global declarations and prototypes #include <stdio.h> #include decls.h Libraries Utility functions/system calls Pre-installed on the system Prototypes of functions called from program must be declared Include appropriate header files String library: #include <string.h> Libraries may have to be linked at compile time using -l<lib> flag cc o hello hello.c -lm 9 10 Programming Commandments Thou shalt write readable code Thou shalt use lot of documentation Comments and indentation Thou shalt use good coding style Meaningful variable/function names E.g.: sort_list() instead of foo() Use named constants instead of numbers E.g.: MAXLEN instead of 1024 Functions should be about 1 page long Put unrelated functions in separate files Error Handling If a function can return an error, check for it Check for every possible error, even if rare Return values from your functions Error codes on error Do not exit abruptly from your functions Exit gracefully from main() System/library calls: use perror() Library calls return -1, set errno Always check library calls for errors if (close(fd)==-1) perror( Failed to close the file ); 11 12 3

Program Compilation Preprocessor: Processes include statements, macro and constant definitions Compiler: Converts source code into assembly code Assembler: Converts assembly code into machine language Linker: Combines multiple object files into executable program Program Compilation hello.c hello.o #include <stdio.h> #define MAXLEN 1024 01110010100 0111... void print_msg() { char *msg= Hello World ; printf( %s\n, msg); } Compiler hello.s Print_msg: store this load that push register... Assembler Linker Object files, Libraries hello 13 14 Program Compilation: make make builds programs by using dependency trees Makefile allows multiple program dependencies to be specified in a file If a source/header file changes, make recompiles dependent targets recursively Program Execution Runtime environment Program arguments Environment variables Dynamic libraries Other programs and the Operating System hello: hello.o util.o gcc o hello hello.o util.o lm hello.o: hello.c hello.h gcc c hello.c 15 16 4

Program Arguments and Environment Command-line arguments: argc, argv Environment variables: Associated with each program Typically determined by the invoking shell environment Dynamically Linked Libraries Static vs. dynamic linking Statically linked libraries are linked into the executable at compile time Is this a problem? Dynamically linked libraries are loaded at runtime when the corresponding function is called System must be able to find dynamic libraries at runtime $LD_LIBRARY_PATH Default: /lib, /usr/lib 17 18 Other Programs and Operating System Other programs impact your program in several ways: Communication Sharing of data Affect performance Operating system: Provides all the low-level services and utilities Manages resources for your program Provides security and isolation Testing and Debugging Run lots of test-cases Ill-formed command lines (too few, too many arguments) Missing files, data, input, etc. Boundary conditions: NULL pointers, zero-size arrays, (n+1)-length strings Too long/too short strings, numbers Read specifications VERY carefully! Ask for clarifications ASAP 19 20 5

Debugging Iterative process Using printf() Track variables, functions by printing out useful information Edit, recompile, run to get additional info Turn debugging on/off using compiler flags #ifdef DEBUG fprintf(stderr, val=%d\n, val); #endif Debugging: gdb GNU debugger Allows examining the program in slow-motion with pauses and fast-forwards (playbacks, but no rewinds) Set breakpoints Step through program Examine variable values Use g flag while compiling program gcc g o hello hello.c 21 22 Program Performance Correctness and Functionality Quantitative Metrics Runtime Latency Throughput System Resource usage CPU time, memory, disk space, network bandwidth Observing a Running Program time: Get the runtime of your program (both user and kernel time) ps: Gives information about running programs in the system (processes) top: Gives resource usage information (CPU share, memory, running time, status, etc.) strace: Trace system calls 23 24 6