1 / 68

TDC561 Network Programming

TDC561 Network Programming. Review UNIX Architecture and Programming. Camelia Zlatea, PhD Email: czlatea@cs.depaul.edu. References. W. Richard Stevens, Network Programming, Vol, I, 2 nd Ed, Prentice Hall PTR, NJ, 1998.

sophie
Download Presentation

TDC561 Network Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TDC561 Network Programming Review UNIX Architecture and Programming Camelia Zlatea, PhD Email: czlatea@cs.depaul.edu

  2. References • W. Richard Stevens, Network Programming, Vol, I, 2nd Ed, Prentice Hall PTR, NJ, 1998. • John Shapley Gray, Interprocess Communications in UNIX -- The Nooks and Crannies Prentice Hall PTR, NJ, 1998. • HP-UX man pages http://docs.hp.com/hpux/onlinedocs/B2355-90682/B2355-90682.html (system calls)

  3. Overview UNIX Architecture UNIX processes, threads UNIX program development System calls – fork, exec, etc.

  4. UNIX Architectural Overview Applications Network Applications DBMS Utilities Shell’s Executable Programs Commands pipe, filters System Calls Interface Kernel UNIX Hardware Architecture

  5. 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 Features

  6. System V Interface Definition (SVID), AT&T Portable Operating System Interface for Computer Environments (POSIX), based on SVID, IEEE ANSI C, American National Standard Institute ANSI/ISO C++ Standard (draft) UNIX Standards

  7. Solaris - Sun Microsystems SunOS (later called Solaris), Solaris 2.x based on SVR4 HP-UX, Hewlett-Packard, SVR2 Linux, Linus Torvalds, free distribution, PC-based AIX, IBM, similar to SVR4 IRIX, Silicon Graphics, SVR4 UNIX Implementations

  8. UNIX Architectural Overview Applications Network Applications DBMS Utilities Shell’s Executable Programs Commands pipe, filters System Calls Interface Kernel UNIX Hardware Architecture

  9. 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 Features

  10. Multi-process/Multi-user architecture • Virtual Machine • timesharing OS • process, process quantum, process states • Kernel, base OS • manages all HW dependent functions • users have no direct access to it • System Calls Interface • service routine performing user requests

  11. A process may create sub-processes fork(); A process may terminate exit(); A process may put itself to sleep temporarily sleep(20); pause(); wait(); Processes synchronization mechanisms communication mechanisms UNIX Processes

  12. Multiple Processes - concurrency at OS level Multiple Threads - 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, PC, register set and states context switches are avoided efficient mapping on multi-processor machines UNIX Threads

  13. UNIX Kernel - model User Space User Processes System Call Interface (Library Routines) Process Memory File System I/O Services Mgmt. Mgmt. Kernel Space Scheduler Device Drivers I/O Buffers Hardware

  14. Apps./Utilities System Calls UNIX KERNEL Hardware Interrupts Hardware

  15. Process representation, scheduling, dispatching Memory allocation and de-allocation Interrupt handling Low level device control Disk Mgmt., data buffering Process synchronization and IPC UNIX Kernel - low level

  16. Maps user-level requests with device driver actions A user system call is translated to a call of the kernel routine, providing that requested service Type of Services: process creation and termination I/O services UNIX file system services terminal handling services Kernel - services level

  17. User program: system(“ps -e”); System calls actions: fork(); //process creation service execl(“/bin/ps”, “ps -e”); //code execution Kernel - services level

  18. A user mode process is translated into a protected kernel mode process Now, program can call kernel routines System Call Interface level

  19. User processes running: shells Unix commands utilities application programs User Processes level

  20. 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.1? #define _POSIX_SOURCE or % CC -D_POSIX_SOURCE *.C

  21. 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

  22. UNIX and POSIX API User Process (User Mode of Execution) an API is invoked API execution completed UNIX API’s level Kernel mode of execution API executed in protected mode

  23. Context Switch from user to kernel mode more overhead than library functions, for the same task I/O lib.functions are buffered UNIX and POSIX API

  24. 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

  25. Some Unix Terms • 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

  26. Process - a program in execution process - 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, SP, ...) Process Table - linked list of structures associates w/ processes System Calls - interface between OS and User process Processes

  27. Process State new, ready, running, blocked, 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) Process Control Block (process attributes)

  28. Process Image Map Process Table Process Image Proc. n Proc. 1 Text/Code Segment Data Segment Stack Segment Process Control Block

  29. Example: /* Display Segment Address Information */ #include <stdio.h> extern int etext,edata,end; void main(void) { printf(“etext: %6X\t edata: %6X \t end: %6X \n”, &etext, &edata, &end); }

  30. New - process created ( Ex: fork(); ) Ready - process is waiting to be assigned to processor (inserted in ready queue) Running - instructions are being executed Blocked - wait for events to occur (inserted in queue) Ex: wait(); pause(); Terminated - normal/abnormal termination (exit();) Process States

  31. Process Model New created wakeup Ready Quantum Expired dispatch Blocked/ Suspended Running User Mode sleep exit System Call Interrupt Return Running Kernel Mode Terminated Interrupt Interrupt return

  32. 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

  33. 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 K K U U

  34. Context Switching P2 P1 OS Save state in PCB1 Reload state from PCB2 Save state in PCB2 Reload state from PCB1

  35. 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 Context Switching

  36. Multitasking Sequential Execution Context Switching

  37. Compilation cc -o file file.c file Man Pages man cc man sys_call man shell_cmd

  38. UNIX PROCESSES Process - system support Fork system call Process states Exec system call Exit function Background processes Parent-Child Synchronization (wait) File Sharing Examples

  39. A fork system call: pid_t pid; pid = fork(); if (pid ==-1) {/* fork failure, no more entries in process table*/} else if (pid==0){/*child process*/} else {/* parent process */}

  40. The user process calls fork(); Store system call parameters arguments, return address, local variables) into the user stack. Call corresponding kernel service execution of a trap instruction In kernel space save: parameters, return address and local variables for kernel routine Execute kernel routine Normal Return cleans-up kernel stack switch from kernel to user mode Fork - System Call

  41. Successful return from system call: parent and child processes: share same text segment identical data segments identical user & kernel stack segments same user structure parent normal return restores return address from user stack child “pseudo return” restores same return address as the parent from its user stack Fork - System Call

  42. Parent Process Child Process File Table text data stack I-node Table Open Files Current Directory text data stack Open Files Current Directory

  43. #include <sys/types.h> #include <unistd.h> pid_t getpid(void); /* get current process ID */ pid_t getppid(void); /* get ID of the parent of the process */ Process information

  44. pid_t pid; static int x; x=1; pid = fork(); if (pid < 0 ) {perror(“fork failure”)} else if (pid==0) x++; else x--; printf(“process %d: x=%d\n”, getpid(), x); What value(s) are printed for variable x?

  45. #include <stdio.h> #include <sys/types.h> #include <unistd.h> void main(void) { fork(); printf(“A\n”); fork(); printf(“B\n”); fork(); printf(“C\n”); } Comment on the above program output.

  46. #include <stdio.h> #include <sys/types.h> #include <unistd.h> void main(void) { int i; for (i=1; i<=3; i++) { fork(); printf(“PID=%d i=%d\n”, getpid(), i); } printf(“PID=%d i=%d\n”, getpid(), i); } Comment on the above program output.

  47. #include <stdio.h> #include <sys/types.h> #include <unistd.h> void main(void) { int i; for (i=1; i<=3; i++) { if (fork()==0) break; printf(“PID=%d i=%d\n”, getpid(), i); } printf(“PID=%d i=%d\n”, getpid(), i); } Comment on the above program output.

  48. #include <stdio.h> #include <sys/types.h> #include <unistd.h> void main(void) { int i; for (i=1; i<==3; i++) { if (fork()>0) break; printf(“PID=%d i=%d\n”, getpid(), i); } printf(“PID=%d i=%d\n”, getpid(), i); } Comment on the above program output.

  49. Initiated - fork() Ready-to-Run in ready queue for CPU access Running process quantum Blocked sleep(n); /* deterministic delay */ pause(); /* non-deterministic delay */ Terminated exit(int status); Shell command: %ps [options] Process States

More Related