Lecture 4: Build Systems, Tar, Character Strings

Similar documents
Lecture 2: Memory in C

Lecture 10: Potpourri: Enum / struct / union Advanced Unix #include function pointers

Lecture 7: file I/O, more Unix

Lecture 9: Potpourri: Call by reference vs call by value Enum / struct / union Advanced Unix

TNM093 Practical Data Visualization and Virtual Reality Laboratory Platform

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

Using the Unix system. UNIX Introduction

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

Follow us on Twitter for important news and Compiling Programs

Lecture 10: building large projects, beginning C++, C++ and structs

Lecture 15: Even more pointer stuff Virtual function table

Reviewing gcc, make, gdb, and Linux Editors 1

Basic OS Progamming Abstrac2ons

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

Lab 1 Introduction to UNIX and C

ECE 471 Embedded Systems Lecture 4

Basic OS Progamming Abstrac7ons

Unix/Linux Operating System. Introduction to Computational Statistics STAT 598G, Fall 2011

Basic Unix Commands. CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

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

Computer Systems and Architecture

Introduction to Linux (Part II) BUPT/QMUL 2018/03/21

commandname flags arguments

Introduction to Supercomputing

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

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

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

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

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

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

Installing the Quantum ESPRESSO distribution

Introduction to Linux

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

CS354 gdb Tutorial Written by Chris Feilbach

CS201 Lecture 2 GDB, The C Library

Jackson State University Department of Computer Science CSC / Advanced Information Security Spring 2013 Lab Project # 5

Con$nuous Integra$on Development Environment. Kovács Gábor

Introduction. Overview and Getting Started. CS 161 Computer Security Lab 1 Buffer Overflows v.01 Due Date: September 17, 2012 by 11:59pm

CSE 351. GDB Introduction

CSE 303: Concepts and Tools for Software Development

Lecture 03 Bits, Bytes and Data Types

Exercise Session 6 Computer Architecture and Systems Programming

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

Outline. Classic races: files in /tmp. Race conditions. TOCTTOU example. TOCTTOU gaps. Vulnerabilities in OS interaction

CSC209. Software Tools and Systems Programming.

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

Introduction to Linux Basics

Outline. COMP 2718: Software Development Tools: gcc and make. C and Java 3/28/16

History of Java. Java was originally developed by Sun Microsystems star:ng in This language was ini:ally called Oak Renamed Java in 1995

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

Read the relevant material in Sobell! If you want to follow along with the examples that follow, and you do, open a Linux terminal.

Recitation #1 Boot Camp. August 30th, 2016

PRINCIPLES OF OPERATING SYSTEMS

CSE 303 Concepts and Tools for Software Development. Magdalena Balazinska Winter 2010 Lecture 27 Final Exam Revision

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

CISC 220 fall 2011, set 1: Linux basics

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

CS197U: A Hands on Introduction to Unix

University of Texas at Arlington, TX, USA

int $0x32 // call interrupt number 50

Chapter 1 Getting Started

CSC 1600 Memory Layout for Unix Processes"

Outline. Compiling process Linking libraries Common compiling op2ons Automa2ng the process

Vulnerability Analysis (III): Sta8c Analysis

Unix File System. Learning command-line navigation of the file system is essential for efficient system usage

CSE 303: Concepts and Tools for Software Development

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

CSE 451: Operating Systems. Sec$on 2 Interrupts, system calls, and project 1

UNIX Makefile. C Project Library Distribution and Installation.

The plan. Racket will return! Lecture will not recount every single feature of Java. Final project will be wri)ng a Racket interpreter in Java.

Lectures 5-6: Introduction to C

Carnegie Mellon. Linux Boot Camp. Jack, Matthew, Nishad, Stanley 6 Sep 2016

Introduction to Unix: Fundamental Commands

Week Overview. Unix file system File types and file naming Basic file system commands: pwd,cd,ls,mkdir,rmdir,mv,cp,rm man pages

