[問題] linux 的 signal
( *[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)
※ yueayase:轉錄至看板 LinuxDev 12/06 01:19
推
12/06 16:58, , 1F
12/06 16:58, 1F