S -1 Library Functions. S -2 Standard Libraries Any system call is not part of the C language definition Such system calls are defined in libraries, identified.

Slides:



Advertisements
Similar presentations
C: Advanced Topics-II Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Advertisements

Introduction to C Programming
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Adv. UNIX: lib/121 Advanced UNIX v Objectives of these slides: –look at some of the less familiar functions in the ANSI C Standard Library
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Chapter 14 - Advanced C Topics Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 10.
1 Homework / Exam HW7 due class 25 Exam 3 - class 26 –Open Book, Open Notes –Covers up through end of K&R 7 –and Appendix B Standard Library –Plus UNIX.
. Plab – Tirgul 5 pointers to pointers, libraries.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Unix System Overview Programmer’s Perspective Chien-Chung Shen CIS, UD
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
Chapter 6 Buffer Overflow. Buffer Overflow occurs when the program overwrites data outside the bounds of allocated memory It was one of the first exploited.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
SIMULATED UNIX FILE SYSTEM Implementation in C Tarek Youssef Bipanjit Sihra.
Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002.
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.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
Characters and Strings File Processing Exercise C Programming:Part 3.
UNIX Files File organization and a few primitives.
S -1 Pipes. S -2 Inter-Process Communication (IPC) Chapter Data exchange techniques between processes: –message passing: files, pipes, sockets.
Lecture 1 cis208 January 14 rd, Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
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.
Navigating Directories
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
Interacting with Unix. Getting the Process ID u Synopsis #include pid_t getpid(void); u Example: #include int main(){ pid_t n = getpid(); printf("Process.
C LANGUAGE Characteristics of C · Small size
Recitation 9: Error Handling, I/O, Man Anubhav Gupta Section D.
CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm.
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Today’s Material Strings Definition Representation Initialization
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Hank Childs, University of Oregon April 15 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Hank Childs, University of Oregon April 13 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Error handling I/O Man pages
Today topics: File System Implementation
A bit of C programming Lecture 3 Uli Raich.
An Introduction to C Programming
UNIX System Overview.
C Programming:Part 3 Characters and Strings File Processing Exercise.
C: Primer and Advanced Topics
C Stuff CS 2308.
Input/Output and the Operating Systems
Miscellaneous functions
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.
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
CSCI 380: Operating Systems William Killian
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.
COMP 2130 Intro Computer Systems Thompson Rivers University
Presentation transcript:

S -1 Library Functions

S -2 Standard Libraries Any system call is not part of the C language definition Such system calls are defined in libraries, identified with the suffix.a Libraries typically contain many.o object files To create your own library archive file: ar crv mylib.a *.o Disregard “ ranlib ” command in Wang, p311 (no longer needed) Look in /usr/lib and /usr/local/lib for most system libraries Can list all.o files in an archive use “ ar t /usr/lib/lib.a ” More useful to see all the function names: /usr/ccs/bin/nm /usr/lib/libc.a | grep FUNC

S -3 Standard Libraries (cont) By default, gcc links /usr/lib/libc.a to all executables Typing “ man 3 intro ” will give a list of most of the standard library functions Any other libraries must be explicitly linked by referring to the absolute pathname of the library, or preferably by using the “ -l ” gcc switch: gcc *.o /usr/lib/libm.a -o mathExamples gcc *.o -lm -o mathExamples These.a files are also sometimes referred to as static libraries Often you will find for each system.a file a corresponding.so file, referred to as a shared object (not needed for this course) Advantage of shared objects: smaller executable files (library functions loaded at run time)

S -4 Standard Libraries: Example #include /* #include */ int main( void ) { printf( “Square root of 2 is %f\n”, sqrt(2) ); return( 0 ); } May get various problems/errors when you compile with: 1) gcc example.c -o example 2) gcc example.c -m -o example 3) gcc example.c -m -o example # with math.h included

S -5 Files and Directories Disk drives divided into partitions Each partition contains a filesystem (type df for a listing of filesystems mounted on any given computer) Filesystems are mounted onto existing filenames (Fig 8.4, p.241) Each filesystem has a boot block, a super block, an ilist containing inodes (short for index nodes), directory blocks, and data blocks An inode contains all the information about a file: type, time of last modification/write/access, uid/gid of creator, size, permissions, etc. Directories are just lists of inodes (2 files automatically created with mkdir: “. ” (inode of directory) and “.. ” (inode of parent directory) See figure 8.3 (page 240) for an example.

S -6 Example: argc/argv #include int main( int argc, char *argv[]) { if( argc == 2 ) { struct stat buf; if( stat( argv[1], &buf ) != -1 ) printf( “file %s has size %d\n”, argv[1], buf.st_size ); } return( 0 ); }

S -7 Miscellaneous fopen / fread / fwrite / fclose, etc. are implemented in terms of low-level non-standard i/o functions open / read / write / close, etc. There are 3 types of buffering: –fully buffered (or block buffered): actual physical i/o takes place only when buffer is filled –line buffered: actual i/o takes place when a newline ( \n ) is encountered –unbuffered: output as soon as possible All files are normally block buffered, except stdout (line buffered only if it refers to a terminal), and stderr (always unbuffered) Can use fflush() to force a buffer to be cleared

S -8 Advanced Library Functions

S -9 String/Character Handling All “str” functions require input strings be terminated with a null byte Some of the most common ones: strlen, strcpy, strcmp, strcat strtok used for extracting "tokens" from strings memcpy not just for strings! strncmp allows limits to be placed on length of strings, other 'n' string functions Some function for testing/converting single characters: isalpha, isdigit, isspace toupper, tolower atoi, atol

S -10 Storage Allocation Dynamic memory allocation (very important for many C programs): malloc, calloc, free, realloc An (incomplete) example: #include struct xx *sp; sp = (struct xx *) malloc( 5 * sizeof(struct xx) ); if( sp == (struct xx *) NULL ) { fprintf( stderr, “out of storage\n” ); exit( -1 ); }

S -11 Date and Time Functions clock_t, clock(), time_t, time() Most UNIX time functions have evolved from various sources, and are sometimes inconsistent, referring to time as one of: –the number of seconds since Jan 1, 1970 (or Jan 1, 1900) –the number of clock ticks since Jan 1, 1970 (or Jan 1, 1900) –the broken down structure “ struct tm ” (see /usr/include/time.h ) –the broken down structure “ struct timeval ” (see /usr/include/sys/time.h ) Some are intended for time/date, whereas others are intended for measuring elapsed time

S -12 Variable Arguments An under-used but very powerful feature printf() is an example where the number and types of arguments can differ from invocation to invocation /usr/include/stdarg.h provides definitions of: –a special type named va_list –three macros to implement variable arguments: va_start va_end va_arg Another useful function is “ vfprintf ”, as shown in the next slide

S -13 Variable Arguments A very useful example: #include void Abort( char *fmt,... ) { va_list args; va_start( args, fmt ); fprintf( stderr, "\n\t" ); vfprintf( stderr, fmt, args ); fprintf( stderr, "\n\n" ); va_end( args ); exit( -1 ); }

S -14 Environment Interfacing Reading environment variables: getenv( “PATH” ); Executing a “ $SHELL ” shell command: fflush( stdout ); system( “ls -atl” ); Can also execute a system call and have its output sent to a pipe instead of stdout: (we’ll talk more about pipes in chapter 12) FILE *pipe; pipe = popen( “ls -atl”, “r” );... pclose( pipe );