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传说精度更高。
    
     
  • 相关阅读:
    面试题26:复杂链表的复制
    面试题25:二叉树中和为某一值的路径
    面试题24:二叉搜索树后序遍历
    面试题23:二叉树层序遍历
    面试题22:栈的压入,弹出序列
    面试题21:包含min函数的栈
    面试题20:顺时针打印矩阵
    面试题19:二叉树镜像
    plugin.go 源码阅读
    server.go 源码阅读
  • 原文地址:https://www.cnblogs.com/zendu/p/4988386.html
Copyright © 2011-2022 走看看