mac os x顯示進程產生的Mach與BSD系統調用的數量

// 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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章