CS 537 Section 1 Programming in Unix and C

Slides:



Advertisements
Similar presentations
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Advertisements

Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
Guide To UNIX Using Linux Third Edition
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
Agenda Attack Lab C Exercises C Conventions C Debugging
C By Example 1 The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass by.
(language, compilation and debugging) David 09/16/2011.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
Department of Electronic & Electrical Engineering Introduction to C - The Development cycle. Why C? The development cycle. Using Visual Studio ? A simple.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
1 C Basics Monday, August 30, 2010 CS 241. Announcements MP1, a short machine problem, will be released today. Due: Tuesday, Sept. 7 th at 11:59pm via.
C Primer Session – 1/25/01 Outline Hello World Command Line Arguments Bit-wise Operators Dynamic Memory / Pointers Function Parameters Structures.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
Hank Childs, University of Oregon April 15 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Background Survey Answers Operating Systems CS 550 Spring 2016 Kenneth Chiu.
Introduction to C Topics Compilation Using the gcc Compiler
Buffer Overflow By Collin Donaldson.
LINKED LISTS.
CSE 374 Programming Concepts & Tools
CS/COE 0449 (term 2174) Jarrett Billingsley
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Computer Science 210 Computer Organization
C Primer.
CSE 374 Programming Concepts & Tools
A bit of C programming Lecture 3 Uli Raich.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Lecture 8 String 1. Concept of strings String and pointers
C Programming Tutorial – Part I
Command Line Arguments
An Introduction to C Programming
CSE 303 Concepts and Tools for Software Development
CSE 351 Section 1: HW 0 + Intro to C
CSC215 Lecture Input and Output.
C programming language
Command-Line Arguments
C Basics.
CS111 Computer Programming
Programming Languages and Paradigms
Recitation: C Review TA’s 19 Feb 2018.
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Hank Childs, University of Oregon
C Stuff CS 2308.
CSE1320 Strings Dr. Sajib Datta
Dynamic Memory Allocation
Memory Allocation CS 217.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Introduction to C Topics Compilation Using the gcc Compiler
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2015.
CNT4704: Analysis of Computer Communication Network Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Fall 2011.
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Introduction to C Topics Compilation Using the gcc Compiler
Program Execution in Linux
Appendix F C Programming Environment on UNIX Systems
Homework Continue with K&R Chapter 5 Skipping sections for now
C By Example The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2016.
15213 C Primer 17 September 2002.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2013.
SPL – PS1 Introduction to C++.
Chapter 11 Introduction to Programming in C
Introduction to C CS 3410.
Presentation transcript:

CS 537 Section 1 Programming in Unix and C Michael Swift © 2004-2009 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

Sections Ask questions about lecture material Project discussion C programming © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

Project 1 Part 1: Shuffle a file Part 2: add a system call to xv6 You will write a program that reads a text file Files can be of any length, with lines up to 512 characters Output: shuffle lines from beginning and end Program input: read a file specified on the command line: shuffle –i input.txt –o output.txt Part 2: add a system call to xv6 getppid() – get PID of parent of a process © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

xv6 Unix version 6: classic version of Unix from 1974 Fully documented, commented by Lyons Ported to x86 by MIT Allows class projects to use a real operating system From 1974 Avoids complexity of Linux, Windows, & MacOS 16 million lines of code for Linux 7,200 lines of code for xv6 © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

Downloading + compiling xv6 Copy: cp ~cs537-1/ta/xv6/xv6.tar.gz . Unpack: tar -xzf xv6.tar.gz Compile: cd xv6 make Run: make qemu Add a program in xv6: cd user Add file to user director Add file to user/makefile.mk Recompile xv6 © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

Facilities Department Linux machines (penguins): 1355: emperor-01 - 07 1358: rockhopper-01 - 08 1366: royal-01 - 30 1368: snares01 – 10 © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

Issues with C Little hand-holding for programmer Manual memory management Small standard library No native support for threads and concurrency Weak type checking © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

Using C and Unix Make Makefile program.c Compiler program.h stdio.h gcc -c program.c program.c Compiler program.h program.o stdio.h Linker libc.a program © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

