0906130204-廖浩伟-操作系统安全课程设计报告(3)

2019-08-31 14:48

操作系统安全课程设计报告-0906130204-廖浩伟

import java.io.LineNumberReader; import java.io.OutputStream; import java.lang.management.ManagementFactory;

import com.sun.management.OperatingSystemMXBean;

public class MonitorServiceImpl implements IMonitorService { public static ProcessStruct[] proce = new ProcessStruct[200]; public static ServiceStruct[] servi = new ServiceStruct[200]; public static int processCount = 0; public static int threadCount = 0; public static int serviceCount = 0; public static int handleCount = 0;

public MonitorInfoBean getMonitorInfoBean() throws Exception {

int kb = 1024;

OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory

.getOperatingSystemMXBean();

// 操作系统

String osName = System.getProperty(\);

String osVersion = System.getProperty(\); // 总的物理内存

long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / (kb * kb);

// 剩余的物理内存

long freePhysicalMemorySize =

osmxb.getFreePhysicalMemorySize() / (kb * kb); // 已使用的物理内存

long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / (kb * kb);

if (osName.toLowerCase().startsWith(\)) { this.getProcessForWindows(); }

// 构造返回对象

MonitorInfoBean infoBean = new MonitorInfoBean();

infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize); infoBean.setOsName(osName +\+osVersion); infoBean.setTotalMemorySize(totalMemorySize);

- 9 -

操作系统安全课程设计报告-0906130204-廖浩伟

infoBean.setUsedMemory(usedMemory); return infoBean; }

public void getProcessForWindows() { try {

String procCmd = System.getenv(\)

+ \Caption,CSName,Priority,ProcessId,ThreadCount\; // 取进程信息

readProcess(Runtime.getRuntime().exec(procCmd));

} catch (Exception ex) { ex.printStackTrace(); } }

public void getServiceForWindows() { try {

String servCmd = System.getenv(\)

+ \Name,ProcessId,DisplayName,State,StartName\; // 取服务信息

readService(Runtime.getRuntime().exec(servCmd));

} catch (Exception ex) { ex.printStackTrace(); } }

public void getHandleCount(){ try {

String handCmd = System.getenv(\)

+ \Name,HandleCount\; // 取句柄信息

readHandle(Runtime.getRuntime().exec(handCmd)); } catch (Exception ex) { ex.printStackTrace(); } }

public void readHandle(final Process hand){ // 总句柄数

- 10 -

操作系统安全课程设计报告-0906130204-廖浩伟

int i = 0; try {

hand.getOutputStream().close();

InputStreamReader ir = new

InputStreamReader(hand.getInputStream());

LineNumberReader input = new LineNumberReader(ir); String line = input.readLine();

int cn = line.indexOf(\);

int ch = line.indexOf(\);

while ((line = input.readLine()) != null) { if (line.length() < cn) { continue; }

int handle = Integer.parseInt(line.substring(ch, cn-1).trim());

i += handle; }

} catch (Exception ex) { ex.printStackTrace(); } finally { try {

hand.getInputStream().close(); } catch (Exception e) { e.printStackTrace(); } }

this.handleCount = i; }

public void readProcess(final Process proc) { // 总进程数 int i = 0; int t = 0; try {

proc.getOutputStream().close();

InputStreamReader ir = new

InputStreamReader(proc.getInputStream());

LineNumberReader input = new LineNumberReader(ir); String line = input.readLine();

- 11 -

操作系统安全课程设计报告-0906130204-廖浩伟

int capidx = line.indexOf(\); int cmdidx = line.indexOf(\); int prioidx = line.indexOf(\); int pid = line.indexOf(\); int tc = line.indexOf(\);

while ((line = input.readLine()) != null) { if (line.length() < tc) { continue; }

// 字段出现顺序:

Caption,CommandLine,Priority,ProcessId,ThreadCount

String caption = line.substring(capidx, cmdidx - 1).trim();

String caname = line.substring(cmdidx, prioidx - 1).trim();

String priority = line.substring(prioidx, pid - 1).trim();

String processId = line.substring(pid, tc - 1).trim();

String ThreadCount = line.substring(tc, line.length() - 1)

.trim();

proce[i] = new ProcessStruct(caption, caname, priority,

processId, ThreadCount);

int th = Integer.parseInt(ThreadCount); i = i + 1; t += th;

}

} catch (Exception ex) { ex.printStackTrace(); } finally { try {

proc.getInputStream().close(); } catch (Exception e) { e.printStackTrace(); } }

this.threadCount = t;

- 12 -

操作系统安全课程设计报告-0906130204-廖浩伟

this.processCount = i;

}

public void readService(final Process serv) { // 总服务数 int i = 0; try {

serv.getOutputStream().close();

InputStreamReader ir = new

InputStreamReader(serv.getInputStream());

LineNumberReader input = new LineNumberReader(ir); String line = input.readLine();

int capidx = line.indexOf(\); int cmdidx = line.indexOf(\);

int prioidx = line.indexOf(\); int pid = line.indexOf(\); int tc = line.indexOf(\);

while ((line = input.readLine()) != null) { if (line.length() < tc) { continue; }

String displayname = line.substring(capidx, cmdidx - 1).trim();

String name = line.substring(cmdidx, prioidx - 1).trim();

String processid = line.substring(prioidx, pid - 1).trim();

String startname = line.substring(pid, tc - 1).trim();

String state = line.substring(tc, line.length() - 1) .trim();

servi[i] = new ServiceStruct(name, processid, displayname,

state, startname); i = i + 1;

}

} catch (Exception ex) {

- 13 -


0906130204-廖浩伟-操作系统安全课程设计报告(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:在线考试系统毕业论文

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: