P. 1
TDC368_week1

TDC368_week1

|Views: 34|Likes:
Published by Suneetha Reddy

More info:

Published by: Suneetha Reddy on Aug 04, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PPT, PDF, TXT or read online from Scribd
See more
See less

08/04/2011

pdf

text

original

TDC368 UNIX and Network Programming

Week 1:    Course Introduction Overview of UNIX Operating Systems Architecture UNIX process concepts

Camelia Zlatea, PhD Email: czlatea@cs.depaul.edu

Course Outline
y Overview of UNIX operating system architecture. y UNIX process concepts. Process control primitives (fork, exec, exit, wait, waitpid). y Filters and I/O Redirection. Interprocess Comunications (IPC): pipes, FIFO's. y Asynchronous Events. Signals. Signal Functions (kill, system, sleep, raise, alarm, pause, abort). y UNIX system calls: C library functions, C++ classes, POSIX API's. y UNIX File System: files, catalogs (inodes; types,size, permissions). y UNIX File I/O: unbuffered I/O functions (open ,close,seek,umask,read,write). y Standard I/O buffering (fopen, fclose, fflush). File Sharing (dup,dup2,fcntl).
UNIX Network Programming ± TDC368-901 Spring 2003 Page 2

Course Outline (cont.)
y Interprocess Communication: Shared Memories, Semaphores, POSIX Semaphores, lock files. y Socket API (TCP, UDP). y Design Methods for Client/Server Applications y UNIX shells. C-shell commands, shell variables. C-Shell Programming. y Managing UNIX projects (make). Compilation and Debugging. y UNIX shells: I/O redirection, filters(grep, sed), awk.

UNIX Network Programming ± TDC368-901

Spring 2003

Page 3

system call interface. students ± will understand the fundamental concepts of UNIX operating system and use them to create various multitasking applications. ANSI C and ANSI/ISO C++ functions and classes ± Will be familiar with Client/Server methods and Socket APIs ± Will be proficient in UNIX shell programming UNIX Network Programming ± TDC368-901 Spring 2003 Page 4 . ± Will be familiar with UNIX programming environment.Objectives  Upon completion of this course.

UNIX Network Programming ± TDC368-901 Spring 2003 Page 5 .Prerequisites  CSC 309  Basic understanding of operating systems and programming techniques  C/C++ will be used to illustrate the concepts presented in the lecture material and for the implementation of the programming assignments and the project.

hp. UNIX Network Programming Interprocess Communication. 1999. 2nd ed. Richard Stevens. Addison-Wesley. The Nooks & Crannies. Advanced Programming in the UNIX Environment.Texts  Dave Curry. John Shapley Gray. MA. Volume 2. Reading. UNIX Systems Programming for SVR4. 1996. Sebastopol.com/hpux/onlinedocs/B2355-90682/B2355-90682. W.html (system calls)     UNIX Network Programming ± TDC368-901 Spring 2003 Page 6 . 1992. Second Edition. Interprocess Communications in Unix. 1998. NJ.. Richard Stevens. Prentice Hall PTR. NJ. CA. Prentice Hall PTR. Upper Saddle River. O'Reilly & Assoc. HP-UX man pages http://docs. W.

The C Programming Language. 1992. O'Reilly and Associates. NJ.A Guide to Concurrency.A. 1996 Ellie Quingley. Spring 2003 Page 7     UNIX Network Programming ± TDC368-901 . Inc. Inc. NJ. Ritchie. and Multithreading . O'Reilly and Associates. K. 1997 Prabhat K. Englewood Cliffs. Englewood Cliffs. NJ. Sebastopol. Prentice Hall PTR. Communication. CA. Second Edition. Unix in a Nutshell. 1991. PrenticeHall. 1990..Robbins and S. CA. Brian W. Andleigh. Prentice Hall PTR. Kernighan and Dennis M. Robbins. 1989. Practical UNIX Programming . NJ. Sebastopol. Prentice-Hall.Other References   Daniel Gilly. UNIX Shells by Example. Andrew Oran and Steve Talbot. UNIX System Architecture. Managing Projects with make..

Class Work and Grading Programming Assignment 1 Programming Assignment 2 Midterm Programming Assignment 3 Final Project due by April 22th 20% due by May 6th 20% 20% May 13th due by May 27th 20% 20% due by June 10th UNIX Network Programming ± TDC368-901 Spring 2003 Page 8 .

you can use Linux system  Project submissions via COL (dlweb.edu ) ± use Submit Assignments link  Grading will be done on HP-UX or Linux ± An experimental report is required for each programming project » The the report should include the program design description and the test cases and test scenarios for the application.depaul.  Optional.depaul. UNIX Network Programming ± TDC368-901 Spring 2003 Page 9 .cti.edu server (running HP-UX).Programming Environment  All students will get an account on hawk.

9:00-9:30PM Where: Rm.edu Web: http://condor.cti.TDC368 ± Network Programming Instructor: Camelia Zlatea.depaul.depaul.depaul. course materials will be available from dlweb. x26149 (during office hours) or leave a message to setup a conference: +630-979-3621 Email: czlatea@cs.edu/~czlatea/TDC368/ (*) Note: * In addition.edu UNIX Network Programming ± TDC368-901 Spring 2003 Page 10 . PhD Office Hours: T 4:45-5:45PM. 429. ext.

Class Communication  COL (dlweb.edu) will be used primarily for: ± ± ± ± ± Links to course materials/assignments Grades Announcements Email/Communication Newsgroup  Asking questions ± Send me an email at czlatea@cs.cti.depaul.depaul.edu OR ± Use dlweb.depaul.cti.edu and post questions to class forum ‡ Anybody may reply (including your classmates) ‡ Everyone benefits from common issues UNIX Network Programming ± TDC368-901 Spring 2003 Page 11 .

at minimal cost UNIX Network Programming ± TDC368-901 Spring 2003 Page 12 . Ken Thompson.Richie ± UNIX re-written in C  1975. K.UNIX .Thompson&D. offered UNIX to educational institutions. Bell-Labs.Brief History  1969. ± UNIX OS written in Assembly on PDP-7 (DEC)  1973. Bell-Labs.

selectable per user basis UNIX Network Programming ± TDC368-901 Spring 2003 Page 13 .UNIX Features        Portability Multi-process architecture (multitasking) Multi-user capability Ability to initiate asynchronous processes A hierarchical file system Device independent I/O operations User interface: Shell.

tries to unify Berkeley UNIX & other  Berkeley UNIX. CSRG at U.UNIX Major Versions  AT&T UNIX version V ± 1983 /SV. 1986 /SVR3. of California ± BSD (Berkeley Software Distribution) UNIX Network Programming ± TDC368-901 Spring 2003 Page 14 .0 .0  AT&T and Sun Microsystems ± 1988 /SVR4.

American National Standard Institute  ANSI/ISO C++ Standard (draft) UNIX Network Programming ± TDC368-901 Spring 2003 Page 15 . based on SVID.UNIX Standards  System V Interface Definition (SVID). AT&T  Portable Operating System Interface for Computer Environments (POSIX). IEEE  ANSI C.

UNIX Implementations  Solaris . Solaris 2. Silicon Graphics.x based on SVR4  HP-UX. SVR4 UNIX Network Programming ± TDC368-901 Spring 2003 Page 16 . Linus Torvalds. free distribution. SVR2  Linux. PC-based  AIX. IBM. similar to SVR4  IRIX. Hewlett-Packard.Sun Microsystems ± SunOS (later called Solaris).

UNIX Implementations  UNIX-like ± same facilities as SV. kernel is based on OO model server ± Next (implementation inspired by Mach with MacOS UI)  UNIX-alike ± portability is no longer supported ± attributes not compliant with SV or BSD ± OS/9(MicroWare. Intel. RT kernel) ± QNX(Quantum Software. does not use entirely the same code ± Mach (CMU). RT processing) UNIX Network Programming ± TDC368-901 Spring 2003 Page 17 . Motorola 680xx.

UNIX Architectural Overview Applications Network Applications DBMS Utilities Shell¶s Executable Programs pipe. filters Commands System Calls Interface Kernel UNIX Hardware Architecture UNIX Network Programming ± TDC368-901 Spring 2003 Page 18 .

UNIX Features        Portability Multi-process architecture (multitasking) Multi-user capability Ability to initiate asynchronous processes A hierarchical file system Device independent I/O operations User interface: Shell. selectable per user basis UNIX Network Programming ± TDC368-901 Spring 2003 Page 19 .

base OS ± manages all HW dependent functions ± users have no direct access to it  System Calls Interface ± service routine performing user requests UNIX Network Programming ± TDC368-901 Spring 2003 Page 20 .Multi-process/Multi-user architecture  Virtual Machine ± timesharing OS ± process. process states  Kernel. process quantum.

 A process may terminate ± exit().UNIX Processes  A process may create sub-processes ± fork(). ± wait().  Processes ± synchronization mechanisms ± communication mechanisms UNIX Network Programming ± TDC368-901 Spring 2003 Page 21 . ± pause().  A process may put itself to sleep temporarily ± sleep(20).

UNIX Threads  Multiple Processes . register set and states ± context switches are avoided ± efficient mapping on multi-processor machines UNIX Network Programming ± TDC368-901 Spring 2003 Page 22 . PC.concurrency at process level ± thread = flow of control in a process ± multiple threads (stream of instructions) are executed within the same process ± threads share code & data (address space) ± threads have their own execution stack.concurrency at OS level  Multiple Threads .

Some Unix Terms  TRAP INSTRUCTION ± ± ± ± switches a system call from user to kernel mode handles error conditions synchronous execution Ex: division by zero/overflow  INTERRUPT ± used by peripherals to request services from the devices handles ± asynchronous execution  SIGNALS software notification of an event  example: ± when typing ³ctrl-c´ an interrupt is generated for the keyboard driver. which notifies the process by sending a signal UNIX Network Programming ± TDC368-901 Spring 2003 Page 23 .

Mgmt.UNIX Kernel . Space Kernel Space Scheduler Device Drivers Hardware I/O Buffers UNIX Network Programming ± TDC368-901 Spring 2003 Page 24 .model User User Processes System Call Interface (Library Routines) Process Memory File System I/O Services Mgmt.

/Utilities System Calls UNIX KERNEL Hardware Interrupts Hardware UNIX Network Programming ± TDC368-901 Spring 2003 Page 25 .Apps.

UNIX Kernel . scheduling.. dispatching Memory allocation and de-allocation Interrupt handling Low level device control Disk Mgmt.low level       Process representation. data buffering Process synchronization and IPC UNIX Network Programming ± TDC368-901 Spring 2003 Page 26 .

providing that requested service  Type of Services: ± ± ± ± process creation and termination I/O services UNIX file system services terminal handling services UNIX Network Programming ± TDC368-901 Spring 2003 Page 27 .services level  Maps user-level requests with device driver actions  A user system call is translated to a call of the kernel routine.Kernel .

³ps -e´). //process creation service execl(³/bin/ps´.services level Executable Programs User program: system(³ps -e´).Kernel . //code execution Library Functions System Calls Kernel Hardware UNIX Network Programming ± TDC368-901 Spring 2003 Page 28 . System calls actions: fork().

program can call kernel routines UNIX Network Programming ± TDC368-901 Spring 2003 Page 29 .System Call Interface level  A user mode process is translated into a protected kernel mode process  Now.

User Processes level  User processes running: ± ± ± ± shells Unix commands utilities application programs UNIX Network Programming ± TDC368-901 Spring 2003 Page 30 .

UNIX Standards     UNIX and POSIX API Library Functions ANSI C Standard ANSI/ISO C++ Standard UNIX Network Programming ± TDC368-901 Spring 2003 Page 31 .

1? #define _POSIX_SOURCE or % CC -D_POSIX_SOURCE *.C UNIX Network Programming ± TDC368-901 Spring 2003 Page 32 .UNIX and POSIX API  UNIX API .system calls  UNIX API are called by ± C library functions and ± C++ standard classes Example: iostream class  Program confirms POSIX.

UNIX and POSIX API  API set to perform: ± ± ± ± ± determine system configuration and user information file management process creation and management inter-process communication network communication UNIX Network Programming ± TDC368-901 Spring 2003 Page 33 .

UNIX and POSIX API User Process (User Mode of Execution) an API is invoked API execution completed UNIX API¶s level Kernel mo e of execution API executed in protected mode UNIX Network Programming ± TDC368-901 Spring 2003 Page 34 .

for the same task ± I/O lib.functions are buffered UNIX Network Programming ± TDC368-901 Spring 2003 Page 35 .UNIX and POSIX API  Context Switch from user to kernel mode ± more overhead than library functions.

UNIX and POSIX API  UNIX API .C UNIX Network Programming ± TDC368-901 Spring 2003 Page 36 .1? #define _POSIX_SOURCE or % CC -D_POSIX_SOURCE *.system calls  UNIX API are called by ± C library functions and ± C++ standard classes Example: iostream class  Program confirms POSIX.

libsocket.a and libc.so libsocket.h> function perror .so (on most systems) ± other special libraries.system diagnostic message for an API execution failure  Object Code ± libc.h> ± <sys> (/usr/include/sys directory) ± <stdio.UNIX and POSIX API Development elements:  Headers ± <unistd.a and UNIX Network Programming ± TDC368-901 Spring 2003 Page 37 .

UNIX and POSIX API #include <stdio. UNIX Network Programming ± TDC368-901 Spring 2003 Page 38 . #include <string.h> void perror(const char *s).h> char *strerror(int errnum). #include <errno.h> int errno.

Displaying the table of contents of the standard library: ar -t /usr/lib/libc.Library Functions  Library of functions ± object code of the functions stored in archives (library files) ± run-time library routines  UNIX archive ar utility manage library files.a UNIX Network Programming ± TDC368-901 Spring 2003 Page 39 .

ANSI C Standard  Standard C library functions: ± memory management ± get system information ± get/set time information ± data conversion. UNIX Network Programming ± TDC368-901 Spring 2003 Page 40 . processing  Advantages: ± portability ± maintainability ± cost effective for application design  Disadvantages: ± duration of execution ± weak support for time-critical apps.

<memory.h> .h> .so /usr/lib UNIX Network Programming ± TDC368-901 Spring 2003 Page 41 .h> .h> .h> .<stdlib.Standard C library functions  Header files directory:  Header files: .h> .<string.<stdio.a .<stdarg.h> .<assert.h> .<time.<setjmp.<getopt.h> /usr/include  Object code directory: ± libc.h> . libc.<malloc.

