在Solaris上,你可以通過mmaping做到這一點的/ proc/PID/LWP/LWPID/lwpstatus
簡單的例子:
#define _STRUCTURED_PROC 1
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/procfs.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
int main() {
const time_t start = time(NULL);
while (time(NULL) - start < 10);
int fd = open("/proc/self/lwp/1/lwpstatus", O_RDONLY);
assert(fd >= 0);
lwpstatus_t status;
const ssize_t ret = read(fd, &status, sizeof(lwpstatus_t));
close(fd);
assert(sizeof(lwpstatus_t) == ret);
printf("User CPU time: %ld\n", status.pr_utime.tv_sec);
printf("System time: %ld\n", status.pr_stime.tv_sec);
return 0;
}
這兒,這是當前進程的第一個線程,但任何價值可以使用PID和LWPID,前提是您有足夠的訪問權限並存在。
這在我的Solairs測試機上運行正常(SunOS wiked 5.10 Generic_142900-13 sun4v sparc SUNW,T5140)。
我的確嘗試過在這個例子中使用mmap,但是從調用返回的「操作不適用」,所以放棄了並用read來代替。
man -s4 proc詳細介紹了/ proc的信息 – Flexo 2010-10-27 15:11:00
解析/ proc中的文件似乎是最優雅的解決方案。我原本想到這一點,但不知道如何解決這個問題。非常感謝awoodland。 – marcus 2010-11-04 08:32:50