문제

다음 회원 기능이있는 수업이 있습니다.


/// caller pid
virtual pid_t Pid() const = 0; 

/// physical memory size in KB
virtual uint64_t Size() const = 0;  

/// resident memory for this process
virtual uint64_t Rss() const = 0; 

/// cpu used by this process
virtual double PercentCpu() const = 0; 

/// memory used by this process
virtual double PercentMemory() const = 0; 

/// number of threads in this process
virtual int32_t Lwps() const = 0; 

이 클래스의 의무는 발신자에 대한 프로세스 정보를 반환하는 것입니다. 물리적 메모리 크기는 sysctl 호출에 의해 쉽게 결정될 수 있으며, PID는 사소한 일이지만, 나머지 통화는 PS 또는 상단에서 Popen을 호출하고 출력을 구문 분석하는 것 외에도 허용되지 않습니다. 모든 도움은 대단히 감사하겠습니다.

요구 사항 :
G ++ 4.0에서 컴파일합니다
OBJ-C 없음
OSX 10.5

도움이 되었습니까?

해결책

프로세스 정보는 pidinfo:

cristi:~ diciu$ grep proc_pidinfo /usr/include/libproc.h

int proc_pidinfo(int pid, int flavor, uint64_t arg,  void *buffer, int buffersize);

CPU로드가 나옵니다 host_statistics:

cristi:~ diciu$ grep -r host_statistics /usr/include/

/usr/include/mach/host_info.h:/* host_statistics() */

/usr/include/mach/mach_host.defs:routine host_statistics(

/usr/include/mach/mach_host.h:/* Routine host_statistics */

/usr/include/mach/mach_host.h:kern_return_t host_statistics

자세한 내용은 출처를 확인하십시오 top 그리고 lsof, 그들은 오픈 소스입니다 (Apple 개발자로 등록해야하지만 무료입니다) :

https://opensource.apple.com/source/top/top-111.20.1/libtop.c.auto.html

나중에 편집 : 이 모든 인터페이스는 버전에 따라 다르므로 프로덕션 코드 (libproc.h)를 작성할 때이를 고려해야합니다.

/*
 * This header file contains private interfaces to obtain process information.
 * These interfaces are subject to change in future releases.
 */

다른 팁

당신은 객관적인 c라고 말하지 않기 때문에 대부분의 MacOS 프레임 워크를 배제 할 것입니다.

GetRusage ()를 사용하여 CPU 시간을 얻을 수 있으며, 이는 프로세스에 충전 된 총 사용자 및 시스템 CPU 시간을 제공합니다. CPU 백분율을 얻으려면 초당 GetRusage 값을 한 번 스냅 샷 (또는 세분화하려는).

#include <sys/resource.h>

struct rusage r_usage;

if (getrusage(RUSAGE_SELF, &r_usage)) {
    /* ... error handling ... */
}

printf("Total User CPU = %ld.%ld\n",
        r_usage.ru_utime.tv_sec,
        r_usage.ru_utime.tv_usec);
printf("Total System CPU = %ld.%ld\n",
        r_usage.ru_stime.tv_sec,
        r_usage.ru_stime.tv_usec);

getrusage 구조에는 RSS 필드가 있지만 MacOS X 10.5에서는 항상 0 인 것으로 보입니다. 마이클 나이트 몇 년 전에 RSS를 결정하는 방법에 대한 블로그 게시물을 썼습니다.

이 값의 대부분은 Mach API에서 사용할 수 있다고 생각하지만 거기에 찔린 지 오래되었습니다. 또는 "PS"또는 "TOP"명령에 대한 소스 코드를보고이를 수행하는 방법을 볼 수 있습니다.

Mac OS의 프로세스 정보를 위해 아래 코드를 사용할 수 있습니다.

void IsInBSDProcessList(char *name)    { 
  assert( name != NULL); 
  kinfo_proc *result; 
  size_t count = 0; 
  result = (kinfo_proc *)malloc(sizeof(kinfo_proc)); 
  if(GetBSDProcessList(&result,&count) == 0) { 
    for (int i = 0; i < count; i++) { 
      kinfo_proc *proc = NULL; 
      proc = &result[i]; 
      }
  } 
  free(result);
}

kinfo_proc struct에는 프로세스에 대한 모든 정보가 포함되어 있습니다. 프로세스 식별자 (PID), 프로세스 그룹, 프로세스 상태 등으로

이 정보의 대부분은 얻을 수 있습니다 getProcessInformation ().

그건 그렇고, 왜 프로세스 전체 정보를 반환하는 기능을위한 가상 방법은 무엇입니까?

이것은 탄소 코코아와 함께 작동하지 않습니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top