[問題] linux 的 signal

看板LinuxDev作者 (scrya)時間15年前 (2010/12/06 01:19), 編輯推噓1(103)
留言4則, 3人參與, 最新討論串1/1
※ [本文轉錄自 C_and_CPP 看板 #1C-cRy8- ] 作者: yueayase (scrya) 看板: C_and_CPP 標題: [問題] linux 的 signal 時間: Sun Dec 5 00:06:17 2010 ( *[1m *[m 為色碼,可以按 Ctrl+V 預覽會顯示的顏色 ) ( 未必需要依照此格式,文章條理清楚即可 ) 遇到的問題: (題意請描述清楚) 讀進一可執行檔,對其資源的使用作限制,所以寫了以下的codes做測試: I. testcase.c #include <stdio.h> #include <stdlib.h> #include <math.h> int main() { FILE *f; int i; f = tmpfile(); for(i = 0; i < 10000; i++){ fprintf(f, "Do some output\n"); if(ferror(f)){ fprintf(stderr, "Error Writing to temporary file\n"); break; } } return 0; } II. restrition.c: #include <sys/types.h> #include <sys/resource.h> #include <sys/time.h> #include <signal.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main() { struct rlimit r_limit; int status; /* setting 2kbytes file size restriction */ r_limit.rlim_cur = 2048; r_limit.rlim_max = 4096; setrlimit(RLIMIT_FSIZE, &r_limit); status = WTERMSIG( system(./testcase) ); printf("Signal Type: %s\n", strsignal(status)); return 0; } 希望得到的正確結果: Signal Type: SIGXFSZ 程式跑出來的錯誤結果: File size limit exceeded Signal Type: Unknown signal 0 開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux) ubuntu10.04(linux) 有問題的code: (請善用置底文標色功能) system() 對 signal 接收的用法錯誤? 補充說明: 如果改成fork(),那child process 似乎只能用 system("./testcase") 如果用execv, execl,....,應該會直接結束,好像也不是這樣用 我不希望出現File size limit exceeded 但這好像是作業系統的問題 希望能得到正確的解答,因為說明文件是說system(const char* command)的回傳值是: (i) if command is NULL, system() will return nonzero value if a shell is available. (ii) if a child process could not be created or its termination status could not be retreived, then system returns -1. (iii) if a shell could not be execed in the child process, then system returns a value as though the child shell had terminated with the call _exit(127); (iv) If all system calls succeed, system() returns the termination status of the child shell used to execute command.(The termination status of a shell is the termination status of the last command it executes.) In the last two cases, the return value of system() is a wait status of the same form returned by waitpid(). 另外將 restrition.c改成: #include <sys/types.h> #include <sys/resource.h> #include <sys/time.h> #include <signal.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> void signalhandler(int sig) { printf("Oops!! I got signal %d", sig); (void) signal(SIGXFSZ, SIGDFL); } int main() { (void) signal(SIGXFSZ, signalhandler); struct rlimit r_limit; int status; /* setting 2kbytes file size restriction */ r_limit.rlim_cur = 2048; r_limit.rlim_max = 4096; setrlimit(RLIMIT_FSIZE, &r_limit); status = WTERMSIG( system(./testcase) ) printf("Signal Type: %s\n", strsignal(status)); return 0; } signal handler() 也沒反應 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.173.157.58 ※ 編輯: yueayase 來自: 218.173.157.58 (12/05 00:07) ※ 編輯: yueayase 來自: 218.173.157.58 (12/05 00:09) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.251.191.9

12/06 12:39, , 1F
你要抓的是 return value 還是 signal ?
12/06 12:39, 1F

12/06 14:50, , 2F
你確定 signal 是這樣用的?
12/06 14:50, 2F

12/06 21:09, , 3F
的確是搞錯了,沒想到system()會把setrlimit的效果取消
12/06 21:09, 3F

12/06 21:10, , 4F
所以接不到./testcase產生的signal
12/06 21:10, 4F
文章代碼(AID): #1C-ycZIX (LinuxDev)
文章代碼(AID): #1C-ycZIX (LinuxDev)