Print the N first processes, sorted in descending order of cumulative system time. The parameter N is passed as a command line argument.

Closed due to the fact that off-topic participants are Vladimir Martyanov , aleksandr barakin , Viktor Tomilov , AK , 0xdb 21 Feb '18 at 23:55 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Learning tasks are allowed as questions only on the condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve the problem "- Vladimir Martyanov, aleksandr barakin, Viktor Tomilov, AK, 0xdb
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    What did you try on your own? - Mr. Brightside
  • I do not know where to start even ... - Kirill Gorbunov
  • one
    KILL in a subject has some meaning? - de_frag

1 answer 1

As far as I know, the statistics on the kernel and the user, the ps command does not produce separately. But, if you look at the total time, then something like:

 ps -el | grep -v -e'00:00:00' | awk '{print $13, $14}' | tail -n+2 | sort -r | head -n3 

I explain on separate commands of the pipeline:

  • ps -el Provides extended information on all processes.
  • grep -v -e'00: 00: 00 ' Excludes from the list those processes whose time is zero
  • awk '{print $ 13, $ 14}' Displays only the time and name of the program
  • tail -n + 2 Clears the table header.
  • sort -r Sorts descending
  • head -n3 Prints the participants who took the first three places.

Well, you just need to arrange it in the form of a shell script in the last command -n3 (fixed value == 3) with $ 1 - the first parameter in the command line of this script.

  • Thank you very much - Kirill Gorbunov
  • awk is not a shell . - 0andriy
  • awk is not a shell - I did not understand ... But then ps is also NOT a shell .. And even ls is also NOT a shell. - Sergey