Linux 下监控程序 -- 共享内存通讯,消息订阅发布
2014-03-11 15:06
567 查看
实习的时候写的小练习程序:
VMSTAT.h
此代码用以从服务器获取运行状态信息
pshm.h
定义共享内存数据结构的结构体
定义主题ID
shmClient.cpp
shmServer.cpp
服务器运行截图:
客户端运行截图:
VMSTAT.h
此代码用以从服务器获取运行状态信息
class VMSTAT{ public: double stat[20]; /* * Proc * * 0:r: The number of processes waiting for run time. * 1:b: The number of processes in uninterruptible sleep. * * Memory * * 2:swpd: the amount of virtual memory used. * 3:free: the amount of idle memory. * 4:buff: the amount of memory used as buffers. * 5:cache: the amount of memory used as cache. * * Swap * * 6:si: Amount of memory swapped in from disk (/s). * 7:Amount of memory swapped to disk (/s). * * IO * * 8:bi: Blocks received from a block device (blocks/s). * 9:bo: Blocks sent to a block device (blocks/s). * * System * * 10:in: The number of interrupts per second, including the clock. * 11:cs: The number of context switches per second. * * CPU * * 12:us: Time spent running non-kernel code. (user time, including nice time) * 13:sy: Time spent running kernel code. (system time) * 14:id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time. * 15:wa: Time spent waiting for IO. Prior to Linux 2.5.41, included in idle. * 16:st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown. * * Add * * 17:Total Memory * 18:Used Memory * 19:CPU Usage */ void update() { //init array FILE* file = popen("vmstat","r"); char buf[300]; fgets (buf , 300 , file); fgets (buf , 300 , file); fgets (buf , 300 , file); int index=0; for(int i = 0;i<300;i++) { while(buf[i]<'0'||buf[i]>'9') i++; int tmp = 0; while(buf[i]>='0'&&buf[i]<='9') { tmp = tmp*10 + (buf[i]-'0'); i++; } stat[index++] = tmp*1.0; if(index == 17) break; } pclose(file); //total memory file = popen("cat /proc/meminfo","r"); double total=0; fgets (buf , 300 , file); char str[7]; for(int i=17;i<24;i++) str[i-17]=buf[i]; for(int i=0;i<7;i++) { total =total*10+(str[i]==' '?0:(str[i]-'0')); } //calculate all stat[17]=total; stat[18]=total-stat[3]; stat[19]=100-stat[14]; pclose(file); } };
pshm.h
定义共享内存数据结构的结构体
定义主题ID
typedef struct tagMyStat { int attche_count; int total; int avaliable; int used; int cpu_useage; } MyStat; #define CENTOS_PER (4499) //主题CENTOS_PER 由服务器发布 客户端进行订阅 服务器可发布多个主题,而客户端可以选择其中若干个感兴趣的进行订阅
shmClient.cpp
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <unistd.h> #include <sys/shm.h> #include "VMSTAT.h" #include "pshm.h" int main() { void *shm = NULL; MyStat *shared = NULL; int shmid = shmget((key_t)CENTOS_PER,sizeof(MyStat),IPC_CREAT); if(shmid == -1) { printf("%s\n","shmget failed!"); return 0; } shm = shmat(shmid, (void*)0, 0); if(shm == (void*)-1) { printf("%s\n","shmat failed!"); } printf("Memory attached at %lx\n", (long)shm); shared = (MyStat*)shm; shared->attche_count ++; time_t rawtime; struct tm * t; while(1) { time ( &rawtime ); t = localtime ( &rawtime ); printf("Client@%d:%d:%d :Total: %d MB Aviliable: %d MB Used: %d MB CPU: %d %\n",t->tm_hour,t->tm_min,t->tm_sec,shared->total,shared->avaliable,shared->used,shared->cpu_useage); sleep(1); } shared->attche_count --; if(shmdt(shm) == -1) { printf("%s\n","shmdt failed!"); } if(shmctl(shmid, IPC_RMID, 0) == -1) { printf("%s\n","shmctl(IPC_RMID) failed!"); } sleep(2); return 0; }
shmServer.cpp
#include <stdio.h> #include <time.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <sys/shm.h> #include "VMSTAT.h" #include "pshm.h" int main() { void *shm = NULL; MyStat *shared = NULL; int shmid = shmget((key_t)CENTOS_PER,sizeof(MyStat),0666|IPC_CREAT); if(shmid == -1) { printf("%s\n","shmget failed!"); return 0; } shm = shmat(shmid, (void*)0, 0); if(shm == (void*)-1) { printf("%s\n","shmat failed!"); } printf("Memory attached at %lx\n", (long)shm); shared = (MyStat*)shm; memset(shared, 0, sizeof(MyStat)); shared->attche_count=0; VMSTAT s; time_t rawtime; struct tm * t; while(1) { s.update(); shared->total = int(s.stat[17]/1000); shared->avaliable = int(s.stat[3]/1000); shared->used = int(s.stat[18]/1000); shared->cpu_useage = int(s.stat[19]); time ( &rawtime ); t = localtime ( &rawtime ); printf("Server@%2d:%2d:%2d (%d):Total: %d MB Aviliable: %d MB Used: %d MB CPU: %d %\n",t->tm_hour,t->tm_min,t->tm_sec,shared->attche_count,shared->total,shared->avaliable,shared->used,shared->cpu_useage); sleep(1); } if(shmdt(shm) == -1) { printf("%s\n","shmdt failed!"); } if(shmctl(shmid, IPC_RMID, 0) == -1) { printf("%s\n","shmctl(IPC_RMID) failed!"); } sleep(2); return 0; }
服务器运行截图:
客户端运行截图:
相关文章推荐
- linux进程通讯-共享内存
- Linux进程间通信--信号,管道,消息队列,信号量,共享内存,socket
- linux进程通讯-共享内存
- Linux进程间通信--进程,信号,管道,消息队列,信号量,共享内存
- 共享内存在两个不相干的程序之间的通讯
- linux 共享内存消息队列使用
- Linux程序设计笔记(第14章 信号量、共享内存和消息队列)
- linux两个程序通过共享内存通信的一个简单例子
- linux 管道、消息队列、共享内存的对比
- Linux进程同步与通讯:共享内存和信号量的使用
- LINUX 64位下运行32位程序加载和读取大数据段共享内存
- linux下使用hiredis异步API实现sub/pub消息订阅和发布的功能 标签: hiredishiredis异步APIhiredis事件处理redis消息订阅发布redis c接口 2016-
- ipcs命令_Linux ipcs 命令用法详解:分析消息队列共享内存和信号量
- Linux Programing -- ch14-- 信号量、共享内存、消息队列
- linux 消息队列 和 内存共享的简短说明
- linux基础编程:进程通信之System V IPC:消息队列,信号量,共享内存
- php 实现redis发布订阅消息及时通讯
- 基于OpenDDS开发发布订阅HelloMsg程序的过程(Linux)
- Linux 进程间通讯之共享内存方式
- linux基础编程:进程通信之System V IPC:消息队列,信号量,共享内存