[轉錄][問題]定期砍掉run的死程序

看板LinuxDev作者 (艾斯寇德)時間16年前 (2007/09/26 21:09), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/4 (看更多)
※ [本文轉錄自 C_and_CPP 看板] 作者: cerebellum (ymca) 看板: C_and_CPP 標題: [問題]定期砍掉run的死程序 時間: Wed Sep 26 21:03:52 2007 由於qkmj這個程式若是不正常斷線 會造成一個死程序 且cpu資源大量被其佔用 參考了彰師大生物系BioStar的程式 發現在ubuntu上實際執行起來只會砍掉一個執行序 而不會把全部正在run的qkmj程式砍除 而且有時候執行根本無效 要執行多次才會成功 以下是程式碼 有經過稍微的修改(因為原本的根本不會跑) 是否可以請各位幫忙看一下是那裡的問題 感謝 checkqkmj.c #include <stdio.h> int main() { int line; FILE *fp; char a[20], b[20], c[20], d[20], e[20], f[20]; char g[20], h[20], i[20], j[20], k[20]; char cmd[128], *str; char tmpfile[32]; int process_a[16], process_b[16], ii, jj; strcpy(tmpfile, "/tmp/killtop1"); /* 只取出非 idle 中的 qkmj */ sprintf(cmd, "top -i -b -n 1 | grep qkmj > %s", tmpfile); /* 製作第一份執行中 qkmj 程序名單 */ system(cmd); if (!(fp = fopen(tmpfile, "r"))) return -1; for (line = 0; line <= 14; line++) if (fscanf(fp, "%s %s %s %s %s %s %s %s %s %s %s", a, b, c, d, e, f, g, h, i, j, k) == 11) process_a[line] = atoi(a); else process_a[line] = 0; fclose(fp); unlink(tmpfile); sleep(5); /* 小睡五秒 */ /* 製作第二份執行中 qkmj 程序名單 */ system(cmd); if (!(fp = fopen(tmpfile, "r"))) return -1; for (line = 0; line <= 14; line++) if (fscanf(fp, "%s %s %s %s %s %s %s %s %s %s %s", a, b, c, d, e, f, g, h, i, j, k) == 11) process_b[line] = atoi(a); else process_b[line] = 0; fclose(fp); unlink(tmpfile); /* 比對兩份 qkmj 程序名單 */ for (ii = 0; ii <= 14; ii++) { if (!process_a[ii]) break; for (jj = 0; jj <= 14; jj++) { if (!process_b[jj]) break; if (process_a[ii] == process_b[jj]) /* 如果發現有相同 */ { sprintf(cmd, "kill -9 %d", process_a[ii]); system(cmd); /* 代表這極有可能是死程序,必須 kill 掉 */ } } } return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.64.220

09/26 21:05,
...
09/26 21:05
-- IceCold::IceCode -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.132.228.138

09/27 00:53, , 1F
兩個for loop中的break是continue的筆誤?
09/27 00:53, 1F
文章代碼(AID): #16-bdrBz (LinuxDev)
文章代碼(AID): #16-bdrBz (LinuxDev)