{"id":398,"date":"2014-03-11T13:51:30","date_gmt":"2014-03-11T05:51:30","guid":{"rendered":"http:\/\/wangkaixuan.tech\/?p=398"},"modified":"2020-06-07T14:06:02","modified_gmt":"2020-06-07T06:06:02","slug":"linux-%e4%b8%8b%e7%9b%91%e6%8e%a7%e7%a8%8b%e5%ba%8f-%e5%85%b1%e4%ba%ab%e5%86%85%e5%ad%98%e9%80%9a%e8%ae%af%ef%bc%8c%e6%b6%88%e6%81%af%e8%ae%a2%e9%98%85%e5%8f%91%e5%b8%83","status":"publish","type":"post","link":"http:\/\/www.wangkaixuan.tech\/?p=398","title":{"rendered":"Linux \u4e0b\u76d1\u63a7\u7a0b\u5e8f &#8212; \u5171\u4eab\u5185\u5b58\u901a\u8baf\uff0c\u6d88\u606f\u8ba2\u9605\u53d1\u5e03"},"content":{"rendered":"\n<p>\u5b9e\u4e60\u7684\u65f6\u5019\u5199\u7684\u5c0f\u7ec3\u4e60\u7a0b\u5e8f\uff1a<\/p>\n\n\n\n<p>VMSTAT.h<\/p>\n\n\n\n<p>\u6b64\u4ee3\u7801\u7528\u4ee5\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u8fd0\u884c\u72b6\u6001\u4fe1\u606f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class VMSTAT\n{\npublic:\n\tdouble stat&#91;20];\n\t\/*\n\t *       Proc\n\t *\n\t * 0:r: The number of processes waiting for run time.\n\t * 1:b: The number of processes in uninterruptible sleep.\n\t *\n\t *       Memory\n\t *\n\t * 2:swpd: the amount of virtual memory used.\n\t * 3:free: the amount of idle memory.\n\t * 4:buff: the amount of memory used as buffers.\n\t * 5:cache: the amount of memory used as cache.\n\t *\n\t *       Swap\n\t *\n\t * 6:si: Amount of memory swapped in from disk (\/s).\n\t * 7:Amount of memory swapped to disk (\/s).\n\t *\n\t *       IO\n\t *\n\t * 8:bi: Blocks received from a block device (blocks\/s).\n\t * 9:bo: Blocks sent to a block device (blocks\/s).\n\t *\n\t *       System\n\t *\n\t * 10:in: The number of interrupts per second, including the clock.\n\t * 11:cs: The number of context switches per second.\n\t *\n\t *       CPU\n\t *\n\t * 12:us: Time spent running non-kernel code. (user time, including nice time)\n\t * 13:sy: Time spent running kernel code. (system time)\n\t * 14:id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.\n\t * 15:wa: Time spent waiting for IO. Prior to Linux 2.5.41, included in idle.\n\t * 16:st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.\n\t *\n\t *       Add\n\t *\n\t * 17:Total Memory\n\t * 18:Used Memory\n\t * 19:CPU Usage\n\t *\/\n\n\tvoid update()\n\t{\n\n\t\t\/\/init array\n\t\tFILE *file = popen(\"vmstat\", \"r\");\n\t\tchar buf&#91;300];\n\t\tfgets(buf, 300, file);\n\t\tfgets(buf, 300, file);\n\t\tfgets(buf, 300, file);\n\n\t\tint index = 0;\n\t\tfor (int i = 0; i &lt; 300; i++)\n\t\t{\n\t\t\twhile (buf&#91;i] &lt; '0' || buf&#91;i] > '9')\n\t\t\t\ti++;\n\t\t\tint tmp = 0;\n\t\t\twhile (buf&#91;i] >= '0' &amp;&amp; buf&#91;i] &lt;= '9')\n\t\t\t{\n\t\t\t\ttmp = tmp * 10 + (buf&#91;i] - '0');\n\t\t\t\ti++;\n\t\t\t}\n\t\t\tstat&#91;index++] = tmp * 1.0;\n\t\t\tif (index == 17)\n\t\t\t\tbreak;\n\t\t}\n\t\tpclose(file);\n\n\t\t\/\/total memory\n\t\tfile = popen(\"cat \/proc\/meminfo\", \"r\");\n\t\tdouble total = 0;\n\t\tfgets(buf, 300, file);\n\t\tchar str&#91;7];\n\t\tfor (int i = 17; i &lt; 24; i++)\n\t\t\tstr&#91;i - 17] = buf&#91;i];\n\n\t\tfor (int i = 0; i &lt; 7; i++)\n\t\t{\n\t\t\ttotal = total * 10 + (str&#91;i] == ' ' ? 0 : (str&#91;i] - '0'));\n\t\t}\n\n\t\t\/\/calculate all\n\t\tstat&#91;17] = total;\n\t\tstat&#91;18] = total - stat&#91;3];\n\t\tstat&#91;19] = 100 - stat&#91;14];\n\t\tpclose(file);\n\n\t}\n\n};\n<\/code><\/pre>\n\n\n\n<p>pshm.h<\/p>\n\n\n\n<p>\u5b9a\u4e49\u5171\u4eab\u5185\u5b58\u6570\u636e\u7ed3\u6784\u7684\u7ed3\u6784\u4f53<br>\u5b9a\u4e49\u4e3b\u9898ID<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef struct tagMyStat\n{\n        int attche_count;\n        int total;\n        int avaliable;\n        int used;\n        int cpu_useage;\n \n} MyStat;\n \n#define CENTOS_PER   (4499)  \/\/\u4e3b\u9898CENTOS_PER \u7531\u670d\u52a1\u5668\u53d1\u5e03 \u5ba2\u6237\u7aef\u8fdb\u884c\u8ba2\u9605 \u670d\u52a1\u5668\u53ef\u53d1\u5e03\u591a\u4e2a\u4e3b\u9898\uff0c\u800c\u5ba2\u6237\u7aef\u53ef\u4ee5\u9009\u62e9\u5176\u4e2d\u82e5\u5e72\u4e2a\u611f\u5174\u8da3\u7684\u8fdb\u884c\u8ba2\u9605<\/code><\/pre>\n\n\n\n<p>shmClient.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h>\n#include &lt;time.h>\n#include &lt;stdlib.h>\n#include &lt;unistd.h>\n#include &lt;sys\/shm.h>\n#include  \"VMSTAT.h\"\n#include \"pshm.h\"\nint main()\n{\n\tvoid *shm = NULL;\n\tMyStat *shared = NULL;\n\tint shmid = shmget((key_t) CENTOS_PER, sizeof(MyStat), IPC_CREAT);\n\tif (shmid == -1)\n\t{\n\t\tprintf(\"%s\\n\", \"shmget failed!\");\n\t\treturn 0;\n\t}\n\tshm = shmat(shmid, (void*) 0, 0);\n\tif (shm == (void*) -1)\n\t{\n\t\tprintf(\"%s\\n\", \"shmat failed!\");\n\t}\n\tprintf(\"Memory attached at %lx\\n\", (long) shm);\n\tshared = (MyStat*) shm;\n\tshared->attche_count++;\n\ttime_t rawtime;\n\tstruct tm *t;\n\twhile (1)\n\t{\n\t\ttime(&amp;rawtime);\n\t\tt = localtime(&amp;rawtime);\n\t\tprintf(\n\t\t\t\t\"Client@%d:%d:%d :Total: %d MB  Aviliable: %d MB  Used: %d MB  CPU: %d %\\n\",\n\t\t\t\tt->tm_hour, t->tm_min, t->tm_sec, shared->total,\n\t\t\t\tshared->avaliable, shared->used, shared->cpu_useage);\n\t\tsleep(1);\n\t}\n\tshared->attche_count--;\n\tif (shmdt(shm) == -1)\n\t{\n\t\tprintf(\"%s\\n\", \"shmdt failed!\");\n\t}\n\tif (shmctl(shmid, IPC_RMID, 0) == -1)\n\t{\n\t\tprintf(\"%s\\n\", \"shmctl(IPC_RMID) failed!\");\n\t}\n\tsleep(2);\n\treturn 0;\n}\n<\/code><\/pre>\n\n\n\n<p>shmServer.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h>\n#include &lt;time.h>\n#include &lt;string.h>\n#include &lt;stdlib.h>\n#include &lt;unistd.h>\n#include &lt;sys\/shm.h>\n#include \"VMSTAT.h\"\n#include \"pshm.h\"\n\nint main()\n{\n\tvoid *shm = NULL;\n\tMyStat *shared = NULL;\n\n\tint shmid = shmget((key_t) CENTOS_PER, sizeof(MyStat), 0666 | IPC_CREAT);\n\tif (shmid == -1)\n\t{\n\t\tprintf(\"%s\\n\", \"shmget failed!\");\n\t\treturn 0;\n\t}\n\tshm = shmat(shmid, (void*) 0, 0);\n\tif (shm == (void*) -1)\n\t{\n\t\tprintf(\"%s\\n\", \"shmat failed!\");\n\t}\n\n\tprintf(\"Memory attached at %lx\\n\", (long) shm);\n\n\tshared = (MyStat*) shm;\n\tmemset(shared, 0, sizeof(MyStat));\n\tshared->attche_count = 0;\n\tVMSTAT s;\n\n\ttime_t rawtime;\n\tstruct tm *t;\n\n\twhile (1)\n\t{\n\t\ts.update();\n\t\tshared->total = int(s.stat&#91;17] \/ 1000);\n\t\tshared->avaliable = int(s.stat&#91;3] \/ 1000);\n\t\tshared->used = int(s.stat&#91;18] \/ 1000);\n\t\tshared->cpu_useage = int(s.stat&#91;19]);\n\n\t\ttime(&amp;rawtime);\n\t\tt = localtime(&amp;rawtime);\n\n\t\tprintf(\n\t\t\t\t\"Server@%2d:%2d:%2d (%d):Total: %d MB  Aviliable: %d MB  Used: %d MB  CPU: %d %\\n\",\n\t\t\t\tt->tm_hour, t->tm_min, t->tm_sec, shared->attche_count,\n\t\t\t\tshared->total, shared->avaliable, shared->used,\n\t\t\t\tshared->cpu_useage);\n\t\tsleep(1);\n\t}\n\n\tif (shmdt(shm) == -1)\n\t{\n\t\tprintf(\"%s\\n\", \"shmdt failed!\");\n\t}\n\n\tif (shmctl(shmid, IPC_RMID, 0) == -1)\n\t{\n\t\tprintf(\"%s\\n\", \"shmctl(IPC_RMID) failed!\");\n\t}\n\n\tsleep(2);\n\n\treturn 0;\n}\n<\/code><\/pre>\n\n\n\n<p>\u670d\u52a1\u5668\u8fd0\u884c\u622a\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"554\" height=\"224\" src=\"http:\/\/wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/1-1.png\" alt=\"\" class=\"wp-image-399\" srcset=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/1-1.png 554w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/1-1-300x121.png 300w\" sizes=\"auto, (max-width: 554px) 100vw, 554px\" \/><\/figure>\n\n\n\n<p>\u5ba2\u6237\u7aef\u8fd0\u884c\u622a\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"528\" height=\"162\" src=\"http:\/\/wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/2-1.png\" alt=\"\" class=\"wp-image-400\" srcset=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/2-1.png 528w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/2-1-300x92.png 300w\" sizes=\"auto, (max-width: 528px) 100vw, 528px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9e\u4e60\u7684\u65f6\u5019\u5199\u7684\u5c0f\u7ec3\u4e60\u7a0b\u5e8f\uff1a VMSTAT.h \u6b64\u4ee3\u7801\u7528\u4ee5\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u8fd0\u884c\u72b6\u6001\u4fe1\u606f pshm.h \u5b9a\u4e49\u5171\u4eab\u5185\u5b58\u6570\u636e\u7ed3\u6784\u7684\u7ed3\u6784\u4f53\u5b9a\u4e49\u4e3b\u9898ID shmClient.c&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"http:\/\/www.wangkaixuan.tech\/?p=398\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-398","post","type-post","status-publish","format-standard","hentry","category-06-03-play-ground"],"_links":{"self":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=398"}],"version-history":[{"count":0,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/398\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=398"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}