zoukankan      html  css  js  c++  java
  • unistd.h

    unistd.h是unix std的意思,是POSIX标准定义的unix类系统定义符号常量的头文件,

    包含了许多UNIX系统服务的函数原型

    unistd.h在unix中类似于window中的windows.h!

    #ifdef WIN32
    #include <windows.h>
    #else
    #include <unistd.h>
    #endif

    unistd.h含有的常量与函数:

    ssize_t      read(int, void *, size_t); // 读取文件使用
    int          unlink(const char *);
    ssize_t      write(int, const void *, size_t); // 写文件
    int          usleep(useconds_t); // 进程休眠,单位为微妙
    unsigned     sleep(unsigned); // 进程休眠,单位为秒
    
    int          access(const char *, int); // 获取文件的权限
    unsigned     alarm(unsigned);
    int          chdir(const char *);
    int          chown(const char *, uid_t, gid_t);
    int          close(int); // 关闭文件
    size_t       confstr(int, char *, size_t);
    void        _exit(int);
    pid_t        fork(void);
    
     
    
    NULL           // Null pointer
    SEEK_CUR    // Set file offset to current plus offset.
    SEEK_END    // Set file offset to EOF plus offset.
    SEEK_SET    // Set file offset to offset.

    在进行堵塞式系统调用时。为避免进程陷入无限期的等待,能够为这些堵塞式系统调用设置定时器。Linux提供了alarm系统调用和SIGALRM信号实现这个功能。

            要使用定时器。首先要安装SIGALRM信号。假设不安装SIGALRM信号,则进程收到SIGALRM信号后。缺省的动作就是终止当前进程。

    SIGALRM信号成功安装后,在什么情况下进程会收到该信号呢?这就要依赖于Linux提供的定时器功能。在Linux系统下,每一个进程都有惟一的一个定时器,该定时器提供了以秒为单位的定时功能。在定时器设置的超时时间到达后,调用alarm的进程将收到SIGALRM信号。

    void main()
    {
        //安装SIGALRM信号
        if(signal(SIGALRM,CbSigAlrm)==SIG_ERR)
        {
            perror("signal");
            return;
        }
        //关闭标准输出的行缓存模式
        setbuf(stdout,NULL);
        //启动定时器
        alarm(1);
        //进程进入无限循环,仅仅能手动终止
        while(1)
        {
            //暂停,等待信号
            pause();
        }
    }        
    alarm函数的使用:
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    
    static int flag = 0;
    void handle(int signum){
        printf("hello world %d
    ", flag++);
        alarm(1);
    }
    
    int main() {
        alarm(1);
        signal(SIGALRM, handle);
        while(1);
    }
  • 相关阅读:
    9.11 eventbus
    9.10,,,实现new instanceof apply call 高阶函数,偏函数,柯里化
    9.9 promise实现 写完了传到gitee上面了,这里这个不完整
    9.5cors配置代码
    9.5 jsonp 实现
    9.5 http tcp https总结
    9.3 es6 class一部分 and es5 class 发布订阅
    8.30 cookie session token jwt
    8.30vue响应式原理
    warning: LF will be replaced by CRLF in renard-wx/project.config.json. The file will have its original line endings in your working directory
  • 原文地址:https://www.cnblogs.com/pjl1119/p/9922469.html
Copyright © 2011-2022 走看看