zoukankan      html  css  js  c++  java
  • linux 进程学习笔记-暂停进程

    <!--[if !supportLists]-->Ÿ <!--[endif]-->暂停进程 
    
    int pause() 
    
    其会挂起当前进程直到有信号来唤醒或者进程被结束。 
    
    随便提一下,如果你仅仅需要简单地暂停一下(press any key  to continue...), 可以使用 system("pause")这个系统调用,甚至是getch()之类的。 
    
    下面这个demo有点晦涩,需要一些“信号”的知识。你可以阅读完“进程间通信——信号”后再回过头来看。其利用alarm(10)让系统再10秒后给自己发一个SIGALRM信号,然后暂停进程,10秒后,进程收到信号后被唤醒。 
    
    
    #include <unistd.h>
    #include <signal.h>
    #include <stdio.h>
    #include <time.h>
     
    void catcher( int sig ) {
        printf( "Signal catcher called for signal %d
    ", sig );
    }
     
    void showtime( ) {
        time_t t;
        time(&t);
        printf( "the time is %s
    ", ctime(&t) );
    }
     
    int main()  {
     
        struct sigaction sigact;
     
        sigemptyset( &sigact.sa_mask );
        sigact.sa_flags = 0;
        sigact.sa_handler = catcher;
        sigaction( SIGALRM, &sigact, NULL );
     
        alarm( 10 );
     
    printf("before pause, ");
        showtime ();
        
    pause();
        
    printf("after pause, ");
    showtime ();
     
        return 0;
    }
    输出如下:
    before pause, the time is Thu Jun 23 17:12:25 2011
    signal catcher called for signal 14
    after pause, the time is Thu Jun 23 17:12:35 2011
     
    unsigned sleep(unsigned seconds)
    int usleep(useconds_t useconds)
    int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
    sleep系列函数都是让进程挂起一段时间,sleep只能精确到秒,usleep能精确到微妙,而nanosleep传说精度更高。
    
     
  • 相关阅读:
    npm ci命令比npm installer命令快2至10倍
    Liferay 7.1发布啦
    2016/07/05 zend optimizer
    2016/06/16 phpexcel
    2016/06/13 phpexcel 未完待续
    2016/06/10 日历插件 Datepicker
    2016/06/09 ThinkPHP3.2.3使用分页
    2016/06/02 网摘记录 svn 服务器端 客户端 安装使用
    2016/05/27 php上传文件常见问题总结
    2016/05/25 抽象类与API(接口)差别
  • 原文地址:https://www.cnblogs.com/zendu/p/4988386.html
Copyright © 2011-2022 走看看