COSC 350 System Programming Dr. Xiaohong (Sophie) Wang Department of Mathematics & Computer Science Email: xswang@salisbury.edu Office: HS122 Phone: (410) 677 5380
Course Outline UNIX/Linux basics & programming in Linux Working with files Terminals Processes IPC with signals and pipes Socket programming Perl programming (time permit)
Course Syllabus http://faculty.salisbury.edu/~xswang/ Courses/Csc350/cosc350.htm
0. Introduction Two kinds of computer software: System programs: manage the operation of the computer itself (operating systems, device drivers, etc) Application programs: solve problems for the computer users (compilers, editors, word processors)
What is Operating System? System program that controls all the computer’s resources and provides the base on which the application programs can be written. As an extended machines Present user with the equivalent of an extended machine that is easier to program than the underlying hardware As a resource manager Keep track of who is using which resource, to grant resource, to account for usage, and to mediate conflicting requests from different programs and users.
System calls A set of instructions provided by the operating system as the interface between the operating system and the user programs.
Kinds of system calls: File and directory management Process management Signaling Protection Time management
Organization of the OS GUI, Shell programming shell Kernel
Organization of the OS Kernel Shell Interacts with the hardware (know about devices, file structure, etc) Is built for a specific piece of hardware Has only a few user-level commands such as cp, mv, rm and file creation and manipulation Shell A program runs by the kernel Accepts user commands and presents them to the kernel Many varieties
Shell can work in Interactive mode Shell scripts Display prompt Read command Interpret command Execute command Shell scripts Write all shell commands into an executable file Run the executable file within the shell
History and Evolution of UNIX at Bell lab by Thompson & Ritchie in 1969 Two basic varieties: 4.3+BDS System V Release 4 Other vendors: IBM AIX Sun Solaris HP HP-UX SCO Unixware
Ideas behind UNIX Multi-users and multi-processes time slicing foreground and background Hierarchical directory structure (tree structure) Large number of small tools
UNIX Philosophy Simplicity Focus Reusable components Filters Open file format Flexibility
What is Linux? A freely distributed implementation of a UNIX-like kernel Low-level core of an operating system Similar to UNIX Initially developed by Linus Torvalds at University of Helsinki Developers from across the Internet
Programming Linux Linux programs (executables and scripts) The C compiler - gcc Library files Static libraries Shared libraries Getting help man page info
Libraries Collections of precompiled functions Standard system libraries are usually stored in /lib or /usr/lib A library file name starts with lib followed by what library this is (e.g., libc for c library, or libm for mathematical library) The last part of the name is .a for traditional, static libraries .so for shared libraries Compiler needs to be told which library to look for $gcc –o fred fred.c /usr/lib/libm.a $gcc –o fred fred.c –lm $gcc –o x11fred –L/usr/openwin/lib x11fred.c –lX11
Static libraries Collection of object files kept together To use those functions, include the header file The compiler and linker take care of combining the program code and library into a single executable -l is needed to indicate which libraries other than standard C library
Try it out - Static libraries Create source files #include <stdio.h> void fred(int arg) { printf("fred: you passed %d\n", arg); } void bill(char *arg) printf("bill: you passed %s\n", arg);
Compile those functions %gcc –c bill.c fred.c %ls *.o Header file for the library /* This is lib.h. It declares the functions fred and bill for users */ void bill(char *); void fred(int);
The calling program (program.c) #include "lib.h" int main() { bill("Hello World"); exit(0); } Compile the program %gcc –c program.c %gcc –o program program.o bill.o %./program Bill: you passed Hello World %
Create the library %ar crv libfoo.a bill.o fred.o a - bill.o a - fred.o Some systems (Berkeley UNIX) requires to create the table of content for the library (under Linux is not necessary) %ranlib libfoo.a To use the library %gcc –o program program.o –L. -lfoo
Shared libraries Problem with static library – many copies of the same function in the memory and disk space. When shared library is used, it is not included in the calling program. A reference to the library is made available to the calling program at run time. When the calling program is loaded for execution, the reference is resolved and calls made to the shared library, which is loaded into memory if needed. To see which shared libraries are required by a program $ldd program libc.so.6 => /lib/libc.so.6(0x4002a000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
Getting help On-line manual page On-line documentation system $man gcc $info gcc