cpu調(diào)度及內(nèi)存分配算法
為珠海等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及珠海網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、成都網(wǎng)站建設(shè)、珠海網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
寫(xiě)的一個(gè)操作系統(tǒng)的實(shí)驗(yàn),篇幅原因,只給了內(nèi)存分配和回收代碼.
/**
單 位:中南大學(xué)軟件學(xué)院0501班
文 件:CpuScheduler.java
項(xiàng) 目:操作系統(tǒng)cpu調(diào)度算法模擬程序
作 者:劉欣
創(chuàng)建時(shí)間: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("無(wú)內(nèi)存可釋放");
//return false;
}
last = MemTable;
now = last.next;
if (MemTable.GetPcd_name() == mem_temp.GetPcd_name()){//如果第一個(gè)就是要釋放的分區(qū);
MemTable.Setmem_state(0);
MemTable.SetPcd_name(null);
if(now != null now.Getmem_state() == 0 ){//如果后鄰接分區(qū)也是空閑的;
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){//在鏈表中尋找要釋放的分區(qū);
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){//查看后面相鄰結(jié)點(diǎn)是否有空閑;
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){//查看前面相鄰結(jié)點(diǎn)是否有空閑;
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獲取所有系統(tǒng)信息(CPU、內(nèi)存、進(jìn)程等)的代碼:
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系統(tǒng)信息(CPU,內(nèi)存,文件系統(tǒng))
* @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());
}
//獲取內(nèi)存使用率
public static String getMemery(){
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
// 總的物理內(nèi)存+虛擬內(nèi)存
long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();
// 剩余的物理內(nèi)存
long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
Double compare=(Double)(1-freePhysicalMemorySize*1.0/totalvirtualMemory)*100;
String str="內(nèi)存已使用:"+compare.intValue()+"%";
return str;
}
//獲取文件系統(tǒng)使用率
public static ListString getDisk() {
// 操作系統(tǒng)
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+":盤(pán) 已使用 "+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";
// 取進(jìn)程信息
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相關(guān)信息
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;
}
// 字段出現(xiàn)順序: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對(duì)漢字處理存在問(wèn)題(把一個(gè)漢字視為一個(gè)字節(jié)),因此在 包含漢字的字符串時(shí)存在隱患,現(xiàn)調(diào)整如下:
* @param src 要截取的字符串
* @param start_idx 開(kāi)始坐標(biāo)(包括該坐標(biāo))
* @param end_idx 截止坐標(biāo)(包括該坐標(biāo))
* @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溫度的數(shù)值由主板驅(qū)動(dòng)供給windows,目前的現(xiàn)狀是許多PC兼容機(jī)不提供或沒(méi)安裝“專用的主板驅(qū)動(dòng)”(比如大量的ghost集成裝機(jī)),連win都沒(méi)法獲知溫度,更別提win之上的Java。
在專用主板驅(qū)動(dòng)安裝正常的win-java系統(tǒng)下可以:
import java.util.Scanner;
public class Test {
static public void main(String 土數(shù)[]) {
try {
final String cmd="wmic /namespace:\\\\root\\WMI path MSAcpi_ThermalZoneTemperature get CurrentTemperature";
Process p = Runtime.getRuntime().exec(cmd);
p.getOutputStream().close();//這句不寫(xiě)就不執(zhí)行
Scanner sc=new Scanner(p.getInputStream());
sc.next();
float t=(sc.nextInt()-2732f)/10f;
sc.close();
System.out.println("當(dāng)前CPU溫度:"+t);
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
}
==
當(dāng)前CPU溫度:41.0
linux下同樣依賴“主板驅(qū)動(dòng)”, 數(shù)據(jù)提供給/dev/sensor