Docsity

Objectives and Contents - Linux System Programming - Lecture Notes, Study notes of Linux skills

Toggle sidebar
bg1
Linux system programming
- About this course
Objectives and contents
1. Programming in Linux environment by us-
ing C (assume that you already have some
C experience).
2. Linux(Unix) system facilities e.g.
System calls, Process and signal handler,
Inter- process communication, develop-
ment tools, etc.
3. Linux hierarchy e.g.
Linux file systems (e.g. VFS, ext2, etc),
Linux kernel structure, device drivers.
1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Objectives and Contents - Linux System Programming - Lecture Notes and more Study notes Linux skills in PDF only on Docsity!

Linux system programming

- About this course

Objectives and contents

  1. Programming in Linux environment by us- ing C (assume that you already have some C experience).
  2. Linux(Unix) system facilities e.g. System calls, Process and signal handler, Inter- process communication, develop- ment tools, etc.
  3. Linux hierarchy e.g. Linux file systems (e.g. VFS, ext2, etc), Linux kernel structure, device drivers.

1

Linux is not Unix

Unix was developed at Bell laboratory, it be- comes a popular multiuser, multitasking operating system for a wide variety of dif- ferent hardware platforms.

Linux is a freely distributed implementation of a Unix-like kernel, the low level core of an operating system.

Linux contains no Unix code, it takes Unix as a model, it is a rewrite based on POSIX standard.

GNU project

Linux community supports the concepts of free software, i.e. software that is free 5

usr/local is a good point to compile pro- grams, to access required files, to up- grade the operating system.

usr/X11 is the X window system, which is commonly installed under this direc- tory.

  • Make sure the C compiler is installed in your system

type in this program, save it as hello.

#include<stdio.h>

int main()

{ printf(‘‘Hello World\n’’); exit(0); }

type in the two lines of commands, to see the r

gcc -o hello hello.c

./hello

  • Header files
    1. Header files are normally located in /usr/include. Depends on the particu- lar form of Unix or Linux, they can run in /usr/include/sys, /usr/include/linux.
    2. Use header files in non standard place. For example, you would like compiler to look in the specific directory, as well as the standard places for header files, which are included in fred.c.
  1. Standard libraries are usually stored in /lib and /usr/lib. The C compiler has to be told explicitly which libraries to search, as by default, it searches only the stan- dard C library.

A simple example:

cc -o fred fred.c /usr/lib/libm.a

or

cc -o fred fred.c -lm

Search for a specific library

cc -o x11fred -L/usr/opendwin/lib x11fred.c -lX

where -L indicates the location of libX11.

  1. Static libraries and Shared libraries

Library is a collection of object files kept together in a ready-to-use form.

The compiler and linker take care of com- bining the program code and the library into a single executable program. You must use the -l option to indicate which libraries.

Static Libraries : .a Shared Libraries: .so

When a program uses a shared library, it is linked in such a way that it doesn’t con- tain function code itself, but reference to shared code that will be made available at run time. When the program is executed, the function references are resolved and calls are made to the shared library, which will be loaded into memory if needed.

/* This is lib.h. It declares the function fred and bill*/ void bill(char *arg); void fred(int);

/* This is program.c */ #include ‘‘lib.h’’ int main() { bill(‘‘Hello World’’); exit(0); }

/* compile the function separately, test it */ cc -c program.c cc -o program program.o bill.o ./program

/* create our own library called libfoo.a */ ar crv libfoo.a bill.o fred.o

/* create a table contents for the library, make the library ready to use */ ranlib libfoo.a

/* use this library to link to program.o */ cc -o program program.o libfoo.a ./program

/* a shorthand */ cc -o program program.o -L. -lfoo

$ ls -l /bin/sh lrwxrwxrwx 1 root root 4 Sep 11 2001 /bin/sh -> bash

/check the version if you have bash/ $/bin/bash -version

GNU bash, version 2.04.21(1)-release (i386-redhat-linux-gnu) Copyright 1999 Free Software Foundation,Inc.

or /* invoke your bash command prompt */ $/bin/bash bash-2.04$ echo $BASH_VERSION 2.04.21(1)-release bash-2.04$ exit exit $

So, I have a Bourne Again Shell! What is yours? Probably the same. It is the Linux staple, from the GNU project .bash, has advantage that the source code is free available.

  • Redirection and Pipes
    1. Redirecting output $ ls -l > lsoutput.txt $ emacs lsoutput.txt & $ You can output the result of a com- mand into a file as you like.This is called redirecting, but be careful that your exiting file with the same name will be overwritten. $ ps >> lsoutput.txt $ emacs lsoutput.txt & $ append the result of “ps” command into lsoutput.txt, check the file!
    2. Pipes /* try this on your machine */ $ ps -x | sort | more $ ps -xo comm | sort | more
  1. A Shell script

Type this command in an editor, we save it as a file called “first.sh”.

#!/bin/sh echo "Is it morning?Please answer yes/no" read timeofday

if [ $timeofday = "yes" ]; then echo ‘‘Good Morning’’ else echo ‘‘Good afternoon’’ fi exit 0

Execute this file as following:

$./first.sh Is it morning? Please answer yes or no yes Good Morning

$ ./first.sh Is it morning? Please answer yes or no

no Good afternoon

  1. Function

Type this command in an editor, we save it as a file called “foo”.

#!/bin/sh foo( ){ echo "Is it morning? Please answer yes or n read timeofday

if [ $timeofday = "yes" ]; then echo "Good Morning" else echo "Good afternoon" fi } echo "script starting" foo echo "script ended" exit 0

What did we learn?

Unix Philosophy !!!

KISS (keep it small and simple)

Filter

Reusable-component

Flexibility

7

Review An Example

bill.c , fred.c

/*compile the functions */ cc -o bill.c fred.c /usr/lib/../crt1.o: In function ‘_start’: /usr/lib/../crt1.o: undefined reference to ‘main’ collect2: ld returned 1 exit status

error!!! the correct way: cc -c bill.c fred.c bill.o , fred.o

/* Create a header file */ lib.h = void bill(char *arg) ; void fred(int)

/* Create an executable file(which has main() function) / program.c = / This is program .c */

#include "lib.h" int main() { 8