Universität Ulm, Fakultät für Mathematik und Wirtschaftswissenschaften, SAI

# Lösung zu Blatt 6 --- Implementierung kleiner Datenbanken unter UNIX (SS 2001)

## 6 Ein kleiner Prozessmanager

```#! /bin/sh
# the next line restarts using wish \
exec wish "\$0" "\$@"

proc ScrolledListbox { parent args } {
# args: Definition fuer scrollbars
frame \$parent
eval { listbox \$parent.list \
-yscrollcommand [list \$parent.scry set] \
-xscrollcommand [list \$parent.scrx set] } \$args
frame \$parent.bottom
scrollbar \$parent.scrx -orient horizontal \
-command [list \$parent.list xview]
scrollbar \$parent.scry -orient vertical \
-command [list \$parent.list yview]
#Dummy Ecke
set pad [expr [\$parent.scry cget -width] + 2* \
([\$parent.scry cget -bd] + [\$parent.scry cget -highlightthickness] ) ]
pack \$parent.bottom -side bottom -fill x
pack \$parent.pad -in \$parent.bottom -side right
pack \$parent.scrx -in \$parent.bottom -side bottom -fill x
pack \$parent.scry -side right -fill y
pack \$parent.list -side left -fill both -expand true
bind \$parent.list <ButtonRelease-1> [list ListSelect  %W %y]
return \$parent.list
}

proc ListSelect {src y } {
global action
# Liefert den gewaehlten Datensatz also Prozess
set out [\$src get [ \$src nearest \$y ] ]
regexp {.. ([0-9]+)} \$out all pid rest
switch -exact -- \$action {
k {exec kill \$pid }
r {exec renice -n 2 \$pid}
default { puts stderr "Fehler"; exit 0 }
}
}

proc FillList { radio } {
global usrname
# Loesche Inhalt der Listbox
.lb.f.list delete 0 end
a { set opt -elf }
u { set opt "-alf"}
o { set opt "-flu \$usrname" }
default { puts stderr "Fehler"; exit 0 }
}
# Lese Ausgabe von ps
set p [open "|ps \$opt" r]
while { [gets \$p line ] >= 0 } {
.lb.f.list insert end \$line
}
}

set action r
set usrname [exec whoami]
frame .lb
frame .action
frame .buttons
ScrolledListbox .lb.f -width 20 -height 5 -setgrid true
label .radio.label -text "Display following processes:"
label .action.label -text "Execute on selected process:"
radiobutton .action.renice -text "renice 2" -variable action -value r -anchor w
radiobutton .action.kill -text "kill" -variable action -value k -anchor w
button .buttons.refresh -text Refresh -command { FillList \$radio }
button .buttons.exit -text Exit -command exit
pack .lb.f -fill both -expand true
pack .radio.label -side top -anchor n