What will be the result when the following commandline is run

Дата канвертавання27.04.2016
Памер15.85 Kb.

With this question, I asked Linux Mandrake forum for help, and a forum moderator from Spain replied. Do not remember his name, but he was a life-saver...

What will be the result when the following commandline is run:

(who; date) & (sleep 5; uname -a & whoami)
(who; date) & ---> the "(something) &" creates a subshell "()" which is run in the background, that subshell executes first 'who' and once it finishes executes 'date'

(sleep 5; uname -a & whoami) ---> This time the subshell is launched on the foreground, and it runs in the following way: 'sleep 5' waits 5 seconds, that gives enough time to the first subshell to finish its execution, then the next command 'uname -a &' is launched (in the background) and finally 'whoami' is executed

------------------my comments:

There is no question that (who; date) and uname -a run on background.

What confused me is the unexpected order of execution (output) -

who, date, sleep 5, uname -a, and whoami.

I'd expect it to be

sleep 5, whoami, uname -a, who, and date.

Why? running in the background doesn't mean run slower or in a diferent order
I thought that jobs running on forground have priority in execution, and sleep 5 is the 1st forground job...

No, jobs in the background are just jobs which are executed in the background. Executing jobs in the background allows the shell to be ready (or) to run other programs without waiting for the last command launched to be ended.

The priority of a job can be set by the command nice(1) (or by the command renice(8 ) if you are changing the priority of an already running job)

Seems to me that there is no difference in sequence of execution

of the following two command lines (???):
$ (who; date) & (sleep 5; uname -a & whoami)


$ who; date; sleep 5; uname -a; whoami
Of course there is a difference! see my explanation above your second example is just a list of commands to be run one after another... I think that a better example would have been:

(sleep 5; who; date) & (uname -a & whoami)

The questions are:

(a) Which of these commands run sequentially and which run in parallel?

--------------------my answer:

run sequentially:

who and date

sleep 5 and whoami

run in parallel:

sleep 5 and uname -a

(who; date) and (sleep 5; uname -a & whoami)
both (1) AND (2) subshells run in parallel --> I mean that (who; date) subshell AND (sleep 5; uname -a & whoami) subshell run in parallel


uname -a AND whoami run in parallel
The other cases are run sequentially ";"
(b) How many of the processes run in parallel?

Hint: Use the ps f command to verify.

--------------------my answer:

Probably, 3 processes (out of total 5) run in parallel on background -

who, date, and uname -a
See my explanation about the subshells
Back note: a list of commands within parentheses '()' creates a subshell, and each command of the list is executed in that subshell, while a list of commands within curly braces '{}' causes the list to be executed in the current shell (no subshell)

How to use this instructor's "hint" ? Is there way to see how many processes run in parallel? I tried to type ps f WHILE waiting for sleep 5 (shell prompt was already available, somehow), then I tried to type ps f in the another window (again, while sleep 5 was running), and it didn't tell me anything!

see man ps(1); man top(1)
for example, before running your example you can open another terminal running 'top' to monitor what is happening in your system. Or, run 'ps aux | less' to take 'photos' of all the processes running at the same time in your system.
Also you can check the inheritance of the processes using pstree(1)

Also, I could see that process 1, what was (who; date), ran on background

[1]+ Done ( who; date )
But why didn't i see [2]+ done uname -a ??

It was a second background process...

You didn't see that "[2]+ done uname -a " because, as I told you, it was executed on a different subshell.

It was executed in the background of a SUBSHELL, not in the background of the current shell (command line). Indeed it was the first "[1]+" background job of that subshell, not the second "[2]+" of the parent shell. Anyhow, you wont see any "job control" output from a non interactive subshell.

In your example the parent shell doesn't know what is happening on its son shell, it is just waiting for the return status of the subshell.

База данных защищена авторским правом ©shkola.of.by 2016
звярнуцца да адміністрацыі

    Галоўная старонка