Concurrency is the tendency for things to happen at the same time in a system. Process that coexist on the memory at a given time are called concurrent process. The concurrent process may either be independent or cooperating.

The independent process, as the name implies do not share any kind of information or data with each other. They just compete with each other for resources like CPU, I/O devices etc.

In single processor multiprogramming systems, processors are interleaved in the time to yield the appearances of simultaneous execution.

Even though actual parallelism is not achieved, and even though there is a certain amount of overhead in involved in switching back and forth between processes, interleaved execution provides major benefits in processing efficiency.

In multiprocessor system, it is possible not only to interleave the execution of multiple process but also to overlap them.

At first glance, it may seems that Interleaving and overlapping represent fundamentally different modes of execution and present different problems. But in fact both are examples of concurrent processing and both represent same problems.

In concurrent processing following problems arises:

Sharing of global resources

For example: if two process both make the use of same global variables and both perform reads and writes on that variables, then the order in which the various reads and writes are executed is critical.

Optimal resource allocation

For example, process A may request use of, and be granted control of, a particular I/O channel and then be suspended before using the channel. It may be undesirable for the OS simply to lock the channel and prevent its use by other process; indeed this may result in deadlock.

Locating programming errors

It becomes very difficult to locate a programming error because results are typically not deterministic and reproducible.

A Simple Example

void echo()

{

chin = getchar();

chout = chin;

putchar(chout);

}

In case of single processor multiprogramming system


The sharing of main memory among processes is useful to permit efficient and close interaction among processes. However, Consider the following sequence: Consider the following sequence:

  1. Process P1 invokes the echo procedure and is interrupted immediately after getchar() returns its value and stores it in chin. At this point, the most recently entered character, x, is stored in variable chin.
  2. Process P2 is activated and invokes the echo procedure, which runs to conclusion, inputting and then displaying a single character, y, on the screen.
  3. Process P1 is resumed. By this time, the value x has been overwritten in chin and therefore Instead, chin contains y, which is transferred to chout and displayed.

Solution

Permit only one process at a time to be in that procedure. Then the foregoing sequence would result in the following:

  1. Process P1 invokes the echo procedure and is interrupted immediately after the conclusion of the input At this point, the most recently entered character, x, is stored in variable chin.
  2. Process P2 is activated and invokes the echo procedure. However, because P1 is still inside the echo procedure, although currently suspended, P2 is blocked from entering the Therefore, P2 is suspended awaiting the availability of the echo procedure.
  3. At some later time, process P1 is resumed and completes execution of echo. The proper character, x, is displayed.
  4. When P1 exits echo, this removes the block on P2. When P2 is later resumed, the echo procedure

In case of single processor multiprogramming system

Processes P1 and P2 are both executing, each on a separate processor. Both processes invoke the echo procedure.

The following events occur; events on the same line take place in parallel:

Process P1 Process P2
. .
chin = getchar(); .
. chin = getchar();
chout = chin; chout = chin;
putchar(chout);  
. putchar(chout);
   
  1. Processes P1 and P2 are both executing, each on a separate processor. P1 invokes the echo procedure.
  2. While P1 is inside the echo procedure, P2 invokes echo. Because P1 is still inside the echo procedure (whether P1 is suspended or executing), P2 is blocked from entering the procedure. Therefore, P2 is suspended awaiting the availability of the echo procedure.
  3. At a later time, process P1 completes execution of echo, exits that procedure, and continues Immediately upon the exit of P1 from echo, P2 is resumed and begins executing echo.

In the case of a uniprocessor system, the reason we have a problem is that an interrupt can stop instruction execution anywhere in a process.

In the case of a multiprocessor system, we have that same condition and, in addition, a problem can be caused because two processes may be executing simultaneously and both trying to access the same global variable.

However, the solution to both types of problem is the same: control access to the shared resource.