% lec 20: concurrency intro # administrivia hw4 due next Thursday final exam March 18th (Wednesday) # today's plan sockets revisited processes & threads # client & server sockets `getaddrinfo`: convert hostname port to sockaddr `socket` / `connect` send a file over network: `read`/`write`, `mmap`, or `sendfile` `bind` / `listen` / `accept` (see ex15/ex16 reference solutions) `_POSIX_C_SOURCE`: see Section 3 or gcc manual ([macros](http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html), [-std](https://gcc.gnu.org/onlinedocs/gcc/Standards.html)) [draw front-end/back-end servers] # files & file descriptors standard Unix abstractions: files, sockets, pipes, ... /dev/[u]random: cat /dev/random | xxd [procfs](http://en.wikipedia.org/wiki/Procfs): /proc/cpuinfo see also [lec 3: /proc/PID/maps](l03-mm.html#/staticautomaticdynamic-alloc) # echo server [lec 18: echo server](l18-sockets.html#/echo-server) problem: one connection at a time goal: handle multiple connections approaches: multiplexing / virtualization # concurrency: multiplexing event-driven select/poll [epoll (Linux)](http://en.wikipedia.org/wiki/Epoll), [kqueue (BSD)](http://en.wikipedia.org/wiki/Kqueue), [I/O completion ports (Windows)](http://en.wikipedia.org/wiki/Input/output_completion_port) hard to program: your application is the scheduler examples: nginx, node.js # concurrency: virtualization processes threads OS scheduling example: Apache # processes [CSE 351: processes](https://courses.cs.washington.edu/courses/cse351/15wi/lectures/09-processes_wi15.pptx) virtualization: processor & address space # fork ```c pid_t fork(void); ``` returns -1 if error, 0 in child process, pid in parent process child: inherits a copy of parent's file descriptors parent: should `wait` on child or `signal(SIGCHLD, SIG_IGN)` Linux: actually uses `clone`, inspired by Plan 9 # fork: echo server `fork`: concurrency through virtualization [echosrv-fork.c](l20/echosrv-fork.c.html) . . . add a global counter? # fork-based servers separate address spaces reliable: crash in one process won't affect others sharing needs more work: inter-process communication performance concerns # threads (lightweight) virtualization: processor share the same address space separate stack easy to communicate; need synchronization API: [pthreads](https://computing.llnl.gov/tutorials/pthreads/), [C11 threads](http://en.cppreference.com/w/c/thread), [C++11 threads](http://en.cppreference.com/w/cpp/thread) # pthreads ```c int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); int pthread_detach(pthread_t thread); int pthread_join(pthread_t thread, void **retval); ``` include ``; link with `-lpthread` call `pthread_detach` or `pthread_join` to release its resources pass `arg` to `start_routine`: lifetime & pack multiple values [echosrv-pthreads.c](l20/echosrv-pthreads.c.html) # C++11 threads [``](http://en.cppreference.com/w/cpp/thread): detach/join [``](http://en.cppreference.com/w/cpp/atomic): shared data [echosrv-c++thread.cc](l20/echosrv-c++thread.cc.html): w/ global counter high-level interface: [``](http://en.cppreference.com/w/cpp/header/future) # ex17 [ex17](http://courses.cs.washington.edu/courses/cse333/15wi/exercises/ex17.html): use 4 threads to compute the sum of array elements `pthread_create`: start threads pass multiple values to pthread_create: use a struct call `pthread_join` to wait for completion or use C++11 async ([example](http://en.cppreference.com/w/cpp/thread/async))