TRHEADS

-In computer science, a thread of execution results from a fork of a computer program into two or more concurrently running tasks. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process. Multiple threads can exist within the same process and share resources such as memory, while different processes do not share these resources.
On a single processor, multithreading generally occurs by time-division multiplexing (as in multitasking): the processor switches between different threads. This context switching generally happens frequently enough that the user perceives the threads or tasks as running at the same time. On a multiprocessor or multi-core system, the threads or tasks will generally run at the same time, with each processor or core running a particular thread or task. Support for threads in programming languages varies: a number of languages simply do not support having more than one execution context inside the same program executing at the same time. Examples of such languages include Python, and OCaml, because the parallel support of their runtime support is limited by the use of a central lock, called "Global Interpreter Lock" in Python, "master lock" in Ocaml. Other languages may be limited because they use threads that are user threads, which are not visible to the kernel, and thus cannot be scheduled to run concurrently. On the other hand, kernel threads, which are visible to the kernel, can run concurrently.
Many modern operating systems directly support both time-sliced and multiprocessor threading with a process scheduler. The kernel of an operating system allows programmers to manipulate threads via the system call interface. Some implementations are called a kernel thread, whereas a lightweight process (LWP) is a specific type of kernel thread that shares the same state and information.
Programs can have user-space threads when threading with timers, signals, or other methods to interrupt their own execution, performing a sort of ad-hoc time-slicing.


-single threaded process

-multi threaded process


-Benefits of Multi-threaded Programming


-Responsiveness

-Resource Sharing

-Economy

-Utilization of MP Architechtures


  • User Thread

-Thread management done by user-level threads library


(e.g.):


1. POSIX Pthreads


2. Mach C-threads


3.Solaris threads



  • Kernel Thread

- Supported by Kernel


(e.g.):


1.Windows 95/98/NT/2000


2.Solaris


3.Tru64 UNIX


4.BeOS


5.Linux



  • Thread Library




  • Multithreading models

1)Many-to-one Model


-Many user-level threads mapped to single kernel thread.


-Used on systems that do not supported kernel threads.



2)One-to-One Model


-Each user-level thread maps to kernel thread.


(e.g.):


-Windows 95/98/NT/2000


-OS/2


3)Many-to-Many Model

-Allows many user level threads to be mapped to many kernel threads.

-Allows the operating system to create a sufficient number of kernel threads.

-Solaris 2

-Windows NT/2000 with the ThreadFiber package.

Producer-Consumer Example

One process generates data – the producer

• The other process uses it – the consumer

• If directly connected – time coordination



How would they coordinate the time ?

BUFFERING

-The mechanism that buffers messages (a.k.a. queue)

may have the following properties






  • zero capacity-queue has lenght 0, no messages can be out standing on link, sender blocks for message exchange.



  • bounded capacity-queue has length N, N message can be in queue at any point in time, sender blocks if queue at any point in tme, sender blocks if queue is full, otherwise it may continue to execute.



  • unbounded capacity- queue has infinite length, sender never blocks

INTERPROCESS COMMUNICATION

*-*Inter-process communication (IPC) is a set of techniques for the exchange of data among multiple threads in one or more processes. Processes may be running on one or more computers connected by a network. IPC techniques are divided into methods for message passing, synchronization, shared memory, and remote procedure calls (RPC). The method of IPC used may vary based on the bandwidth and latency of communication between the threads, and the type of data being communicated.


There are several reasons for providing an environment that allows process cooperation:
-Information sharing


-Computation speedup


-Modularity


- Convenience






1)Direct Communication


  • sender/reciever refer to each other, as seen before

  • properties of communication link

-link is associated with exactly two processes


-exactly one link for every pair of processes



  • communication is symmetric (above) or asymmetric




2)Indirect Communication



3)Synchronization-message passing maybe blocking or non-blocking (synchronous or asynchronous)




  • blocking send-sender bloked until message is recieve by receiver (orby mailbox)


  • Non-blocking send-sending process resumes operation right after sending.


  • blocking receive-reiever blocks until message is available.

  • nonblocking receive-receiver retrieves a vald message or returns an error code.

5)INTERPROCESS COMMUNICATION


  • For Communication and Synchronization

-Share memory


-OS provide IPC



  • Message system

-No need for shared available


-Two operations:


1.send (message)-message fixed or variable


2.recieve message



  • if P and Q wish to communicate, they need to:

-establish a communication link betweeen them


-exchange message via send/ recieve



  • Implemention of communication Link

-physical(e.g. , shared memory, hardware bus)


-logical(e.g. , logical properties)

4)COOPERATING PROCESSES


  • Advantages of process cooperation

-Information sharing

-Comunication speed-up

-Modularity

-Convinience


  • Independent process connot affect/be affected by the execution of another process, cooperating once can.

  • Issue

-Communication


-avoid processes getting into each other's ways


-Ensure proper sequencing when there are dependencies



  • Common Paradigm:producer costumer

-unbounded-buffer - no practical consumer


-bounded buffer-assumes fixed buffer size

3)OPERATION PROCESS

a)Process Creation


-Parent process create children processes, which, in turn create other processes, forming a tree of processes.




  • Resource sharing


  1. Parent and children share all resources.


  2. Children share subset of parent’s resources.


  3. Parent and child share no resources.




  • Execution


  1. Parent and children execute concurrently.


  2. Parent waits until children terminate.




  • Address space


  1. Child duplicate of parent.


  2. Child has a program loaded into it.



  • UNIX examples


  1. fork system call creates new process


  2. exec system call used after a fork to replace the process’ memory space with a new program.


b)Process Termination





  • Process executes last statement and asks the operating system to decide it (exit).




  1. Output data from child to parent (via wait).


  2. Process resources are deallocated by operating system.




  • Parent may terminate execution of children processes (abort).


  1. Child has exceeded allocated resources.


  2. Task assigned to child is no longer required.


-Parent is exiting.



  1. Operating system does not allow child to continue if its parent terminates.


  2. Cascading termination.