linux系统调用函数

acct: 禁止/启用系统记录进程信息。若file为NULL,记录功能将被禁用;
  若file指向一个存在的文件(以'\0'结尾),则记录功能被启用,
  正常结束的进程都会在该文件尾添加对应的信息。异常结束是指重启或其它致命的系统问题。
  acct()不能生成那些永远不会结束的进程的信息。
  关于acct()使用的记录结构及其它相关信息,请参阅<sys/acct.h>及acct(5)。

  该系统调用只能由超级用户使用

 

uselib: linux专用, 加载共享库

 

sysinfo: 启动到现在的时间,返回总的可用内存大小, 未用内存大小, 共享内存大小, 缓冲区大小, 交换区大小, 可用交换区大小, 当前进程数。

personality: linux spec, 执行域

 

yield: 让出自己, 引起重新调度,http://www.chineselinuxuniversity.net/articles/40191.shtml

 

futex:等待给定地址值变化, 和唤醒等待特定地址的进程, 常用于共享内存

http://www.chineselinuxuniversity.net/articles/40191.shtml

 

io_setup/io_submit/io_destroy/io_getevents: http://www.hadoopor.com/thread-956-1-1.html

直接异步aio操作

#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <libaio.h>

int main()
{
        io_context_t ctx;
        unsigned nr_events = 10;
        memset(&ctx, 0, sizeof(ctx));  // It's necessary,这里一定要的
        int errcode = io_setup(nr_events, &ctx);
        if (errcode == 0)
                printf("io_setup success\n");
        else
                printf("io_setup error: :%d:%s\n", errcode, strerror(-errcode));

        // 如果不指定O_DIRECT,则io_submit操作和普通的read/write操作没有什么区别了,将来的LINUX可能
        // 可以支持不指定O_DIRECT标志

        int fd = open("./direct.txt", O_CREAT|O_DIRECT|O_WRONLY, S_IRWXU|S_IRWXG|S_IROTH);
        printf("open: %s\n", strerror(errno));

        char* buf;
        errcode = posix_memalign((void**)&buf, sysconf(_SC_PAGESIZE), sysconf(_SC_PAGESIZE));
        printf("posix_memalign: %s\n", strerror(errcode));

        strcpy(buf, "hello xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

        struct iocb *iocbpp = (struct iocb *)malloc(sizeof(struct iocb));
        memset(iocbpp, 0, sizeof(struct iocb));

        iocbpp[0].data           = buf;
        iocbpp[0].aio_lio_opcode = IO_CMD_PWRITE;
        iocbpp[0].aio_reqprio    = 0;
        iocbpp[0].aio_fildes     = fd;

        iocbpp[0].u.c.buf    = buf;
        iocbpp[0].u.c.nbytes = page_size;//strlen(buf); // 这个值必须按512字节对齐
        iocbpp[0].u.c.offset = 0; // 这个值必须按512字节对齐

        // 提交异步操作,异步写磁盘
        int n = io_submit(ctx, 1, &iocbpp);
        printf("==io_submit==: %d:%s\n", n, strerror(-n));

        struct io_event events[10];
        struct timespec timeout = {1, 100};
        // 检查写磁盘情况,类似于epoll_wait或select
        n = io_getevents(ctx, 1, 10, events, &timeout);
        printf("io_getevents: %d:%s\n", n, strerror(-n));

        close(fd);
        io_destroy(ctx);
        return 0;
}

struct iocb {
       /* these are internal to the kernel/libc. */
       __u64   aio_data;       /* data to be returned in event\'s data */用来返回异步IO事件信息的空间,类似于epoll中的ptr。
       __u32   PADDED(aio_key, aio_reserved1); /* the kernel sets aio_key to the req # */
       /* common fields */
       __u16   aio_lio_opcode; /* see IOCB_CMD_ above */
       __s16   aio_reqprio;      // 请求的优先级
       __u32   aio_fildes;        //  文件描述符
       __u64   aio_buf;           // 用户态缓冲区
       __u64   aio_nbytes;      // 文件操作的字节数
       __s64   aio_offset;       // 文件操作的偏移量

       /* extra parameters */
       __u64   aio_reserved2;  /* TODO: use this for a (struct sigevent *) */
       __u64   aio_reserved3;
}; /* 64 bytes */

struct io_event {
       __u64           data;          /* the data field from the iocb */ // 类似于epoll_event中的ptr
       __u64           obj;            /* what iocb this event came from */ // 对应的用户态iocb结构体指针
       __s64           res;            /* result code for this event */ // 操作的结果,类似于read/write的返回值
       __s64           res2;          /* secondary result */
};

 

系统调用功能原型
io_setup为当前进程初始化一个异步IO上下文int io_setup(unsigned nr_events,aio_context_t *ctxp);
io_submit提交一个或者多个异步IO操作int io_submit(aio_context_t ctx_id,long nr, struct iocb **iocbpp);
io_getevents获得未完成的异步IO操作的状态int io_getevents(aio_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout);
io_cancel取消一个未完成的异步IO操作int io_cancel(aio_context_t ctx_id, struct iocb *iocb, struct io_event *result);
io_destroy从当前进程删除一个异步IO上下文int io_destroy(aio_context_t ctx);

 

posix_fadvise: 预定义file数据的操作行为,主要用于缓冲优化:

linux下读写文件时,OS会为文件建立缓存,用以提高速度。这部分用于缓存的内存,在文件关闭后,仍然不会被释放(什么时候会被释放不得而知)。

但是在内存使用有限度或者文件很大的情况下,有时需要马上释放缓存或者禁止使用缓存。

有以下两种方法。

1,posix_fadvise函数

int posix_fadvise(int fd, off_t offset, off_t len, int advice);
posix_fadvise(
fd, 0, 0, POSIX_FADV_DONTNEED);
len=0表示文件全部

POSIX_FADV_DONTNEED表示告诉os,这个文件在最近一段时间内不会被使用

试验了一下,发现文件打开后读写前使用这个函数没有用处

文件读写之后关闭之前使用有用

2,/proc/sys/vm/drop_caches    Linux 2.6.16 以后有效

释放PageCache echo 1 > /proc/sys/vm/drop_caches

释放dentry、inode    echo 2 > /proc/sys/vm/drop_caches

释放PageCache dentry、inode  echo 3 > /proc/sys/vm/drop_caches

这个方法没有试验过

 

 

mbind:设置mem策略

 

add_key/request_key/keyctl: linux 内核密钥服务:它的主要意图是在 Linux 内核中缓存身份验证数据。远程文件系统和其他内核服务可以使用这个服务来管理密码学、身份验证标记、跨域用户映射和其他安全问题。它还使 Linux 内核能够快速访问所需的密钥,并可以用来将密钥操作(比如添加、更新和删除)委托给用户空间。

http://news.dayoo.com/tech/201005/21/10000612_102055614.htm

具体还需要学习密钥原理

 

 

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值