1 / 94

TDC561 Network Programming

TDC561 Network Programming. Week 6-7: Concurrency Aspects : POSIX Threads; Multithreading. Camelia Zlatea, PhD Email: czlatea@cs.depaul.edu. W. Richard Stevens, Network Programming : Networking API: Sockets and XTI, Volume 1, 2nd edition, 1998 (ISBN 0-13-490012-X) Chap. 23

ulla-sears
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 Week 6-7: Concurrency Aspects : POSIX Threads; Multithreading Camelia Zlatea, PhD Email: czlatea@cs.depaul.edu

  2. W. Richard Stevens, Network Programming : Networking API: Sockets and XTI, Volume 1, 2nd edition, 1998 (ISBN 0-13-490012-X) Chap. 23 John Shapley Gray, Interprocess Communications in UNIX -- The Nooks and Crannies Prentice Hall PTR, NJ, 1998 Chap. 11 References

  3. Unit of resource ownership: process, task virtual address space is allocated to it (process image map) resources assigned to it (main memory, files, I/O devices) Unit of Dispatching: process, thread (lightweight process) the entity scheduled and dispatched by the OS Multithreading - multiple threads of execution within a single process concurrency at process level Terms

  4. Threads vs. Processes • Creation of a new process using fork is expensive (time & memory). • A thread (sometimes called a lightweight process) does not require lots of memory or startup time.

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

  6. 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 A thread (sometimes called a lightweight process) does not require lots of memory or startup time. UNIX Threads

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

  8. 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)

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

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

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

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

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

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

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

  16. Multitasking Sequential Execution Context Switching

  17. Multithreaded environment • PROCESS • a virtual address space that holds the process image • all resources allocated: IPC channels, files etc. • THREADS • an execution state: running, ready,.. • an execution context: PC, SP, other registers • a per thread stack

  18. A thread (lightweight process LPW)is a basic unit of CPU utilization; it consists of: program counter register set stack space thread control block A thread shares with its peer threads its: code segment data segment operating system resources A traditional or heavyweight process is a task with one thread Threads

  19. In a multiple treaded task, while one server thread is blocked or waiting, a second thread in the same task can run. Cooperation of multiple threads in the same job confer higher throughput and improved performance. Kernel is not involved in this type of intra-task communication Applications that requires sharing a common buffer benefit from threads utilization. Threads (Cont.)

  20. Threads (Cont.) process threads program counter text segment data segment

  21. All threads of a process share the state and resources of that process Ready Running Blocked Terminated Threads (Cont.)

  22. Spawn: create a new thread within a process and place it in ready list program counter register set stack space Block: a thread waits for an event to occur PC, register set and SP are saved the next ready thread is dispatched Unblock: the tread is moved to ready when event is delivered Finish: Threads completes and its register context and stacks are de-allocated Thread Operations

  23. fork() Data Segment ( Global Variables ) Data Segment ( Global Variables ) Text/Code Text/Code Stack Stack Process Parent fork() Process Child

  24. pthread_create() Data Segment ( Global Variables ) Text/Code Stack Thread Parent Process A address space pthread_create Thread Child Stack

  25. Multiple Threads • Each process can include many threads. • All threads of a process share: • memory (program code and global data) • open file/socket descriptors • signal handlers and signal dispositions • working environment (current directory, user ID, etc.)

  26. Thread-Specific Resources • Each thread has it’s own: • Thread ID (integer) • Stack, Registers, Program Counter • errno • Threads within the same process can communicate using shared memory. • Issues • Mutual Exclusion • Synchronization

  27. Solaris 2 is a version of UNIX support for threads at kernel and user levels symmetric multiprocessing real-time scheduling LWP - lightweight processes - intermediate level between user-level threads and kernel threads.. Solaris Threads

  28. Resource needs of thread types: Kernel Thread: small data structure and a stack; thread switching does not require changing memory access information - relatively fast LWP: PCB with register data, accounting and memory information; switching between LPWs is relatively slow. User-level thread: only needs stack and program counter; no kernel involving means fast switching; Kernel only sees the LWPs that support user-level threads. Solaris Threads (Cont.)

  29. User-Level Thread Active Runnable Stopped Sleeping Lightweight Process (map user threads onto kernel threads) Running Blocked Runnable Stopped Thread States

  30. Ready - maybe scheduled for execution priority scheduling Standby - selected to run waits for processor availability Running - executed by CPU until is preempted, quantum expired, or is blocked or is terminated Waiting - blocked for event delivery, to synchronize with other threads Transition - threads is “Ready” but some resources are not available Terminated Thread States

  31. User-Level vs. Kernel Level Threads • Advantages of ULT • performance: low cost thread operations ( do not req. crossing protection domains) • flexibility: scheduling can be app. Specific • portability: ULT thread library easy to port • Disadvantages of ULT • if a ULT is blocked, the entire process (all threads of that process are blocked) • cannot take advantage of mutiprocessing

  32. User-Level vs. Kernel Level Threads ULTs KLTs Thread Scheduling user kernel Thread Scheduling Process Scheduling

  33. Threads in Solaris 2 task 1 task 2 task 3 user-level thread LWPs kernel kernel thread CPU

  34. Windows NT Architecture Win16 Posix App. Win32 App. DOS App. WOW OS/2 App. Posix Subsyst VDM VDM OS/2 Subsyst Win32 Subsyst Executive service (Kernel Mode) Hardware

  35. Win32 Subsystem multiple threads of execution Ex.: A word processor that spell-checks your document as you type threads are scheduled preemptively A thread receives a specific amount of time for execution, and when that time is up, Windows NT stops executing that thread and begins executing another thread preemptive multitasking with other applications and subsystems VDN (Virtual DOS Machine) a DOS program executes as a single thread in a separate VDM WOW (Windows on Windows) cooperative multitasking each application performs seq. of operations and then return control to OS non-preemptive scheduling Windows NT Threads

  36. POSIX (Portable Operating System Interface) POSIX threads are similar with Solaris 2 threads Difference: POSIX threads use attributes objects to properties of the threads Stack size, Stack Address Schedule Policy and Parameters FCFS, RR, Preemptive Priority Policy Thread package (pthread.h) thread creation, destruction thread mutual exclusion thread synchronization (condition variables) POSIX Threads

  37. Posix Threads • We will focus on Posix Threads - most widely supported threads programming API. • You need to link with “-lpthread”

  38. POSIX Threads API • thread creation and termination pthread_create(&tid, NULL, func, arg) pthread_exit(status) • thread join pthread_join(tid, &status) • mutual exclusion pthread_mutex_lock(&lock) pthread_mutex_unlock(&lock) • condition variable pthread_cond_wait(&c,&lock) pthread_cond_signal(&c)

  39. Thread Creation pthread_create( pthread_t *tid, const pthread_attr_t *attr, void *(*func)(void *), void *arg); • func is the function to be called; when func() returns the thread is terminated. • The return value is 0 for OK and >0 (an error number ) on error. • Does not set errno • Thread ID is returned in tid • Thread attributes can be set using attr, including detached state and scheduling policy. You can specify NULL and get the system defaults.

  40. Thread IDs • Each thread has a unique ID, a thread can find out it's ID by calling pthread_self(). • Thread IDs are of type pthread_t which is usually an unsigned int. When debugging it's often useful to do something like this: printf("Thread %u:\n",pthread_self());

  41. Thread Arguments • When func() is called the value arg specified in the call to pthread_create() is passed as a parameter. • funccan have only 1 parameter, and it can't be larger than the size of a void *. • Complex parameters can be passed by creating a structure and passing the address of the structure. • The structure can't be a local variable (of the function calling pthread_create), since threads have different stacks

  42. Thread args example struct { int x,y } pair; void *myfunc( void *arg) { struct pair *foo = (struct pair *) arg; printf("%u sum of %d and %d is %d\n", pthread_self(), foo->x, foo->y, foo->x+foo->y); return(NULL); }

  43. Thread Lifespan • Once a thread is created, it starts executing the function func() specified in the call to pthread_create(). • If func() returns, the thread is terminated. • A thread can also be terminated by calling pthread_exit(). • If main() returns or any thread calls exit()all threads are terminated.

  44. Detached State • Each thread can be either joinable or detached. • Detached: on termination all thread resources are released by the OS. A detached thread cannot be joined. • No way to get at the return value of the thread. ( a pointer to something: void * ).

  45. Joinable Thread • Joinable: on thread termination the thread ID and exit status are saved by the OS. • One thread can "join" another by calling pthread_join - which waits (blocks) until a specified thread exits. int pthread_join( pthread_t tid, void **status);

  46. Spawn: create a new thread within a process and place it in ready list PC, register set, stack space Block: a thread waits for an event to occur PC, register set and SP are saved the next ready thread is dispatched Unblock: the thread is moved to ready when event is delivered Finish: threads completes and its register context and stacks are de-allocated Thread Operations

  47. Advantages Operations on threads are cheaper than the corresponding operations on processes inter-thread communication is achieved without kernel intervention, through shared memory Disadvantages synchronization is critical easy to induce race conditions Threads vs. Processes

  48. Example threads void *work(void *arg) { int i = pthread_self(); fprintf(stderr,"Thread ID:%d\n”,i); } void monitor_ths(int num_ths){ pthread_t *tid; int i; if ((tid = (pthread_t *)calloc(num_ths, \ sizeof(pthread_t))) == NULL) return;

  49. Example threads /* create a thread for each separate activity */ for (i = 0; i < num_ths; i++) if(error=pthread_create((tid + i), \ NULL, work, (void *)&num_ths)); fprintf(stderr, "Could not create \ thread %d:%s\n", i, strerror(error)); for (i = 0; i < num_ths; i++) pthread_join(*(tid + i), NULL); }

  50. #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> void work(void) { fprintf(stderr,“Process ID: \ %d\n”,getpid()); } void main(void) { pid_t pid; int i, status; fprintf(stderr, “Process ID: \ %d\n”,getpid()); for (i=1; i<=3;i++) if (fork()==0) work(); while (pid=wait(&status)&& pid!=-1) printf(“Child %d is done\n”,getpid()); } Example Concurrent Processes

More Related