Process Management Lecture 17 Review February 25, 2005 Program? Process? Thread? Disadvantages, advantages of threads? How do you identify processes? How do you fork a child process, the child process does some work, while parent process waits? Process Management Program? Process? Thread? Disadvantages, advantages of threads? How do you identify processes? How do you fork a child process, the child process does some work, while parent process waits? if (pid < 0) {exit(1); if (pid == 0) { /* child */ else { /* parent: wait/waitpid */ Process Management What does exec* do? What is a zombie process? Inter-Process Communication Why are pipes and fork() typically used together? Inter-Process Communication Why are pipes and fork() typically used together? fd[1] fd[0] What if you want to talk in both directions? 1
Shared Memory shmget-shmat-shmdt-shmctl key + IPC_CREAT SHM_RDONLY Race Condition int add_element(int element) { if (index == 32) return -1; array[index] = element; index = index + 1; return (index); Semaphores: UP, DOWN? Mutual exclusion, critical sections? Possible problem: solve race condition in code above... Condition Variables void *handler_fct(void *arg) { // handle client pthread_mutex_lock(&at_mutex); active_threads--; pthread_cond_signal(&at_cond); pthread_mutex_unlock(&at_mutex); return(); Condition Variables active_threads = 0; while (1) { pthread_mutex_lock(&at_mutex); while (active_threads < n) { active_threads++; pthread_start(...); pthread_cond_wait(&at_cond, &at_mutex); pthread_mutex_unlock(&at_mutex); OSI Reference Model Layered model: 7. Application 6. Presentation 5. Session 4. Transport 3. Network 2. Data Link 1. Physical OSI Reference Model Layered model: 7. Application 6. Presentation 5. Session 4. Transport 3. Network 2. Data Link 1. Physical Where does UDP/TCP belong to? Ethernet? Bluetooth? IEEE 802.11abg? HTTP? IP? 2
UDP versus TCP versus IP IP offers no guarantee: packets may get lost. packets may be delivered twice. packets may be delivered in the wrong order. packets may be corrupted during transfer. UDP is very similar to IP: send/receive packets, no guarantee. packets are called datagrams. TCP: reliable, data flow, timeouts, ordered, error detection... File I/O What does blocking/non-blocking mean? fd = open(pathname, flags, [mode]); what s the flag for reading only? what if I want to append data at the end of the file? what if I do not want to open it if it already exists? File I/O What does blocking/non-blocking mean? fd = open(pathname, flags, [mode]); what s the flag for reading only? O_RDONLY what if I want to append data at the end of the file? O_APPEND what if I do not want to open it if it already exists? O_EXCL (together with O_CREAT) Positioning off_t lseek(int fd, off_t offset, int whence); SEEK_SET SEEK_CUR SEEK_END File Permissions permission bits: r,x,w r: read w: write x: execute Files drwxrwxrwx 4 cpoellab team1 122 Dec 12 18:02 Assignments Pos: 1 2 3 4 5 6 7 8 9 10 -dl r w x r w x r w x 3
Files What is umask used for? drwxrwxrwx 4 cpoellab team1 122 Dec 12 18:02 Assignments type and permission (drwxrwxrwx) number of links: 4 owner: cpoellab file s group: team1 size in bytes (122) date of last modification (Dec 12 18:02) filename: Assignments Type: d = directory l = symbolic link s = socket p = named pipe - = regular file c = character special file b = block special file Set and determine the default file creation permissions on the system: 777 - executable files 666 - text files Permission for creation of new executable is calculated by subtracting the umask value: 666-022=644 umask 022 /etc/profile Client-Server SERVER socket() bind() listen() accept() read() write() blocks until connection from client connection establishment process request data request data reply CLIENT socket() connect() write() read() TCP Client #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> extern struct in_addr host2ip (char *host); int main(int argc, char *argv[]) { int s, n; struct sockaddr_in sin; char msg[80] = Hello, World! ; if ((s = socket(pf_inet, SOCK_STREAM, 0)) < 0) { perror( socket ); return(-1); TCP Server TCP Server int main(int argc, char *argv[]) { int s, t, n; struct sockaddr_in sin; char *r; int sinlen; if ((s = socket(pf_inet, SOCK_STREAM, 0)) < 0) { perror( socket); return -1; sin.sin_family = AF_INET; sin.sin_port = htons(13333); sin.sin_addr.s_addr = INADDR_ANY; if (bind (s, (struct sockaddr *) &sin, sizeof(sin)) < 0) { perror( bind ); return -1; if (listen(s, 5) < 0) { perror( listen ); return -1; for (;;) { sinlen = sizeof(sin); if ((t = accept(s, (struct sockaddr *) &sin, &sinlen)) < 0) { perror( accept ); return -1; r = gettime(); if (write(t, r, strlen(r)) < 0) { perror ( write ); return -1; if (close(t) < 0) { perror( close ); return -1; if (close(s) < 0) { perror( close ); return -1; 4
Iterative vs Concurrent Iterative servers: process one request at a time. Concurrent server: process multiple requests simultaneously. Concurrent: better use of resources (service others while waiting) and incoming requests can start being processed immediately after reception. Multi-threaded approach void sig_chld(int) { while (waitpid(0, NULL, WNOHANG) > 0) { signal(sigchld, sig_chld); int main() { int fd, newfd, pid; signal(sigchld, sig_chld); while (1) { newfd = accept(fd,...); if (newfd < 0) continue; pid = fork(); if (pid == 0) { handle_request(newfd); exit(0); else {close(newfd); Process Pools #define NB_PROC 10 void recv_requests(int fd) { int f; while (1) { f = accept(fd,...); handle_request(f); close(f); int main() { int fd; for (int i=0; i<nb_proc; i++) { if (fork() == 0) recv_requests(fd); while (1) pause(); select() int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); nfds: highest number assigned to a descriptor. block until >=1 file descriptors have something to be read, written, or timeout. set bit mask for descriptors to watch using FD_SET. returns with bits for ready descriptor set: check with FD_ISSET. cannot specify amount of data ready. fd_set void FD_ZERO(fd_set *fdset); void FD_SET(int fd, fd_set *fdset); void FD_CLR(int fd, fd_set *fdset); int FD_ISSET(int fd, fd_set *fdset); Why inetd/xinetd Separation of master server and actual services. Allows separate maintenance or replacement without recompilation. Called super server. Services can easily be added/removed/replaced. Static configuration: when server starts up (file: services + executable programs), changes require restart. Dynamic configuration: same as above but admin can notify server of changes (e.g., signals), no restart required. 5
hostent RPC Multiple addresses possible. h_addr: for compatibility, refers to first location in the host address list. struct hostent *host; char *thishostname = wizard.cse.nd.edu ; if (host = gethostbyname(thishostname)) { /* access address in host->h_addr */ else { /* error handling */ memcpy((char*)&sa.sin_addr, (char*)host->h_addr, host->h_length); main() stub(12); return 5; stub Request Reply skeleton func(12); return 5; func() Client Server RPC Specification.x file client.c add.x add_clnt.c add.h add_xdr.c client.o add_clnt.o add_xdr.o client Step 1: Write a specification file (add.x) struct add_in { /* Arguments of procedure */ long arg1; long arg2; ; typedef long add_out; /* Return value */ serverproc.c You write these files add_scv.c rpcgen generates these files add_svc.o serverproc.o You compile every C file server You obtain a client & server program ADD_PROG { version ADD_VERS { add_out ADD_PROC(add_in) = 1 /* Procedure# = 1 */ = 1; /* Version# = 1 */ = 0x3434000; /* Program# = 0x3434000 */ RPC RMI Purpose of port mapper? Purpose of XDR? How do you indicate direction of conversion? What s the buffer paradigm? Client Client Remote Object Interface Stub Server Remote Object Implementation Remote Object Interface Skeleton You implement this RMI implements this 6
Interface More Questions? Step 1: write interface Calculator.java import java.rmi.remote; import java.rmi.remoteexception; public interface Calculator extends Remote { public long add(long a, long b) throws RemoteException; public long sub(long a, long b) throws RemoteException; public long mul(long a, long b) throws RemoteException; public long div(long a, long b) throws RemoteException; Good Luck! 7