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] ) ]
   frame $parent.pad -width $pad -height $pad
   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
   global radio
# 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 }
   }
   FillList $radio
}

proc FillList { radio } {
   global usrname
   # Loesche Inhalt der Listbox
   .lb.f.list delete 0 end
   switch -exact -- $radio {
      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 radio a
set action r
set usrname [exec whoami]
frame .lb
frame .radio
frame .action
frame .buttons
ScrolledListbox .lb.f -width 20 -height 5 -setgrid true
label .radio.label -text "Display following processes:"
radiobutton .radio.all -text All -variable radio -value a -anchor w
radiobutton .radio.user -text "With Terminal" -variable radio -value u -anchor w
radiobutton .radio.own -text Own -variable radio -value o -anchor w
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
pack .radio.all .radio.user .radio.own \
   -side right -fill x -expand true -anchor n
pack .action.label -side top -anchor n
pack .action.renice .action.kill -side right -fill x -expand true -anchor n
pack .buttons.exit -side right -fill x -expand true
pack .buttons.refresh -side right -fill x -expand true
pack .lb -side top -fill both -expand true
pack .radio .action .buttons -side top -fill x 
FillList $radio

Universität Fakultät SAI

Ingo Melzer, 27. June 2000