CSC209. Software Tools and Systems Programming.

CSE 351, Spring 2010 Lab 7: Writing a Dynamic Storage Allocator Due: Thursday May 27, 11:59PM

Unix Introduction to UNIX

Unix Tools / Command Line

CSE 374 Programming Concepts & Tools

Midterm Review. What makes a file executable? Dr. Jack Lange 2/10/16. In UNIX: Everything is a file

Lecture 14: more class, C++ streams

377 Student Guide to C++

Projects and Environment Introduction

Unix and C Program Development SEEM

Intro to Linux. this will open up a new terminal window for you is super convenient on the computers in the lab

PROGRAMMAZIONE I A.A. 2015/2016

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

Project #1 Exceptions and Simple System Calls

Lab #1 Installing a System Due Friday, September 6, 2002

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

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 11 gdb and Debugging (Thanks to Hal Perkins)

Warm-up sheet: Programming in C

Lectures 5-6: Introduction to C

Graphite Ge+ng Started. Ge+ng up and running with Graphite

Why OO programming? want but aren t. Ø What are its components?

Using the Zoo Workstations

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

Transcription:

CIS 330:! / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 4: Build Systems, Tar, Character Strings April 10 th, 2017 Hank Childs, University of Oregon

(Finish Lecture 3)

More array rela5onships int A[10]; int *B; B=(A+5) à A[5] = B[0] B=&(A[0]) à B = A B=&(A[5]) à B = A+5

Pointers to pointers Remember: pointer points to a loca5on in memory We ve been considering cases where loca5ons in memory are arrays of integers But loca5ons in memory could be pointer themselves 0x7fff55bc Code Data Stack 0x7fff55b8 Free Heap

Simple pointers to pointers example

What s the difference between these two programs? Answer: X is on the heap on the lez, and on the stack on the right. But they are both pointers- to- pointers.

What s the difference between these two programs? Answer: program on lez makes one alloca5on for each pointer, program on right makes one alloca5on for whole program & each pointer points at loca5ons within that alloca5on.

Call by value / call by reference Refers to how parameters are passed to a func5on. Call by value: send the value of the variable as a func5on parameter Side effects in that func5on don t affect the variable in the calling func5on Call by reference: send a reference (pointer) as a func5on parameter Side effects in that func5on affect the variable in the calling func5on

Call by Value

Call by value Code Data Return copies into loca5on specified by calling func5on Stack stack_varc stack_vard <info for how to get back to main> A (= 3) <Loca5on for RV> stack_vara Free Heap

Call by reference

Call by reference * Code Data * Return copies into loca5on specified by calling func5on & Stack stack_varc stack_vard <info for how to get back to main> A (= address) <Loca5on for RV> stack_vara Free Heap

(grade 2A)

Outline Review Project 1B Overview Build Project 1C Overview Tar Character Strings

Useful vi commands yy: yank the current line and put it in a buffer 2yy: yank the current line and the line below it p: paste the contents of the buffer Naviga5on :100 go to line 100 in the file / : search forwards,? : search backwards Arrows can be used to navigate the cursor posi5on (while in command mode) So do h, j, k, and l We will discuss more 5ps for vi throughout the quarter. They will mostly be student- driven (Q&A 5me each class)

Permissions: System Calls System calls: a request from a program to the OS to do something on its behalf including accessing files and directories System calls: Typically exposed through func5ons in C library Unix u5li5es (cd, ls, touch) are programs that call these func5ons Permissions in Unix are enforced via system calls.

Executable files An executable file: a file that you can invoke from the command line Scripts Binary programs The concept of whether a file is executable is linked with file permissions

Transla5ng permissions to binary Which of these modes make sense? Which don t? We can have separate values (0-7) for user, group, and other Image from wikipedia

Unix command: chmod chmod: change file mode chmod 750 <filename> User gets 7 (rwx) Group gets 5 (rx) Other gets 0 (no access) Lots of op5ons to chmod (usage shown here is most common)

