| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
from fltk import * |
| 28 |
import sys, os, platform |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
G_disp = [0,0,0]; |
| 37 |
G_buff = [0,0,0]; |
| 38 |
G_outfd = [0,0,0]; |
| 39 |
G_pids = [0,0,0] |
| 40 |
|
| 41 |
|
| 42 |
def start_child(t): |
| 43 |
global G_pids |
| 44 |
|
| 45 |
out = os.pipe() |
| 46 |
G_pids[t] = os.fork() |
| 47 |
print("pid = ", G_pids[t]) |
| 48 |
|
| 49 |
if G_pids[t] == -1: |
| 50 |
|
| 51 |
os.close(out[0]) |
| 52 |
os.close(out[1]) |
| 53 |
elif G_pids[t] == 0: |
| 54 |
|
| 55 |
os.close(out[0]) |
| 56 |
os.dup2(out[1],2) |
| 57 |
os.close(out[1]) |
| 58 |
if t == 0: |
| 59 |
os.execlp("/bin/sh", "sh", "-c", "ps auxww 1>&2") |
| 60 |
print("execlp(ps)") |
| 61 |
|
| 62 |
sys.exit(1) |
| 63 |
elif t == 1: |
| 64 |
os.execlp("/bin/sh", "sh", "-c", "perl -e 'for($t=0; sleep(1); $t++) {print STDERR rand().\"\\n\"; if ($t>5) {kill(9,$$);}}' 1>&2") |
| 65 |
print("execlp(perl)") |
| 66 |
|
| 67 |
sys.exit(1) |
| 68 |
elif t == 2: |
| 69 |
os.execlp("/bin/sh", "sh", "-c", "(ls -la; ping -c 8 localhost) 1>&2") |
| 70 |
|
| 71 |
print("execlp(ls/ping)") |
| 72 |
sys.exit(1) |
| 73 |
else: |
| 74 |
|
| 75 |
G_outfd[t] = out[0] |
| 76 |
os.close(out[1]) |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
def data_ready(fd, data): |
| 81 |
t = data |
| 82 |
s = "" |
| 83 |
bytes = os.read(fd, 4096-1); |
| 84 |
if len(bytes) == 0: |
| 85 |
G_buff[t].append("\n\n*** EOF ***\n") |
| 86 |
try: |
| 87 |
(pid,status) = os.waitpid(G_pids[t], os.WNOHANG) |
| 88 |
except: |
| 89 |
bytes = "waitpid(): no child process\n" |
| 90 |
else: |
| 91 |
if os.WIFEXITED(status): |
| 92 |
bytes = f"Exit={os.WEXITSTATUS(status)}\n" |
| 93 |
os.close(fd) |
| 94 |
Fl.remove_fd(fd) |
| 95 |
G_pids[t] = -1 |
| 96 |
elif os.WIFSIGNALED(status): |
| 97 |
bytes = f"Killed with {os.WTERMSIG(status)}\n" |
| 98 |
os.close(fd) |
| 99 |
Fl.remove_fd(fd) |
| 100 |
G_pids[t] = -1 |
| 101 |
elif os.WIFSTOPPED(status): |
| 102 |
bytes = f"Stopped with {os.WSTOPSIG(status)}\n" |
| 103 |
|
| 104 |
G_buff[t].append(bytes) |
| 105 |
else: |
| 106 |
if sys.version >= '3': |
| 107 |
G_buff[t].append(bytes.decode('utf-8')) |
| 108 |
else: |
| 109 |
G_buff[t].append(bytes) |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
def close_cb(widget): |
| 114 |
print("Killing child processes..\n") |
| 115 |
for t in range(3): |
| 116 |
if G_pids[t] == -1: |
| 117 |
continue |
| 118 |
os.kill(G_pids[t], 9) |
| 119 |
sys.exit(0) |
| 120 |
|
| 121 |
|
| 122 |
if __name__=='__main__': |
| 123 |
if platform.platform().find("Windows") >= 0: |
| 124 |
fl_message("Not supported on Windows platform!") |
| 125 |
else: |
| 126 |
|
| 127 |
win = Fl_Double_Window(620,520,"fltk-tty"); |
| 128 |
win.callback(close_cb) |
| 129 |
|
| 130 |
|
| 131 |
for t in range(3): |
| 132 |
start_child(t) |
| 133 |
G_buff[t] = Fl_Text_Buffer() |
| 134 |
G_disp[t] = Fl_Text_Display(10+t*200, 10, 200, 500) |
| 135 |
G_disp[t].buffer(G_buff[t]) |
| 136 |
G_disp[t].textfont(FL_COURIER) |
| 137 |
G_disp[t].textsize(12) |
| 138 |
Fl.add_fd(G_outfd[t], data_ready, t) |
| 139 |
|
| 140 |
win.resizable(win) |
| 141 |
win.show() |
| 142 |
Fl.run() |
| 143 |
|