Linux Programming Introduction - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Linux Programming Introduction

Description:

first developed by Ken Thompson (Bell Lab) on PDP-7 (16 bit machine) ... 0) { /* Save errno because it's clobbered by the next system call. */ int error_code = errno; ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 20
Provided by: david2175
Category:

less

Transcript and Presenter's Notes

Title: Linux Programming Introduction


1
Linux Programming Introduction
  • CS 230
  • ???

2
The Unix System - History
  • first developed by Ken Thompson (Bell Lab)
  • on PDP-7 (16 bit machine)
  • Dennis Ritchie joined (MULTICS experience)
  • C was developed for Unix
  • two ATT organizations
  • Bell Labs Research
  • 8th edition, 9th edition, Plan 9
  • stream IO, RFS,
  • USG-gtUSDL-gtATTIS-gtUSO-gtUSL
  • System V
  • UC Berkeley
  • add virtual memory to the 7th edition
  • DARPA funding started for a standard Unix
  • TCP/IP, The Internet
  • influenced SunOS significantly (4.3BSD)

3
Commercial Unix
  • Sun
  • SunOS from BSD
  • Solaris from System V
  • DEC
  • Ultrix, Digital Unix
  • OSF/1 (from Mach)
  • MS
  • Xenix -gt NT
  • IBM
  • AIX

4
Unix Lineage
1st ed.
USG -gt USL
Bell Labs
UCB
6th ed.
1BSD
7th ed.
3BSD
8th ed.
Sys III
4.2BSD
Mach
10 ed.
SunOS 3, 4
SVR 3
4.3BSD
OSF/1
Plan 9
SVR 4
Solaris
4.4BSD
Solaris 2
5
Design Principles
  • time sharing
  • supports multiple processes
  • easy process creation
  • simple, replaceable user interface
  • shell
  • file system
  • multilevel tree structrure
  • IO devices are treated like files
  • details are hidden in low level -kernel (device
    drivers)
  • not so much goals in early design
  • OS for programmer, not for production
  • simple and easy (not performance and
    functionality)
  • interactive (no batch processing)
  • C from the original programmers preference
  • easy porting -gt wide spread of Unix
  • small hardware platform
  • entails elegance

6
System Calls
  • Defines the programmer interface to UNIX
  • how to use files
  • how to manage processes
  • .
  • Details of syscall mechanism are hidden from app.
    programmers
  • syscalls are provided as C functions
  • classification of syscalls
  • process control
  • file manipulation
  • information manipulation
  • device manipulation (similar to files)

7
Compile and Link Review
  • gcc -c -02 -D NDEBUG file_name.cpp
  • NDEBUG decides the level of debugging (assert
    statement)
  • -c option produces object file, not an executable
  • -02 defines the level of optimization
  • more optimized code is more difficult to debug
  • info gcc for more information about the compiler
  • g -o test main.o test.o L
    /usr/local/lib/pam lpam
  • g for linking c code (gcc for C code)
  • g searches /lib and /usr/lib specify other
    locations with L
  • g add lib prefix and .a suffix pam
    becomes libpam.a

8
Makefile
  • reciprocal main.o reciprocal.o
  • g (CFLAGS) -o reciprocal main.o reciprocal.o
  • main.o main.c reciprocal.hpp
  • gcc (CFLAGS) -c main.c
  • reciprocal.o reciprocal.cpp reciprocal.hpp
  • g (CFLAGS) -c reciprocal.cpp
  • clean
  • rm -f .o reciprocal
  • make CFLAG-02
  • make clean
  • info make

9
Debugging with gdb
  • compile with g option
  • gdb reciprocal
  • (gdb) run
  • Starting program reciprocal
  • Program received signal SIGSEGV, Segmentation
    fault.
  • __strtol_internal (nptr0x0, endptr0x0,
    base10, group0)
  • at strtol.c287
  • 287 strtol.c No such file or directory.
  • (gdb)
  • other commands
  • where shows where the program stops
  • up 2 go up 2 levels of stack
  • print - shows the value of a variable
  • break proc - stops at the 1st line of the
    procedure proc
  • next - proceeds one more line

10
Execution Environment
  • you must have learnt
  • argv and argc to the main program
  • stdout and stdin
  • GNU Coding Standards
  • ls - s /usr/lib
  • ls - -size /usr/lib
  • info (standards)User Interfaces
  • getopt_long( )
  • a lib function to parse command line options
  • const char const short_options hov
  • const struct option long_options
  • help, 0, NULL, h ,
  • output, 1, NULL, o ,
  • verbose, 0, NULL, v ,
  • NULL, 0, NULL, 0 / Required at end of
    array. /
  • each call returns a short option letter
  • o option(followed by ) requires and argument

11
Standard IO
  • provided by standard C lib
  • stdin, stdout, stderr
  • can be accessed as a file descriptor
  • 0, 1, 2
  • can be used for redirections
  • program gt output_file.txt 2gt1 stderr is
    merged with stdout
  • program 2gt1 filter order gt,
    2gt1, filter
  • stdout is buffered while stderr is NOT
  • fflush(stdout)

12
The Environment
  • Examples USER, HOME, PATH, DISPLAY, EDITOR,
  • set an environment variable
  • EDITORemacs
  • export EDITOR
  • environ variable (defined in GNU C lib) points to
    all environment variables

13
Use of Environment in a Program
14
temporary files
  • You need them to store data or to pass the data
    to other programs.
  • more than one processes may access the same file
  • file permission should set properly
  • file name should be esoteric
  • mkstemp
  • char temp_filename /tmp/temp_file.XXXXXX
  • int fd mkstemp (temp_filename)
  • unlink (temp_filename)
  • replaces XXXXXX with unique characters
  • opens a file for r/w
  • the file is not deleted automatically
  • unlink makes the file to be deleted when the
    program exits
  • fd tmpfile()
  • creates a temporary file for internal use only
  • deletes the file when the program exits

15
Defensive Coding
  • assert
  • C macro expands to C code runtime overhead
  • compile with NDEBUG will remove this statement
  • assert(some_function() 0) this may not be
    executed
  • r some_function() this is always
    executed
  • assert(r 0)
  • argument Boolean expression
  • assert(status0)
  • nice places to use
  • check null pointer
  • check parameter values
  • system call may fail due to unexpected system
    failures
  • run out of resources
  • permission
  • wrong parameters to the calls
  • hardware may not work as you expected
  • your code should cope with such failures

16
Defensive Coding (2)
  • Error codes from syscall
  • errno contains the error number denoted by
    EINVAL, EACCES, ..
  • strerror returns the string from errono
  • Blocking syscall
  • returns EINTR when a signal is received while
    being blocked

17
Defensive Coding - Example
rval chown (path, user_id, -1) if (rval ! 0)
/ Save errno because its clobbered by the
next system call. / int error_code
errno assert (rval -1) switch
(error_code) case EPERM / Permission
denied. / case EROFS / PATH is on a
read-only file system. / case ENAMETOOLONG /
PATH is too long. / case ENOENT / PATH does
not exit. / case ENOTDIR / A
component of PATH is not a directory. / case
EACCES / A component of PATH is not
accessible. / fprintf (stderr, error chown of
s s\n, path, strerror (error_code)) break
/ continue ? / case EFAULT / PATH contains
an invalid memory address. / abort () case
ENOMEM / Ran out of kernel memory. / fprintf
(stderr, s\n, strerror (error_code)) exit
(1) default / Some other, unexpected, error
code. / abort ()
18
Archives
  • an archive is a collection of object files
  • creating an archive
  • ar cr libtest.a test1.o test2.o
  • shared library
  • library that is linked dynamically
  • is a single object file containing all the
    functions defined
  • shared by many processes
  • it should be position independent code (PIC)
  • creation and usage
  • gcc -c -fPIC test1.c creates PIC
  • gcc -shared -fPIC -o libtest.so test1.o test2.o
    creates slib
  • gcc -o app app.o -L. -ltest link with
    libtest.so
  • lib searching order can be seen with ldd command
  • the static option will search only for static
    archive (xxx.a)
  • linker does not provide the path name of the
    archive to the runtime
  • the runtime searches only /lib and /usr/lib
  • use Wl , -rpath options or LD_LIBRARY_PATH to
    provide more paths to the runtime

19
Archives(2)
  • gcc automatically link programs with libc (g,
    with libstdc)
  • not with libm
  • when a lib is dependent on other shared libs,
    they are linked automatically
  • not with static libs
  • comparison with slib
  • slib supports independent upgrade on the lib
  • slib makes the program image smaller
  • dynamic loading (lazy linking)
  • RTLD_LAZY defines how to bind symbols (resolve
    them as code executes unlike RTLD_GLOBAL which
    resolves at dlopen() time)
  • dlsym() fetches a pointer to a static variable
Write a Comment
User Comments (0)
About PowerShow.com