리눅스에서 프로세스 상태 검사 API

misari의 이미지

HP-UX용 응용프로그램을 리눅스로 변환하고 있습니다. 그러나 "pstat.h"와 관련된 문제를 갖고 있습니다. 특정 프로세스의 상태를 온라인으로 감시하기 위하여 HP-UX에서는 "pstat_getproc" 함수를 이용하여 pst_cpu (cpu utilization), pst_cptickstotal(cpu total ticks for process), usercycles, systemcycles, 등을 검사하여 특정 프로세스가 정상적으로 동작하고 있는지 확인하도록 되어 있습니다.
리눅스에는 이에 대응하는 API가 없군요. 우여곡절 끝에 비슷한 API를 찾았습니다. "get_proc_stats"라는 API이고, "libproc"에서 제공되는군요. 하지만, 프로세스 상태라던지, "user-mode CPU time"이나 "kernel-mode CPU time" 등만 있고, HP-UX에서와 같은 기능을 제공하지는 않습니다. 이 기능들로 구현을 시도해 보았지만, CPU를 많이 쓰지 않는 프로세스일 경우, 위 두 가지 값이 항상 '0'이어서 원하는 기능을 구현할 수 없네요.

다음은 HP-UX에서 구현된 소스 코드입니다.

int goodStatus(int pid)
{
   static long   lastUserCycles;
   static long   lastSystemCycles;
 
   struct   pst_status   pst;
   int      retval = GOOD_STATUS;
 
   if (pstat_getproc(&pst, sizeof(pst_status), 0, pid) != -1)
   {
      if ((pst.pst_stat != PS_SLEEP) && (pst.pst_stat != PS_RUN))
         return BAD_STATUS;
 
      // if no user or system cycles used since last check,
      // and processor utilization is 0, assume process has a problem.
      if (lastUserCycles == pst.pst_usercycles.psc_lo  &&
          lastSystemCycles == pst.pst_systemcycles.psc_lo  &&
          0 == pst.pst_cpu)
      {
         cout << "  NO ACTIVITY Status = " << dec << pst.pst_stat 
              << " CPU total ticks = " << pst.pst_cptickstotal << endl;
         cout << "pid = " << pst.pst_pid << " name = "
              << pst.pst_ucomm << " has NO ACTIVITY" << endl;
         cout << "   proc util: " << pst.pst_cpu 
              << " user cycles: " << pst.pst_usercycles.psc_hi 
              << pst.pst_usercycles.psc_lo
              << " system cycles: " << pst.pst_systemcycles.psc_hi 
              << pst.pst_systemcycles.psc_lo
              << endl;
         return BADSTATUS;
      }
      else
      {
         lastUserCycles = pst.pst_usercycles.psc_lo;
         lastSystemCycles = pst.pst_systemcycles.psc_lo;
      }
   }
 
   return retval;
}
IsExist의 이미지

top 소스를 분석해보면 아시겠지만 리눅스에서는 /proc 밑의 파일을 바로 해석합니다.
---------
간디가 말한 우리를 파괴시키는 7가지 요소

첫째, 노동 없는 부(富)/둘째, 양심 없는 쾌락
셋째, 인격 없는 지! 식/넷째, 윤리 없는 비지니스

이익추구를 위해서라면..

다섯째, 인성(人性)없는 과학
여섯째, 희생 없는 종교/일곱째, 신념 없는 정치

---------
간디가 말한 우리를 파괴시키는 7가지 요소

첫째, 노동 없는 부(富)/둘째, 양심 없는 쾌락
셋째, 인격 없는 지! 식/넷째, 윤리 없는 비지니스

이익추구를 위해서라면..

다섯째, 인성(人性)없는 과학
여섯째, 희생 없는 종교/일곱째, 신념 없는 정치

misari의 이미지

간단한 로직으로 구성된 프로그램으로 시험해 보면 변하는 수치가 거의 없더군요.

$ cat /proc/13814/status 
Name:	testproc
State:	S (sleeping)
Tgid:	13814
Pid:	13814
PPid:	13813
TracerPid:	0
Uid:	1000	1000	1000	1000
Gid:	1000	1000	1000	1000
FDSize:	64
Groups:	4 7 20 24 46 106 121 122 1000 
VmPeak:	   11752 kB
VmSize:	   11624 kB
VmLck:	       0 kB
VmHWM:	     944 kB
VmRSS:	     944 kB
VmData:	     132 kB
VmStk:	      84 kB
VmExe:	       4 kB
VmLib:	    3132 kB
VmPTE:	      44 kB
Threads:	1
SigQ:	0/16382
SigPnd:	0000000000000000
ShdPnd:	0000000000000000
SigBlk:	0000000000000000
SigIgn:	0000000000000000
SigCgt:	0000000000000000
CapInh:	0000000000000000
CapPrm:	0000000000000000
CapEff:	0000000000000000
CapBnd:	ffffffffffffffff
Cpus_allowed:	3
Cpus_allowed_list:	0-1
Mems_allowed:	00000000,00000001
Mems_allowed_list:	0
voluntary_ctxt_switches:	154581
nonvoluntary_ctxt_switches:	1
 
$ cat /proc/13814/status 
Name:	testproc
State:	S (sleeping)
Tgid:	13814
Pid:	13814
PPid:	13813
TracerPid:	0
Uid:	1000	1000	1000	1000
Gid:	1000	1000	1000	1000
FDSize:	64
Groups:	4 7 20 24 46 106 121 122 1000 
VmPeak:	   11752 kB
VmSize:	   11624 kB
VmLck:	       0 kB
VmHWM:	     944 kB
VmRSS:	     944 kB
VmData:	     132 kB
VmStk:	      84 kB
VmExe:	       4 kB
VmLib:	    3132 kB
VmPTE:	      44 kB
Threads:	1
SigQ:	0/16382
SigPnd:	0000000000000000
ShdPnd:	0000000000000000
SigBlk:	0000000000000000
SigIgn:	0000000000000000
SigCgt:	0000000000000000
CapInh:	0000000000000000
CapPrm:	0000000000000000
CapEff:	0000000000000000
CapBnd:	ffffffffffffffff
Cpus_allowed:	3
Cpus_allowed_list:	0-1
Mems_allowed:	00000000,00000001
Mems_allowed_list:	0
voluntary_ctxt_switches:	169929
nonvoluntary_ctxt_switches:	1

"voluntary_ctxt_switches" 값만 유일하게 변하는 군요.

-----------------
꿈을 버리지 말자!

꿈을 버리지 말자!

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.