ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

mac os x显示进程产生的Mach与BSD系统调用的数量

2019-07-06 19:00:07  阅读:402  来源: 互联网

标签:calls system mac call syscalls BSD os Unix Mach


// syscalls_test.c

#include <stdio.h>
#include <fcntl.h>>
#include <unistd.h>
#include <mach/mach.h>

int
main()
{
    int           i, fd;
    mach_port_t   p;
    kern_return_t kr;

    setbuf(stdout, NULL);
    printf("My pid is %d\n", getpid());
    printf("Note the number of Mach and Unix system calls, and press <enter>");
    (void)getchar();

    // At this point, we will have some base numbers of Mach and Unix
    // system calls made so far, say, M and U, respectively

    for (i = 0; i < 100; i++) { // 100 iterations

        // +1 Unix system call per iteration
        fd = open("/dev/null", O_RDONLY);

        // +1 Unix system call per iteration
        close(fd);

        // +1 Mach system call per iteration
        kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &p);

        // +1 Mach system call per iteration
        kr = mach_port_deallocate(mach_task_self(), p);

    }

    // +1 Unix system call
    printf("Note the number of Mach and Unix system calls again...\n"
           "Now sleeping for 60 seconds...");

    // sleep(3) is implemented using nanosleep, which will call
    // clock_get_time() and clock_sleep_trap() -- this is +2 Mach system calls

    (int)sleep(60);

    // Mach system calls = M + 2 * 100 + 2 (that is, 202 more calls)
    // Unix system calls = U + 2 * 100 + 1 (that is, 201 more calls)

    return 0;
}

mac os x显示进程产生的Mach与BSD系统调用的数量
用工具:https://blog.51cto.com/haidragon/2417758

haidragondeMacBook-Pro:7-22 haidragon$ gcc -Wall -o syscalls_test syscalls_test.c 
syscalls_test.c:4:19: warning: extra tokens at end of #include directive [-Wextra-tokens]
#include <fcntl.h>>
                  ^
                  //
syscalls_test.c:46:5: warning: expression result unused [-Wunused-value]
    (int)sleep(60);
    ^    ~~~~~~~~~
2 warnings generated.
haidragondeMacBook-Pro:7-22 haidragon$ ./syscalls_test
My pid is 2128
Note the number of Mach and Unix system calls, and press <enter>

标签:calls,system,mac,call,syscalls,BSD,os,Unix,Mach
来源: https://blog.51cto.com/haidragon/2417769

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有