大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
cpu调度及内存分配算法
为珠海等地区用户提供了全套网页设计制作服务,及珠海网站建设行业解决方案。主营业务为成都做网站、成都网站建设、珠海网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
写的一个操作系统的实验,篇幅原因,只给了内存分配和回收代码.
/**
单 位:中南大学软件学院0501班
文 件:CpuScheduler.java
项 目:操作系统cpu调度算法模拟程序
作 者:刘欣
创建时间:2007年5月14日
**/
public static void InitMem(){
MemTable = new memory();
MemTable.Setmem_beg(0);
MemTable.Setmem_size(100);
MemTable.Setmem_state(0);
}
public boolean InitMemory(Pcb temp){
memory mem_temp,last,now;
last = MemTable;
mem_temp = new memory(temp.GetName(),temp.Getmem_size());
boolean flag = true;
if(mem_temp.Getmem_size() 100 ){
c.tcpuinfo.setText(mem_temp.GetPcd_name()+"is too big");
flag = false;
}
if(MemTable == null){
return flag;
}
if(mem_temp.Getmem_size() + nowsize 100){
c.tcpuinfo.setText("memory has been full please wait");
flag = false;
}
if(MemTable.Getmem_state() == 0){// if the first is empty;
if(MemTable.Getmem_size() mem_temp.Getmem_size()){
mem_temp.next = MemTable;
mem_temp.Setmem_beg( last.Getmem_beg() );
mem_temp.Setmem_state(1);
MemTable.Setmem_beg(mem_temp.Getmem_beg() + mem_temp.Getmem_size());
MemTable.Setmem_size( MemTable.Getmem_size()-mem_temp.Getmem_size() );
MemTable = mem_temp;
nowsize += mem_temp.Getmem_size();
return flag;
}
if (MemTable.Getmem_size() == mem_temp.Getmem_size()){
// MemTable.SetPcd_name(mem_temp.GetPcd_name());
mem_temp = MemTable;
mem_temp.Setmem_state(1);
mem_temp.next = MemTable;
nowsize += mem_temp.Getmem_size();
return flag;
}
}
// begin obtain the other;
if(last != null){
now = last.next;
while(now != null){// search the teble for sutible memory;
if(now.Getmem_state() == 0){// if the first is empty;
if(now.Getmem_size() mem_temp.Getmem_size()){
mem_temp.next = now;
mem_temp.Setmem_beg( now.Getmem_beg() );
mem_temp.Setmem_state(1);
now.Setmem_beg(mem_temp.Getmem_beg() + mem_temp.Getmem_size());
now.Setmem_size( now.Getmem_size()-mem_temp.Getmem_size() );
last.next = mem_temp;
nowsize += mem_temp.Getmem_size();
return flag;
}
else if (now.Getmem_size() == mem_temp.Getmem_size()){
now.SetPcd_name(mem_temp.GetPcd_name());
mem_temp = now;
mem_temp.Setmem_state(1);
last.next = mem_temp;
nowsize += mem_temp.Getmem_size();
return flag;
}
}
last = now;
//if(last != null){
now = now.next;
//}
}
}
return flag;
}
public void ReleaseMem(Pcb Temp){
memory mem_temp,last,now;
mem_temp = new memory(Temp.GetName(),Temp.Getmem_size());
if(MemTable == null){
c.tcpuinfo.setText("无内存可释放");
//return false;
}
last = MemTable;
now = last.next;
if (MemTable.GetPcd_name() == mem_temp.GetPcd_name()){//如果第一个就是要释放的分区;
MemTable.Setmem_state(0);
MemTable.SetPcd_name(null);
if(now != null now.Getmem_state() == 0 ){//如果后邻接分区也是空闲的;
MemTable.Setmem_size(MemTable.Getmem_size() + now.Getmem_size());
MemTable.SetPcd_name(null);
nowsize -= mem_temp.Getmem_size();
MemTable.next = null;
// return true;
}
}
while (now != null){//在链表中寻找要释放的分区;
if(now.GetPcd_name() == mem_temp.GetPcd_name()){//找到;
nowsize -= mem_temp.Getmem_size();
now.Setmem_state(0);
if(now.next != null now.next.Getmem_state() == 0){//查看后面相邻结点是否有空闲;
last.next = now.next;
now.next.Setmem_beg(now.Getmem_beg());
now.next.Setmem_size(now.Getmem_size() + now.next.Getmem_size());
now = last.next;
now.SetPcd_name(null);
}
if(last.Getmem_state() == 0){//查看前面相邻结点是否有空闲;
last.next = now.next;
last.Setmem_size(last.Getmem_size() + now.Getmem_size());
last.SetPcd_name(null);
now = last.next;
// now.SetPcd_name(null);
}
}
last = now;
now = now.next;
}
}
java获取所有系统信息(CPU、内存、进程等)的代码:
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;
import mytools.com.sun.management.OperatingSystemMXBean;
import mytools.java.io.File;
import mytools.java.lang.management.ManagementFactory;
/**
* 获取windows系统信息(CPU,内存,文件系统)
* @author libing
*
*/
public class WindowsInfoUtil {
private static final int CPUTIME = 500;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;
public static void main(String[] args) {
System.out.println(getCpuRatioForWindows());
System.out.println(getMemery());
System.out.println(getDisk());
}
//获取内存使用率
public static String getMemery(){
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
// 总的物理内存+虚拟内存
long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();
// 剩余的物理内存
long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
Double compare=(Double)(1-freePhysicalMemorySize*1.0/totalvirtualMemory)*100;
String str="内存已使用:"+compare.intValue()+"%";
return str;
}
//获取文件系统使用率
public static ListString getDisk() {
// 操作系统
ListString list=new ArrayListString();
for (char c = 'A'; c = 'Z'; c++) {
String dirName = c + ":/";
File win = new File(dirName);
if(win.exists()){
long total=(long)win.getTotalSpace();
long free=(long)win.getFreeSpace();
Double compare=(Double)(1-free*1.0/total)*100;
String str=c+":盘 已使用 "+compare.intValue()+"%";
list.add(str);
}
}
return list;
}
//获得cpu使用率
public static String getCpuRatioForWindows() {
try {
String procCmd = System.getenv("windir") + "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
// 取进程信息
long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
Thread.sleep(CPUTIME);
long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
if (c0 != null c1 != null) {
long idletime = c1[0] - c0[0];
long busytime = c1[1] - c0[1];
return "CPU使用率:"+Double.valueOf(PERCENT * (busytime)*1.0 / (busytime + idletime)).intValue()+"%";
} else {
return "CPU使用率:"+0+"%";
}
} catch (Exception ex) {
ex.printStackTrace();
return "CPU使用率:"+0+"%";
}
}
//读取cpu相关信息
private static long[] readCpu(final Process proc) {
long[] retn = new long[2];
try {
proc.getOutputStream().close();
InputStreamReader ir = new InputStreamReader(proc.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line = input.readLine();
if (line == null || line.length() FAULTLENGTH) {
return null;
}
int capidx = line.indexOf("Caption");
int cmdidx = line.indexOf("CommandLine");
int rocidx = line.indexOf("ReadOperationCount");
int umtidx = line.indexOf("UserModeTime");
int kmtidx = line.indexOf("KernelModeTime");
int wocidx = line.indexOf("WriteOperationCount");
long idletime = 0;
long kneltime = 0;
long usertime = 0;
while ((line = input.readLine()) != null) {
if (line.length() wocidx) {
continue;
}
// 字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount,
// ThreadCount,UserModeTime,WriteOperation
String caption =substring(line, capidx, cmdidx - 1).trim();
String cmd = substring(line, cmdidx, kmtidx - 1).trim();
if (cmd.indexOf("wmic.exe") = 0) {
continue;
}
String s1 = substring(line, kmtidx, rocidx - 1).trim();
String s2 = substring(line, umtidx, wocidx - 1).trim();
if (caption.equals("System Idle Process") || caption.equals("System")) {
if (s1.length() 0)
idletime += Long.valueOf(s1).longValue();
if (s2.length() 0)
idletime += Long.valueOf(s2).longValue();
continue;
}
if (s1.length() 0)
kneltime += Long.valueOf(s1).longValue();
if (s2.length() 0)
usertime += Long.valueOf(s2).longValue();
}
retn[0] = idletime;
retn[1] = kneltime + usertime;
return retn;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
proc.getInputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 由于String.subString对汉字处理存在问题(把一个汉字视为一个字节),因此在 包含汉字的字符串时存在隐患,现调整如下:
* @param src 要截取的字符串
* @param start_idx 开始坐标(包括该坐标)
* @param end_idx 截止坐标(包括该坐标)
* @return
*/
private static String substring(String src, int start_idx, int end_idx) {
byte[] b = src.getBytes();
String tgt = "";
for (int i = start_idx; i = end_idx; i++) {
tgt += (char) b[i];
}
return tgt;
}
}
研究了一下
cpu温度的数值由主板驱动供给windows,目前的现状是许多PC兼容机不提供或没安装“专用的主板驱动”(比如大量的ghost集成装机),连win都没法获知温度,更别提win之上的Java。
在专用主板驱动安装正常的win-java系统下可以:
import java.util.Scanner;
public class Test {
static public void main(String 土数[]) {
try {
final String cmd="wmic /namespace:\\\\root\\WMI path MSAcpi_ThermalZoneTemperature get CurrentTemperature";
Process p = Runtime.getRuntime().exec(cmd);
p.getOutputStream().close();//这句不写就不执行
Scanner sc=new Scanner(p.getInputStream());
sc.next();
float t=(sc.nextInt()-2732f)/10f;
sc.close();
System.out.println("当前CPU温度:"+t);
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
}
==
当前CPU温度:41.0
linux下同样依赖“主板驱动”, 数据提供给/dev/sensor