ls - l Long lis5ng of files Permissions Links (*) Owner Group File size Date of last change How to interpret this? Filename

Permissions and Directories You can only enter a directory if you have execute permissions to the directory Quiz: a directory has permissions 400. What can you do with this directory? Answer: it depends on what permissions a system call requires.

Directories with read, but no execute

Outline Review Project 1B Overview Build Project 1C Overview Tar Character Strings

Unix scripts Scripts Use an editor (vi/emacs/other) to create a file that contains a bunch of Unix commands Give the file execute permissions Run it like you would any program!!

Unix scripts Arguments Assume you have a script named myscript If you invoke it as myscript foo bar Then $# == 2 $1 == foo $2 == bar

Project 1B Summary: write a script that will create a specific directory structure, with files in the directories, and specific permissions.

$ L;,$74),-.*)E$3.)?-.?),$3;*?97$D,'$ $ Project 1B $ $

Outline Review Project 1B Overview Build Project 1C Overview Tar Character Strings

Build: The Actors File types Source code Object code Executable code Programs Compiler Linker Source code (e.g., C code) Compiler Object code Linker Executable code

Analogy Source Code Object Code Executable Code

Compilers, Object Code, and Linkers Compilers transform source code to object code Confusing: most compilers also secretly have access to linkers and apply the linker for you. Object code: statements in machine code not executable intended to be part of a program Linker: turns object code into executable programs

GNU Compilers GNU compilers: open source gcc: GNU compiler for C g++: GNU compiler for C++ C++ is superset of C. With very few excep5ons, every C program should compile with a C++ compiler.

C++ comments // : everything following on this line is a comment and should be ignored Examples: // we set pi below float pi = 3.14159; // approxima5on of pi Can you think of a valid C syntax that will not compile in C++? float radians=degrees//*approx. of pi*/3.14159;

A comment on case (i.e., uppercase vs lowercase) Case is important in Unix But Mac is tolerant gcc t.c à invokes C compiler gcc t.c à invokes C++ compiler

Our first gcc program Invoke gcc compiler Name of file to compile Default name for output programs

Our first gcc program: named output - o sets name of output Output name is different Output has execute permissions

gcc flags: debug and op5miza5on gcc g : debug symbols Debug symbols place informa5on in the object files so that debuggers (gdb) can: set breakpoints provide context informa5on when there is a crash gcc O2 : op5miza5on Add op5miza5ons never fails gcc O3 : provide more op5miza5ons Add op5miza5ons some5mes fails gcc O3 g Debugging symbols slow down execu5on and some5mes compiler won t do it anyways

Debug Symbols live code gcc S t.c # look at t.s gcc S g t.c # look at t.s (- S flag: compile to assembly instead of object code)

Object Code Symbols Symbols associate names with variables and func5ons in object code. Necessary for: debugging large programs

Large code development Source code file1.c Compiler Object code file1.o Why could this be a good idea? Source code file2.c Compiler Object code file2.o Linker Executable code Source code file3.c Compiler Object code file3.o

Mul5- file development: example cat is a Unix command that prints the contents of a file $? is a shell construct that has the return value of the last executed program

Mul5- file development: example

Mul5- file development: example Linker order ma ers for some linkers (not Macs). Some linkers need the.o with main first and then extract the symbols they need as they go. Other linkers make mul5ple passes.

Libraries Library: collec5on of implementa5ons (func5ons!) with a well defined interface Interface comes through header files. In C, header files contain func5ons and variables. Accessed through #include <file.h>

Libraries Why are libraries a good thing? Answers: separa5on I.e., divide and conquer increases produc5vity I.e., simplicity I.e., prevents tendrils between modules that shouldn t exist encapsula5on (hides details of the implementa5on) A li le knowledge is a dangerous thing Products I can sell you a library and don t have to give you the source code.

Libraries Why are libraries a bad thing? Answers: separa5on I.e., makes connec5ons between modules harder (were the library interfaces chosen correctly?) complexity need to incorporate libraries into code compila5on

