Concurrent and Parallel Processing in Shell

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
Bad question set.
Refer to the question below in the next post to understand why the commands below are both concurrent and also parallel.
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
Question to ponder
cat | cat | cat | cat
Are the 4 unix cat(concatenate) utilities running concurrently ?
Are they also running in parallel ? If they are why ? If they are not, why ?
:)
 
Last edited:

peterchan75

Supremacy Member
Joined
Apr 26, 2003
Messages
6,718
Reaction score
529
If I may say, this is an IT mental gymnastic.:(
Concurrent is more of a software implementation i.e. scheduling tasks for a fast cpu and giving an impression of multiple events happening at the same time.
Whereas parallel is of hardware i.e. multicore cpu or gpu processor executing multiple tasks at the same time.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
If I may say, this is an IT mental gymnastic.:(
Concurrent is more of a software implementation i.e. scheduling tasks for a fast cpu and giving an impression of multiple events happening at the same time.
Whereas parallel is of hardware i.e. multicore cpu or gpu processor executing multiple tasks at the same time.
Who says concurrent must be software implementation ?

If I have 10 cores all working together, are these cores working concurrently and independently ?
Concurrent doesn't has to be parallel, it only means they are active at the same time, but they could be blocking from one process to another which makes them _NOT_ parallel.
There is absolutely no concept that bind concurrency and parallelism to hardware or software. You could have a set of hardware that is blocking each other and still there is no parallelism.

Your mention of scheduling and context switching in a one core/processor system is a concept on how early days of hardware achieve multitasking, but still very much in use today. This is yet another terminology to describe this sort of phenomenon. In Parallel and Concurrent Programming courses, these sort of concepts are explicitly discussed.

In fact, if you go into hardware, you could also introduce hardware and software interrupts that provide some sort of dedicated multitasking and also event driven which allow the process context switch from its main processing queue. Context switching can be either preemptive or non-preemptive. It may not be as general as multiprocessors or multicores, but it is also a form of context switching.

Here the discussion scope is more narrow, it's about processes in the OS.
If I simply have
CMD1 | CMD2 | CMD3 | ... | CMD10
Are all these CMD?(commands) running concurrently ? How would you know if it is or not ?

:)
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
Question to ponder
cat | cat | cat | cat
Are the 4 unix cat(concatenate) utilities running concurrently ?
Are they also running in parallel ? If they are why ? If they are not, why ?
:)
So are the 4 cat commands. Yes the commands are concurrently started and active in the process list as shown in the example below
Code:
$ echo $$
630818
$ cat | cat | cat | cat

 630818 pts/0    Ss     0:00  \_ -bash
 630844 pts/0    S+     0:00  |   \_ cat
 630845 pts/0    S+     0:00  |   \_ cat
 630846 pts/0    S+     0:00  |   \_ cat
 630847 pts/0    S+     0:00  |   \_ cat

But are they all running parallel now ? Nope, they are all blocking on the STDIN.

However if I do this, are they all running in parallel ?
Code:
$ </dev/zero cat | cat | cat | cat >/dev/null

Yes. Like an assembly factory where all the workers lining up in a pipeline are working concurrently and in parallel.
Below in a snapshot of the top utility
Code:
    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 630818 ubuntu    20   0    8740   4736   3072 S   0.0   0.3   0:00.03              `- bash
 630902 ubuntu    20   0    5740   1408   1280 S  36.0   0.1   1:19.44                  `- cat
 630903 ubuntu    20   0    5740   1408   1408 S  64.0   0.1   1:56.95                  `- cat
 630904 ubuntu    20   0    5740   1536   1408 R  70.0   0.1   1:55.09                  `- cat
 630905 ubuntu    20   0    5740   1408   1408 S  30.0   0.1   1:07.50                  `- cat

Working in parallel doesn't mean doing the same things exactly at the same time.
The leftmost cat may be working on item X, while the rightmost cat will be working on item X-4, depending on how fast the data are pass across the pipeline.
However one could also have the cat commands replaced as subprocess which depends on nothing on the stdin and stdout streams, hence
Code:
$ (while true; do echo 1 >/dev/null; done) | (while true; do echo 1 >/dev/null; done) | (while true; do echo 1 >/dev/null; done) | (while true; do echo 1 >/dev/null; done)
Each subprocess could be rewritten as a shell script and invoke like this
Code:
$ script.sh | script.sh | script.sh | script.sh

Hence you wouldn't know what is going on in the script.sh until you read the codes.
Here you will find the subprocesses are running concurrently and in parallel with nothing worthy being done and yet occupying very significant cpu load
Code:
 630818 ubuntu    20   0    8740   4736   3072 S   0.0   0.3   0:00.04              `- bash
 630957 ubuntu    20   0    8740   3168   1408 R  49.5   0.2   0:57.85                  `- bash
 630958 ubuntu    20   0    8740   3168   1408 R  48.5   0.2   0:57.85                  `- bash
 630959 ubuntu    20   0    8740   3168   1408 R  48.5   0.2   0:57.84                  `- bash
 630960 ubuntu    20   0    8740   3168   1408 R  47.5   0.2   0:57.84                  `- bash

If you understand how unix processes works, you can often take advantage of certain constructs that has unorthodox way of using them.
Simply Interprocess Communication (IPC) in Unix is an extremely powerful pillar construct which grows into other like named pipes, fifo, file streams and descriptors, semaphores, shared memory, message queues and more which is offered in Unix kernels all at your disposal.

I originally try to set a question to explain on these, but I realise that question is too narrow in scope and doesn't explain correctly.
Hence I removed it and put out this explanation for interested parties to discuss.
:)
 
Last edited:

MoxLotus

Master Member
Joined
Aug 21, 2007
Messages
2,982
Reaction score
4
I like the explanation from one of the stanford professor on this. he used his hands to demonstrate the difference between concurrent and parallel.
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top