#include "csapp.h" /*编写sleep的包装函数,snooze函数,返回一个进程实际休眠了多少时间*/ unsigned int snooze(unsigned int secs) //secs为总休眠时间 { unsigned int timeLeft; unsigned int timeAll=secs; timeLeft=sleep(timeAll); printf("Slept for %u of %u secs \n", timeAll-timeLeft,timeAll); return timeLeft; } void handler(int sig) { return; //结束进程,并返回进程的返回值 } int main(int argc,char *argv[]) { if(signal(SIGINT,handler)==SIG_ERR) printf("signal error"); if(argv[1]==NULL) { printf("需要指明参数,即休眠的时间长度\n"); exit(0); } (void) snooze(atoi(argv[1])); //atoi是用来将字符串转换成整型数据 exit(0); }
当程序执行时,按住ctrl+C中断进程,返回值例如下所示
Slept for 2 of 4 secs
时间: 2024-10-24 21:38:49