ferror unnamed pipes: popen/pclose UNIX Network Programming ± TDC368-901 Spring 2003 Page 42 .declares FILE data type  functions/macros -manage stream files open/close stream files: fopen/fclose read/write block data: fread/fwrite read/write lines: fgets/fputs write formatted data: fprintf positions/returns stream file location:fseek/ftell macros: feof.h> .Standard C library functions  <stdio.

} UNIX Network Programming ± TDC368-901 Spring 2003 Page 43 . /* echo */ pclose(cmd). /* read grep process execution output */ while(fgets(answer. stdout). return 0. if (!cmd) { perror(³popen´). return 1.h> int main() { /* execute the command */ FILE *cmd = popen(³ls -l|grep tdc368´.´r´).cmd)) fputs(answer.Example: ls -l|grep tdc368 #include <stdio. sizeof(answer). } char answer[256].

declares set of functions for data conversions. shell command execution. atof.h> . atol. strtol rand. process termination ± ± ± ± ± atoi. strtod.Standard C library functions  <stdlib. variables. putenv system exit UNIX Network Programming ± TDC368-901 Spring 2003 Page 44 . srand getenv. get/set env.

} UNIX Network Programming ± TDC368-901 Spring 2003 Page 45 .h> int main() { /* execute the command */ if (system(³ls -l|grep tdc368´)== -1) { perror(³system´). return 1.Example: ls -l|grep tdc368 #include <stdlib. } return 0.

h> int main() { char *env = getenv(³REMOTEHOST´). } UNIX Network Programming ± TDC368-901 Spring 2003 Page 46 .Example: getenv/putenv #include <iostream. return 0. cout << ³\REMOTEHOST\´ is:´<<env<<µ\n¶.h> #include <stdlib. if (putenv(³CC=c++´)) cer<<³putenv of CC failed\n´).

//C++ style . free(adr1). delete adr2.Example: <malloc. no use of malloc ! char* adr2 = new char[4096]. UNIX Network Programming ± TDC368-901 Spring 2003 Page 47 .h> //C style char* adr1 = (char *)malloc(4096).

h> .declares set of functions for system clock query time .#sec since 01/01/70 (UNIX birth) ctime .(ANSI C) . #microsec since the process first called clock UNIX Network Programming ± TDC368-901 Spring 2003 Page 48 .current local time localtime gmtime asctime mktime clock .Standard C library functions  <time.

cout <<³Duration:´<<(duration/tick)<<endl.start_time. clock_t start_time = clock().Example: <time. // start timer /* process code here */ clock_t duration = clock() .h> // evaluate process execution time #include <iostream.h> main() { time_t tick = CLOCKS_PER_SECOND. } UNIX Network Programming ± TDC368-901 Spring 2003 Page 49 .h> #include <time.

Standard C library functions 
<assert.h> - declares a macro to assert conditions or invariants of the program
± easier program debugging ± compiler option -DNDEBUG take them out or #ifndef NDEBUG #define assert(p) {if (!(p)) {\ fprintf(stderr,´Assertion failed: \ file:\´%s\´,line %d\n´,\ _FILE_, _LINE_); exit(1); } #endif

UNIX Network Programming ± TDC368-901

Spring 2003

Page 50

Example: <assert.h>
#include <fstream.h> #include <string.h> #include <assert.h> int main(int argc, char * argv[]) { assert(argc >1); // should have 1 arg ifstream ifs(argv[1]) assert(ifs.good()); // should be opened OK char *adr = new char[strlen(argv[1])+1]; assert(adr); // should not be NULL }

UNIX Network Programming ± TDC368-901

Spring 2003

Page 51

ANSI/ISO C++ Standard
1980, Bjarne Stroustrup, C++ 1994, ANSI/ISO C++ standard (draft) C++ compiler (AT&T C++ V3.0) support:
± ± ± ± ± C++ classes, derived classes virtual functions, operator overloading templates classes, functions exception handling iostream classes 

  

UNIX Network Programming ± TDC368-901

Spring 2003

Page 52

c file Man Pages man cc man sys_call man shell_cmd UNIX Network Programming ± TDC368-901 Spring 2003 Page 53 .Compilation cc -o file file.

Processes  Process .. SP.. .interface between OS and User process UNIX Network Programming ± TDC368-901 Spring 2003 Page 54 .)  Process Table .a program in execution ± process .linked list of structures associates w/ processes  System Calls .active entity ± program .passive entity (binary file)  Address Space .list of memory locations from where a process reads/writes (code/data/stack)  Set of registers (PC.

running. ready. terminated  Process Image Map Pointer  Process ID ± assigned at process creation  Program Counter (PC) ± address of next instruction to be executed     CPU Registers (saved process context) List of Open File Descriptors I/O Devices Attached CPU Scheduling Info (priority) UNIX Network Programming ± TDC368-901 Spring 2003 Page 55 . blocked.Process Control Block (process attributes)  Process State ± new.

n Data Segment Stack Segment Process Control Block UNIX Network Programming ± TDC368-901 Spring 2003 Page 56 . 1 Process Image Text/Code Segment Proc.Process Image Map Process Table Proc.

U N IX u a re a D a ta S tru c tu re s B u ffe rs K e rn e l c o d e L o w le v e l d e v ic e d riv e rs Spring 2003 Page 57 UNIX Network Programming ± TDC368-901 .. e n v iro n m e n t P ro c e s s In fo ... p ro c e ss i . S ta c k S y s te m S y s te m S y s te m a d d re s s a b le a re a ig h A r e ss S y ste m M e m o r y M a p p ro c e ss 0 p ro c e ss 1 ..P r o c e ss M e m o r y p ro g ra m M a p L o A r e ss te x t s e g m e n t s ta tic d a ta s ta tic d a ta U s e r a d d re s s a b le a re a in itia liz e d u n in itia liz e d h e a p s ta c k a rg c a rg v ....

&edata. &end).end.h> extern int etext.Example: /* Display Segment Address Information */ #include <stdio. void main(void) { printf(³etext: %6X\t edata: %6X \t end: %6X \n´. } UNIX Network Programming ± TDC368-901 Spring 2003 Page 58 .edata. &etext.

process created ( Ex: fork().) UNIX Network Programming ± TDC368-901 Spring 2003 Page 59 .  Terminated .wait for events to occur (inserted in queue) Ex: wait().Process States  New .normal/abnormal termination (exit().instructions are being executed  Blocked . pause(). )  Ready process is waiting to be assigned to processor (inserted in ready queue)  Running .

Process Model Ne wakeup Blocke / Suspen e created Rea y dispatch Quantum Expired Running sleep User Mo e exit System Call Return Running Interrupt Kernel Mo e Terminate UNIX Network Programming ± TDC368-901 Interrupt Interrupt return Spring 2003 Page 60 .

 Context of a Process ± ± ± ± ± process state (defined by it¶s code) value of u-area values of registers the process uses contents of user and kernel stacks is associated with process image map  Context Switching ± system executes a process in the context of the process ± when the kernel decides to execute another process. it does context switching ± kernel saves enough information such that it can later switch back to the first process and resumes its execution  Mode Switching ± moving from user to kernel mode ± kernel save information to return to user mode UNIX Network Programming ± TDC368-901 Spring 2003 Page 61 .

 User mode ± processes in use mode can access their own instructions and data. NOT kernel or other process¶s code or data  Kernel mode ± process can access system(kernel) code and data and user addresses ± Kernel is part of each process ± Kernel executes on behalf of the process P1 P2 P3 P4 OS HW Kernel Mode User Mode UNIX Network Programming ± TDC368-901 K U U K Spring 2003 Page 62 .

Context Switching P2 P1 OS Save state in PCB1 Reload state from PCB2 Save state in PCB2 Reload state from PCB1 UNIX Network Programming ± TDC368-901 Spring 2003 Page 63 .

Context Switching  Switching the CPU to another process by saving the state of the old process (PCB) and load the state of the new process (PCB)  Pure Overhead  Performance Bottleneck  Avoid Overhead of Context Switching by introducing new structures: THREADS UNIX Network Programming ± TDC368-901 Spring 2003 Page 64 .

Context Switching Multitasking Sequential Execution UNIX Network Programming ± TDC368-901 Spring 2003 Page 65 .

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->