C language #include <stdio.h> int main(int argc, char * argv[]) Preprocessor include directive for header files #include <stdio.h> int main(int argc, char * argv[]) { printf(“Hello, world: %s\n”,argv[1]); return(0); } Declaration of main function and arguments Print first command-line parameter © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

Issues with C Memory allocation Pointer arithmetic and arrays malloc(), free() Pointer arithmetic and arrays Preprocessor © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

Example #include <stdio.h> int main(int argc, char * argv[]) { int i; for (i = 0; i < argc; i++) { printf("argv[%d] = %s\n",i, argv[i]); } return(0); © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

Strings Strings in C are arrays of bytes: char str[100]; They are null terminated – so you need to make space for it str[0] = ‘\0’; strlen(str) = 0; There are a bunch of functions for working with them: strlen, strcpy, strcat © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

String Example #include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { char s[100]; strcpy(s,"hello"); strcat(s,", world"); printf("S = %s\n",s); } © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

Memory You have to mange memory yourself. Fixed-size variables can be allocated on a stack The contents of these variables go way when the function returns. char str[100] = “hello, world\n”; Variable-size variables are allocated using malloc, like new() in Java. Memory from malloc only becomes invalid when you free it: char *str; str = malloc(n); strpy(str,”hello, world\n”); free(str); © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

Memory Example 1 #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { int * array = NULL; int i; array = (int *) malloc(100 * sizeof(int)); if (array == NULL) { printf("Malloc failed\n"); return(0); } for (i = 0; i < 100; i++) array[i] = 100-i; printf("array[%d] = %d\n",i,array[i]); free(array); © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

Memory Example 2 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char *argv[]) { char * s; s = malloc(100); if (s == NULL) { printf("Malloc failed\n"); return(0); } strcpy(s,"hello"); strcat(s,", world"); printf("S = %s\n",s); free(s); © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

File I/O f* functions for accessing files: struct FILE *: represents an open file f = fopen(“foo”,”r”) – open file foo for reading fclose(f) - says you are done with f bytes = fread(buffer,size,count,f) = reads size x count bytes from f into buffer fwrite(buffer, size, count ,f) = writes size x count bytes to f from buffer © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

File Example #include <stdio.h> int main(int argc, char * argv[]) { FILE * f; char s[101]; f = fopen("test.txt","r"); if (f == NULL) { printf("Error opening file\n"); return(0); } while (fgets(s, 100, f) != NULL) printf("%s",s); fclose(f); © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

Data Structures in C Global arrays Dynamic arrays are pointers int global_data[500]; Dynamic arrays are pointers int * dyn_array; dyn_array = malloc(sizeof(int) * 100); Structures typedef struct __rec_t { int key; int size; int record[NUMRECS]; } rec_t; rec_t r; r.key = 10; r.size = 10; r.record[0] = 0; … © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

More advanced topics Compiler errors and warnings Multiple files gcc –o foo -Wall foo.c Multiple files gcc –o foo foo.c bar.c baz.c © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

Documentation Unix/Linux man pages example: “man close” CLOSE(3) BSD Library Functions Manual FCLOSE(3) NAME fclose -- close a stream LIBRARY Standard C Library (libc, -lc) SYNOPSIS #include <stdio.h> int fclose(FILE *stream); DESCRIPTION The fclose() function dissociates the named stream from its underlying … RETURN VALUES Upon successful completion 0 is returned. Otherwise, EOF is returned and © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

Man pages Documentation is divided into sections Programs, commands System calls Subroutine libraries Hardware Config files Games Miscellaneous System administration man returns the result from the lowest-numbered section apropos searches for commands with a word © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

Debugging Compile with debugging using “-g” Run your program with gdb gcc -g -o foo.o foo.c Run your program with gdb gdb foobar GNU gdb 6.3 <copyright omitted> (gdb) break main breakpoint 1 at 0x80483b0: in file foo.c, line 5 (gdb) run Starting program: /afs/cs.wisc.edu/…/foobar Breakpoint 1, main (argc=1, argv=0xbfe27804) at foo.c:5 if (argc > 1) { (gdb) print argc $1 = 1 (gdb) © 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift