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;
}
用工具: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