Includes and Libraries gcc support for libraries -I : path to headers for library when you say #include <file.h>, then it looks for file.h in the directories -I points at - L : path to library loca5on - lname : link in library libname

Library types Two types: sta5c and shared Sta5c: all informa5on is taken from library and put into final binary at link 5me. library is never needed again Shared: at link 5me, library is checked for needed informa5on. library is loaded when program runs More about shared and sta5c later for today, assume sta5c

Making a sta5c library (should have called this libmul5plier.a) Note the # is the comment character

What s in the file?

Typical library installa5ons Conven5on Header files are placed in include directory Library files are placed in lib directory Many standard libraries are installed in /usr /usr/include /usr/lib Compilers automa5cally look in /usr/include and /usr/lib (and other places)

Installing the library (fixing my mistake) mv : unix command for renaming a file

Example: compiling with a library gcc support for libraries -I : path to headers for library - L : path to library loca5on - lname : link in library libname

Makefiles There is a Unix command called make make takes an input file called a Makefile A Makefile allows you to specify rules if 5mestamp of A, B, or C is newer than D, then carry out this ac5on (to make a new version of D) make s func5onality is broader than just compiling things, but it is mostly used for computa5on Basic idea: all details for compila5on are captured in a configura5on file you just invoke make from a shell

Makefiles Reasons Makefiles are great: Difficult to type all the compila5on commands at a prompt Typical develop cycle requires frequent compila5on When sharing code, an expert developer can encapsulate the details of the compila5on, and a new developer doesn t need to know the details just make

Makefile syntax Makefiles are set up as a series of rules Rules have the format: target: dependencies [tab] system command

Makefile example: mul5plier lib

Fancy makefile example: mul5plier lib

Configura5on management tools Problem: Unix pla~orms vary Idea: Where is libx installed? Is OpenGL supported? Write problem that answers these ques5ons, then adapts build system Example: put - L/path/to/libX lx in the link line Other fixes as well

Two popular configura5on management tools Autoconf Unix- based Game plan: You write scripts to test availability on system Generates Makefiles based on results Cmake Unix and Windows Game plan: You write.cmake files that test for package loca5ons Generates Makefiles based on results CMake has been gaining momentum in recent years, because it is one of the best solu5ons for cross- pla~orm support.

Outline Review Project 1B Overview Build Project 1C Overview Tar Character Strings

Project 1C

Outline Review Project 1B Overview Build Project 1C Overview Tar Character Strings

Unix command: tar Anyone know what tar stands for? tar = tape archiver IBM tape library

Unix command: tar Problem: you have many files and you want to move them to another machine give a copy to a friend etc. Tar: take many files and make one file Originally so one file can be wri en to tape drive Serves same purpose as.zip files.

Unix command: tar tar cvf 330.tar file1 file2 file3 puts 3 files (file1, file2, file3) into a new file called 330.tar scp 330.tar @ix:~ ssh ix tar xvf 330.tar ls file1 file2 file

Outline Review Project 1B Overview Build Project 1C Overview Tar Character Strings

ASCII Character Set There have been various extensions to ASCII now more than 128 characters Many special characters are handled outside this conven5on image source: granneman.com

signed vs unsigned chars signed char ( char ): valid values: - 128 to 127 size: 1 byte used to represent characters with ASCII values - 128 to - 1 are not valid unsigned char: valid values: 0 to 255 size: 1 byte used to represent data

character strings A character string is: an array of type char that is terminated by the NULL character Example: char str[12] = hello world ; str[11] = \0 (the compiler did this automa5cally) The C library has mul5ple func5ons for handling strings

Character strings example

Useful C library string func5ons strcpy: string copy strncpy: string copy, but just first N characters strlen: length of a string

Useful C library string func5ons strcpy: string copy strncpy: string copy, but just first N characters strlen: length of a string What happened here?

More useful C library string func5ons source: cplusplus.com