我寫了一篇關於我的Linux機器一個簡單的例子,如何將一個子進程放緩與SIGSTOP和SIGCONT信號:
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
void dosomething(void){
static volatile unsigned char buffer[1000000];
for(unsigned i=0;i<1000;i++) for(unsigned j=0;j<sizeof(buffer);buffer[j++]=i){;}
}
#define RUN 1
#define WAIT 1
int main(void){
int delay=0, status, pid = fork();
if(!pid){ kill(getpid(),SIGSTOP); dosomething(); return 0; }
do{
waitpid(pid, &status, WUNTRACED | WCONTINUED);
if(WIFSTOPPED (status) ){ sleep(delay); kill(pid,SIGCONT); }
if(WIFCONTINUED(status) && WAIT){ sleep(RUN ); kill(pid,SIGSTOP); }
delay=WAIT;
}while(!WIFEXITED(status) && !WIFSIGNALED (status));
}
時WAIT
爲零沒有放緩,否則每RUN
秒後父阻止孩子爲WAIT
秒。
運行結果:
RUN=1 WAIT=0
---------------
real 3.905s
user 3.704s
sys 0.012s
RUN=1 WAIT=1
---------------
real 9.061s
user 3.640s
sys 0.016s
RUN=1 WAIT=2
---------------
real 13.027s
user 3.372s
sys 0.032s
讓舊遊戲在新電腦上工作的解決方案是在DOSBox中運行它們(我認爲它在386上模擬DOS)。 – 2010-02-07 08:19:01