Schrift
[thread]838[/thread]

Sorting processes



<< >> 7 Einträge, 1 Seite
Gast Gast
 2006-11-15 19:06
#8961 #8961
Hello Guys,

i see a lot of experts here, so i hope, you will help me to lose my homework:

I have to print in linux terminal 10 processes which are most wasteful for CPU at the time. The print should be refreshed every second. I found a command: ps -eo pid,comm,%cpu which gives me a list: ID TASK %CPU usage

I would really please you to help me sort this list...

Code: (dl )
1
2
3
4
5
6
7
8
#!/usr/bin/perl

@table=`ps -eo pid,comm,%cpu`;
# print "@tabela";

@tableedited=split(/ +/,"@table");

#print "@tableedited";


How should i go on?

Thanks in advance boys!
renee
 2006-11-15 20:18
#8962 #8962
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Hi,

you should use the sort-function of Perl (see perldoc -f sort) to sort your data. To reach your goal you should read something about the "Schwartzian Transformation".

To refresh the information, you have to use an infinite loop (while(1){...}) and sleep within this loop (see perldoc -f sleep).

Try to solve your problem with these information and come back if you have any problems...
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
bloonix
 2006-11-15 21:28
#8963 #8963
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
Hello MatoX23,

you could take a look on

CPAN:Proc::ProcessTable::Process
CPAN:Linux::Statistics

With both modules you can collect different informations about the process
table. Maybe it's your thing.

Regards,
opi
What is a good module? That's hard to say.
What is good code? That's also hard to say.
One man's Thing of Beauty is another's man's Evil Hack.
pq
 2006-11-16 12:52
#8964 #8964
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
[quote=MatoX23,15.11.2006, 18:06]Hello Guys,
...
Thanks in advance boys![/quote]
well, you clearly don't want help from girls. too bad for you, maybe
i would have helped...

[quote=renee,15.11.2006, 19:18]To reach your goal you should read something about the "Schwartzian Transformation".[/quote]
it's called "transform", not "transformation".
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem
docsnyder
 2006-11-23 17:20
#8965 #8965
User since
2005-09-08
300 Artikel
BenutzerIn
[Homepage] [default_avatar]
@pq

MatoX23 did not state that he wouldn't like to get help from girls.

He just stated, instead, that he would not thank for that.

LOL

Greetz, Doc
sde42dn
 2006-11-27 16:00
#8966 #8966
User since
2006-10-26
11 Artikel
BenutzerIn
[default_avatar]
hello,

here a simple sort-funktion:

my @table_sort=sort { $a <=> $b }@table ;
foreach (@table_sort) {
print "$_\n";
}
docsnyder
 2006-11-27 17:36
#8967 #8967
User since
2005-09-08
300 Artikel
BenutzerIn
[Homepage] [default_avatar]
@sde42dn

Quote
my @table_sort=sort { $a <=> $b }@table ;
foreach (@table_sort) {
print "$_\n";
}


Unfortunalety, this is not what is needed by MatoX23. He wants process ID's to be sorted by means of CPU time. Please consider, that, in your code, "1000" is lexicographically smaller than "5". Have a look at renee's posting, were he pointed out, that the "Schwartzian Transformation" will do the trick.

Greetz, Doc
<< >> 7 Einträge, 1 Seite



View all threads created 2006-11-15 19:06.