TDC561 Network Programming Camelia Zlatea, PhD Email: czlatea@ cs . depaul . edu Review UNIX Architecture and Programming
Jan 26, 2016
TDC561 Network Programming
Camelia Zlatea, PhD
Email: [email protected]
Review
UNIX Architecture and Programming
Network Programming (TDC561) Page 2Winter 2003
References
W. Richard Stevens, Network Programming, Vol, I, 2nd Ed, Prentice Hall PTR, NJ, 1998.
W. Richard Stevens, Advanced UNIX Programming, Vol, I, 2nd Ed, Prentice Hall PTR, NJ, 1998.– Links to Richard Stevens books; http://www.kohala.com/start/
John Shapley Gray, Interprocess Communications in UNIX -- The Nooks and Crannies Prentice Hall PTR, NJ, 1998.
Web Other resources about UNIX programming:– Brief tutorial on C and UNIX programming
http://www.strath.ac.uk/IT/Docs/Ccourse/– Source code from textbook (Network Programming - Stevens)
http://www.kohala.com/start/unpv12e.html– link to Stevens - UNIX programming book;
http://www.kohala.com/start/apue.html– UNIX Files Systems and I/O programming – guide;
http://www.nd.edu/~lemmon/courses/UNIX/l3/l3.html– HP-UX man pages http://docs.hp.com/hpux/
onlinedocs/B2355-90682/B2355-90682.html (system calls)
Overview
UNIX ArchitectureUNIX processes, threadsUNIX program developmentSystem calls – fork, exec, etc.
Network Programming (TDC561) Page 4Winter 2003
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
Network Programming (TDC561) Page 5Winter 2003
UNIX Standards
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)
Network Programming (TDC561) Page 6Winter 2003
UNIX Implementations
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
Network Programming (TDC561) Page 7Winter 2003
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
Network Programming (TDC561) Page 8Winter 2003
UNIX Architectural Overview
Hardware Architecture
Kernel UNIX
System Calls Interface
Shell’s Executable Programs
pipe, filters Commands
Applications Network Applications
DBMS Utilities
Network Programming (TDC561) Page 9Winter 2003
UNIX Kernel - model
Process Memory File System I/O Services Mgmt. Mgmt.
Kernel
Space
Hardware
System Call Interface (Library Routines)
Scheduler Device Drivers I/O Buffers
User Processes
User
Space
Process Memory File System I/O Services Mgmt. Mgmt.
Network Programming (TDC561) Page 10Winter 2003
UNIX Kernel Kernel - low level functions
– Process representation, scheduling, dispatching– Memory allocation and de-allocation– Interrupt handling– Low level device control– Disk Mgmt., data buffering– Process synchronization and IPC (Inter-Process Communication)
Kernel - services level– 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
System Call Interface level– A user mode process is translated into a protected kernel mode process– Now, program can call kernel routines
Network Programming (TDC561) Page 11Winter 2003
User Processes level
User processes running:– shells
– Unix commands
– utilities
– application programs
Network Programming (TDC561) Page 12Winter 2003
UNIX and POSIX API
UNIX API - system calls UNIX API are called by
– C library functions and
– C++ standard classes
Example: iostream class
API set to perform:– determine system configuration and user information
– file management
– process creation and management
– inter-process communication
– network communication
Network Programming (TDC561) Page 13Winter 2003
UNIX and POSIX API
User Process (User Mode of Execution)
Kernel mode of execution
UNIX API’s level
an API is invoked
API executed
in protected mode
API execution
completed
Context Switch from user to kernel mode
–more overhead than library functions, for the same task
–I/O lib.functions are buffered
Network Programming (TDC561) Page 14Winter 2003
UNIX Processes – APIs
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
Network Programming (TDC561) Page 15Winter 2003
UNIX Threads
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
Network Programming (TDC561) Page 16Winter 2003
Processes
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
Network Programming (TDC561) Page 17Winter 2003
Process Control Block (process attributes)
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)
Network Programming (TDC561) Page 18Winter 2003
Process Image Map (simplified)
Proc. n
Proc. 1Process Image
Process Table
Text/Code Segment
Data Segment
Stack Segment
Process Control Block
Network Programming (TDC561) Page 19Winter 2003
Process Memory Map
High Address
System Memory Map
process 0process 1.....
process i....
program text segment
initialized static data
uninitialized static data
heap
stack
argc argv; environment
System Process Info.System Stack
User addressablearea
Systemaddressablearea
Low Address
UNIX Data Structures
u area
Buffers
Kernel code
Low level device drivers
Network Programming (TDC561) Page 20Winter 2003
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);
}
Network Programming (TDC561) Page 21Winter 2003
Process States
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();)
Network Programming (TDC561) Page 22Winter 2003
Process Model
New
Ready
Blocked/Suspended
RunningUser Mode
Terminated
exitsleep
wakeup
dispatch
created
QuantumExpired
RunningKernel Mode
System Call
InterruptReturn
InterruptInterrupt return
Network Programming (TDC561) Page 23Winter 2003
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
Network Programming (TDC561) Page 24Winter 2003
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
Kernel Mode
User Mode
K K
U U
OSHW
Network Programming (TDC561) Page 25Winter 2003
Context Switching
P1 P2
Save state in PCB1
Reload state from PCB2
Save state in PCB2
Reload state from PCB1
OS
Network Programming (TDC561) Page 26Winter 2003
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
Network Programming (TDC561) Page 27Winter 2003
Compilation (ANSI C)
cc -o file file.c
file
Man Pages
man cc
man <sys_call> , example: man socket
man <shell_cmd>, example: man ps
Network Programming (TDC561) Page 28Winter 2003
Process - system support Fork system call Process states Exec system call Exit function Background processes Parent-Child Synchronization (wait)
More about UNIX Processes
Network Programming (TDC561) Page 29Winter 2003
A fork system call:
pid_t pid;pid = fork();if (pid ==-1)
{ /* fork failure, no more entries in process table*/ }
elseif (pid==0){
/*child process*/ }
else { /* parent process */ }
Network Programming (TDC561) Page 30Winter 2003
Fork - System Call
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
Network Programming (TDC561) Page 31Winter 2003
Fork - System Call
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
Network Programming (TDC561) Page 32Winter 2003
Parent Process
Child Process
File Table
i-node Table
textdatastack
textdatastack
Open FilesCurrent Directory
Open FilesCurrent Directory
Network Programming (TDC561) Page 33Winter 2003
Process information
#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 */
Network Programming (TDC561) Page 34Winter 2003
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?
Race Conditions
Race Condition
-cannot predict if parent or child will run first after fork()
Network Programming (TDC561) Page 35Winter 2003
#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.
Race Conditions
Race Condition
-cannot predict if parent or child will run first after fork()
Network Programming (TDC561) Page 36Winter 2003
#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.
Race Conditions
Race Condition
-cannot predict if parent or child will run first after fork()
Network Programming (TDC561) Page 37Winter 2003
#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.
Race Conditions
Race Condition
-cannot predict if parent or child will run first after fork()
Network Programming (TDC561) Page 38Winter 2003
#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.
Race Conditions
Race Condition
-cannot predict if parent or child will run first after fork()
Network Programming (TDC561) Page 39Winter 2003
Process States
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]
Network Programming (TDC561) Page 40Winter 2003
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
void main(void) {
static char buf[2];
if (fork()==0) strcpy(buf,”A\n”);
else strcpy(buf,”B\n”);
sleep(10);
write(1,buf,sizeof(buf));
}
Comment on the above program execution
Race Condition
-cannot predict if parent or child will run first after fork()
Network Programming (TDC561) Page 41Winter 2003
Exec - System Call
#include <unistd.h>
int execl(const char *file, const char arg0,
.. , const argn);
int execv(const char *file, char *argv[]);
the process executes the the binary code from file if failure return -1 if success does not return
Network Programming (TDC561) Page 42Winter 2003
File test1.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void main(void) {
int i;
for (i=1; i<=3;i++)
if (fork()==0) { /* process child */
execv(“/bin/ps”,”ps -el”);
}
else { /* process partent */
printf(“Parent: %d\n”,getpid());
}
} cc -o test1 test1.c test1
Network Programming (TDC561) Page 43Winter 2003
File test2.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void main(void) {
int i;
for (i=1; i<=3;i++)
if (fork()==0) { /* process child */
execv(“child”,”child”);
}
else { /* process partent */
printf(“Parent: %d\n”,getpid());
}
}
cc -o test2 test2.c
Network Programming (TDC561) Page 44Winter 2003
File child.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void main(void) {
pid_t pid;
printf(“Parent: %d\n”,getpid());
sleep(30);
}
cc -o test2 test2.c
cc -o child child.c
test2
Network Programming (TDC561) Page 45Winter 2003
Exit - Function Call
#include <stdlib.h>
void exit(int status);
status - parameter returned to parent process status=0 - normal termination status# - abnormal termination Effects:
– close all open file descriptors
– notifies the parent (by signal)
– return status info to parent
– if parent no longer exists then PPD:=1 (is adopted by process init)
Network Programming (TDC561) Page 46Winter 2003
_exit - System Call
#include <unistd.h>
void _exit(int status);
status - process exit status code status=0 - normal termination status#0 - abnormal termination Effects:
– terminates a process
– closes all open file descriptors
– deallocates all process data and stack segment
– process becomes a zombie (it is no longer scheduled to run)
– init process clean up process table slot
Network Programming (TDC561) Page 47Winter 2003
_exit - System Call
#include <iostream.h>#include <unistd.h>int main()
{cout << "Test for _exit" << endl;
_exit(0);
return 0;
}% g++ -o test test.c; testTest for _exit% echo $status0
Network Programming (TDC561) Page 48Winter 2003
wait - System Call
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
returns -,1 if failure (check errno)– no unwaited-for child process
– interrupt signal
returns the child PID, is success synchronization:
– parent waits for a child to terminate
– if more child-processes, waits for any
Network Programming (TDC561) Page 49Winter 2003
waitpid - System Call
#include <sys/types.h>
#include <sys/wait.h>
pid_t waitpid(pid_t p, int *status, int opt);
Return value - a child process ID or -1 Argument pid_t p - a child PID
-1 waits for any child (same as wait)
0 waits for any child in same group w/ parent
>0 waits for child process with this PID=p
<-1 waits for any child with GID = |p|
Network Programming (TDC561) Page 50Winter 2003
waitpid - System Call
#include <sys/types.h>
#include <sys/wait.h>
pid_t waitpid(pid_t p, int *status, int opt);
Options valuesWNOHANG - do not block if status cannot be obtained
WNOWAIT - keep process in wait status
WUNTRACED - wait any stopped child process
Network Programming (TDC561) Page 51Winter 2003
Parent-Child Process synchronization
#include <stdio.h>#include <sys/types.h>#include <unistd.h>#include <sys/wait.h>void main(void) {pid_t pid; int i, status;for (i=1; i<=3;i++)
if (fork()==0) execv(“child”,”child”);else
printf(“Parent: %d\n”,getpid());while (pid = wait(&status) && pid!=-1)
printf(“Child %d is done\n”,getpid());exit(0);}
Network Programming (TDC561) Page 52Winter 2003
Parent-Child Process Synchronization (by priorities)#include <stdio.h>#include <sys/types.h>#include <unistd.h>#include <sys/wait.h>void main(void) {pid_t pid[3], p; int i, status;for (i=0; i<=3;i++)
if ((pid[i]=fork())==0) execv(“child”,”child”);
else printf(“Parent: %d\n”,getpid());
for(i=0;(p=waitpid(pid[i], &status,0)&&p!=-1;i++) if (p!=-1) && (errno==EINTR) printf(“Signal Caught: %s\n”,strerrno(errno));
exit(0);}
Network Programming (TDC561) Page 53Winter 2003
Processes Hierarchy
Pid=0 swapper
Pid=1
init
Pid
gtty
Pid
sh
Pid
sh
fork - by initexec gtty
fork - by initexec gttyexec loginexec sh
Pid
ls -l
fork - by shexec ls
Network Programming (TDC561) Page 54Winter 2003
Shell Processes
Execution of a Shell command – [fork] new process
– [exec] command
– [wait] shell waits for child process to terminate
ps -al
Network Programming (TDC561) Page 55Winter 2003
Background Processes
Shell does not wait for child process to complete Returns prompter immediately
sleep 30 & shell: [fork] [fork] [exec]
– shell child exits immediately after the second [fork], returns prompter
– shell grand-child [exec] the command in background
– it is adopted by process init
Network Programming (TDC561) Page 56Winter 2003
Errors API
#include <stdio.h>
void perror(const char *s);
#include <errno.h>
int errno;
#include <string.h>
char *strerror(int errnum);