Sunday, December 11, 2011

Concepts related to threads & processes.



Multithreading as a widespread programming and execution model allows multiple threads to exist within the context of a single process. These threads share the process's resources but are able to execute independently. The threaded programming model provides developers with a useful abstraction of concurrent execution. However, perhaps the most interesting application of the technology is when it is applied to a single process to enable parallel execution on a multiprocessor system.

This advantage of a multithreaded program allows it to operate faster on computer systems that have multiple CPUs, CPUs with multiple cores, or across a cluster of machines — because the threads of the program naturally lend themselves to truly concurrent execution. In such a case, the programmer needs to be careful to avoid race conditions, and other non-intuitive behaviors. In order for data to be correctly manipulated, threads will often need to rendezvous in time in order to process the data in the correct order. Threads may also require mutually-exclusive operations (often implemented using semaphores) in order to prevent common data from being simultaneously modified, or read while in the process of being modified. Careless use of such primitives can lead to deadlocks.


Race conditions arise in software when separate processes or threads of execution depend on some shared state. Operations upon shared states are critical sections that must be mutually exclusive in order to avoid harmful collision between processes or threads that share those states.

Address space: Address space is seperate for process but threads of a process share the same addresss space.
Data sharing(bteween threads & processes): Self Read.
Communication: Communication between processes are through signals.

Memory protection is an issue when you are in the same address space. How do you encounter it? Through memory protection register.
Memory protection is a way to control memory access rights on a computer, and is a part of most modern operating systems. The main purpose of memory protection is to prevent a process from accessing memory that has not been allocated to it. This prevents a bug within a process from affecting other processes, or the operating system itself. Memory protection for computer security includes additional techniques such as address space layout randomization and executable space protection.

Thread Safe/Thread Safety:





Synchronization and thread safety in Java:





Thread or process synchronization:

Thread synchronization or serialization, strictly defined, is the application of particular mechanisms to ensure that two concurrently-executing threads or processes do not execute specific portions of a program at the same time. If one thread has begun to execute a serialized portion of the program, any other thread trying to execute this portion must wait until the first thread finishes. Synchronization is used to control access to state both in small-scale multiprocessing systems -- in multithreaded environments and multiprocessor computers -- and in distributed computers consisting of thousands of units -- in banking and database systems, in web servers, and so on.

InterThread Communication in Java

1. Reading 1
2. Reading 2
3. Reading 3

InterProcess Communication:

Information sharing
Privilege separation

Main IPC methods

Method: Provided by (operating systems or other environments)

File: Most operating systems
Signal: Most operating systems; some systems, such as Windows, implement signals in only the C run-time library and provide no support for their use as an IPC method.
Socket: Most operating systems
Message queue: Most operating systems
Pipe: All POSIX systems, Windows
Named pipe: All POSIX systems, Windows
Semaphore: All POSIX systems, Windows
Shared memory: All POSIX systems, Windows
Message passing:
(shared nothing) Used in MPI paradigm, Java RMI, CORBA, MSMQ, MailSlots, others
Memory-mapped file: All POSIX systems, Windows; this method may carry race condition risk if a temporary file is used.

No comments:

Post a Comment