Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inter-Process Communication  most OSs provide several abstractions for inter- process communication: message passing, shared memory, etc.  communication.

Similar presentations


Presentation on theme: "Inter-Process Communication  most OSs provide several abstractions for inter- process communication: message passing, shared memory, etc.  communication."— Presentation transcript:

1 Inter-Process Communication  most OSs provide several abstractions for inter- process communication: message passing, shared memory, etc.  communication requires synchronization between processes (i.e. data must be produced before it is consumed)  synchronization can be implicit (message passing) or must be explicit (shared memory)  explicit synchronization can be provided by the OS (semaphores, monitors, etc..) or can be achieved exclusively in user-mode (later in the course)

2 Message Passing Implementation  two copy operations in a conventional implementation x=1 send(process2, &X) receive(process1,&Y) print Y process 1 process 2 X Y kernel buffers 1st copy 2nd copy kernel

3 Shared Memory Implementation  no copying but synchronization is necessary X=1 print Y process 1 process 2 X Y kernel physical memory shared region

4 Lecture 5: Threads

5 Threads  the process concept incorporates two abstractions:  a virtual processor (an execution context)  a resource ownership (an address space, open files etc..)  within an address space, we can have more units of execution: threads  all the threads of a process share the same address space and the same resources

6 Multithreaded Environment  Process  all resources allocated: IPC channels, files etc..  a virtual address space that holds the process image  Threads  an execution state: running, ready, etc..  an execution context: PC, SP, other registers  a per-thread stack

7

8 Threads vs. Processes  operations on threads (creation, termination, scheduling, etc..) are cheaper than the corresponding operations on processes  inter-thread communication is supported through shared memory without kernel intervention  Advantages  Disadvantages  easy to introduce race conditions  synchronization is necessary

9 Posix Thread (Pthread) API _ thread creation and termination pthread_create(&tid,NULL,start_fn,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);

10 Condition Variables (example) _ thread 1 pthread_mutex_lock(&lock); while (!my-condition); pthread_cond_wait(&c,&lock); do_critical_section(); pthread_mutex_unlock(&lock); _ thread 2 pthread_mutex_lock(&lock); my-condition = true; pthread_mutex_unlock(&lock); pthread_cond_signal(&c);

11 Process  Have a virtual address space, which holds the process image  Protected access to processors, other processes, files, and I/O resources

12 Thread  An execution state (running, ready, etc.)  Saved thread context (when not running)  Has an execution stack  Per-thread static storage for local variables  Access to the memory and resources of the process it belongs to  all threads of the same process share this

13

14 User-Level Threads  All thread management is done by the application  The kernel is not aware of the existence of threads

15 Kernel-Level Threads  Kernel maintains context information for the process and the threads  Scheduling is done on a thread basis

16 Thread Implementation  user-level threads  implemented as a thread library, which contains the code for thread creation, termination, scheduling and switching  kernel sees one process and it is unaware of its thread activity  can be preemptive or not (co-routines)  kernel-level threads (lightweight processes)  thread management done by the kernel

17 User-Level vs. Kernel-Level Threads  Advantages of the user-level threads  performance: low-cost thread operations (do not require crossing protection domains)  flexibility: scheduling can be application specific  portability: user-level thread library easy to port  Disadvantages of the user-level threads  if a user-level thread is blocked in the kernel, the entire process (all threads of that process) are blocked  cannot take advantage of multiprocessing (the kernel assigns one process to only one processor)

18 User-Level vs. Kernel-Level Threads (cont’d) process processor user-level threads thread scheduling process scheduling processor kernel-level threads thread scheduling kernel user

19 Thread/Process Operation Latencies Operation user-level threadskernel-level threads processes null fork 34 usec 948 11,300 usec usec signal-wait 37 usec441 usec1,840 usec

20 User-Level Thread Implementation code process kernel thread 1 pc sp pc sp thread 2 thread stacks data


Download ppt "Inter-Process Communication  most OSs provide several abstractions for inter- process communication: message passing, shared memory, etc.  communication."

Similar presentations


Ads by Google