操作系统课程设计 设备管理实现 源代码
操作系统课程设计代码

SuperBlock FS; //一个全局文件系统的变量
CurrentStatus CS; //当前系统状态
FSElement *base; //文件元素的根
bool InitFileSys();
/*
* 函数介绍:寻找第一个空白的文件块ID
* 输入参数:无
if (c == 0x7F)
{
return i * 8; //一个字节左边第一位为0,表示该区域未使用
}
c = FS.bm.BMStart[i] | 0xBF;
if (c == 0xBF)
{
return i * 8 + 1;
}
c = FS.bm.BMStart[i] | 0xDF;
typedef struct
{
unsigned Index; //文件元素索引编号
char FileName[NAME_LEN]; //文件元素名
char ParentName[NAME_LEN]; //父节点名
unsigned FileBlockId; //文件元素所在物理块编号
if (c == 0xF7)
{
return i * 8 + 4;
}
c = FS.bm.BMStart[i] | 0xFB;
if (c == 0xFB)
{
return i * 8 + 5;
}
c = FS.bm.BMStart[i] | 0xFD;
char *FileBlockAddr; //文件块地址
struct fb *next; //下一个文件块的地址
设备管理系统 源代码及运行界面

源代码及运行界面1.model类a.Depart.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace model{public class Depart{public Depart(){ }#region modelprivate int _departid;private string _departname;private int _parentid;public int DepartID{set {_departid = value; }get { return _departid; }}public string DepartName{set { _departname = value; }get { return _departname; }}public int ParentID{set { _parentid = value; }get { return _parentid; }}#endregion model}}b.Device.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace model{public class Device{public Device(){ }#region modelprivate int _deviceid;private string _devicetypeid;private int _typeid;private int _classid;private string _devicestate;private Single _price;private int _useofyear;private Single _svp;private string _manufacture;private DateTime _stocktime;public int DeviceID{set { _deviceid = value; }get { return _deviceid; }}public string DeviceTypeID{set { _devicetypeid = value; }get { return _devicetypeid; }}public int TypeID{set { _typeid = value; }get { return _typeid; }}public int ClassID{set { _classid = value;}get { return _classid; }}public string DeviceState{set { _devicestate = value; }get { return _devicestate; }}public Single Price{set { _price = value; }get { return _price; }}public int UseOfYear{set { _useofyear = value; }get { return _useofyear; }}public Single SVP{set { _svp = value; }get { return _svp; }}public string Manufacture{set { _manufacture = value; }get { return _manufacture; } }public DateTime StockDate{set { _stocktime = value; }get { return _stocktime; }}#endregion model}}c.D eviceClass.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace model{public class DeviceClass{public DeviceClass(){ }#region modelprivate int _classid;private string _classname;private int _typeid;public int ClassID{set { _classid = value; }get { return _classid; }}public string ClassName{set { _classname = value;}get { return _classname; }}public int TypeID{set { _typeid = value; }get { return _typeid; }}#endregion model}}d.DeviceType.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace model{public class DeviceType{public DeviceType(){ }#region modelprivate int _typeid;private string _typename;public int TypeID{set { _typeid = value; }get { return _typeid; }}public string TypeName{set { _typename = value; }get { return _typename; }}#endregion model}}e.R epairInfo.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace model{public class RepairInfo{public RepairInfo(){ }#region modelprivate int _repairid;private int _deviceid;private string _repairpeople;private DateTime _beginrepairdate;private Single _repairprice;private int _repairoftime;private string _faultcause;private string _remark;public int RepairID{set { _repairid = value; }get { return _repairid; }}public int DeviceID{set { _deviceid = value; }get { return _deviceid; }}public string RepairPeople{set { _repairpeople = value; }get { return _repairpeople; }}public DateTime BeginRepairDate{set { _beginrepairdate = value; }get { return _beginrepairdate; } }public Single RepairPrice{set { _repairprice = value; }get { return _repairprice; }}public int RepairOfTime{set { _repairoftime = value; }get { return _repairoftime; }}public string FaultCause{set { _faultcause = value; }get { return _faultcause; }}public string Remark{set { _remark = value; }get { return _remark; }}#endregion model}}f.TransInfo.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace model{public class TransInfo{public TransInfo(){ }#region modelprivate int _transid;private int _deviceid;private int _departid;private string _usepeople;private DateTime _begintransdate;private int _useofmonth;private string _remark;public int TransID{set { _transid = value; }get { return _transid; }}public int DeviceID{set { _deviceid = value; }get { return _deviceid; }}public int DepartID{set { _departid = value; }get { return _departid; }}public string UsePeople{set { _usepeople = value; }get { return _usepeople; }}public DateTime BeginTransDate{set { _begintransdate = value; }get { return _begintransdate; }}public int UseOfMonth{set { _useofmonth = value; }get { return _useofmonth; }}public string Remark{set { _remark = value; }get { return _remark; }}#endregion model}}g.UserLevel.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace model{public class UserLevel{public UserLevel(){ }#region modelprivate string _levelid;private string _levelname;private string _description;public string LevelID{set { _levelid = value; }get { return _levelid; }}public string LevelName{set { _levelname = value; }get { return _levelname; }}public string Description{set { _description = value; }get { return _description; }}#endregion model}}h.Users.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace model{public class Users{public Users(){ }#region modelprivate string _userid;private string _userpwd;private string _levelid;public string UserID{set { _userid = value; }get { return _userid; }}public string UserPwd{set { _userpwd = value; }get { return _userpwd; }}public string LevelID{set { _levelid = value; }get { return _levelid; }}#endregion model}}2.DAL类a.Depart.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;using System.Data;using System.Data.SqlClient;namespace DAL{public class Depart{sqlhelp sql = new sqlhelp();public Depart(){ }#region 成员方法public ArrayList GetListByParentID(int pid){ArrayList al = new ArrayList();string s = "select * from Depart where ParentID=" + pid;DataTable dt = null;dt = sql.Execusql(s);for (int i = 0; i < dt.Rows.Count; i++){model.Depart modeldepart = new model.Depart();modeldepart.DepartID = Convert.ToInt32(dt.Rows[i]["DepartID"]);modeldepart.ParentID = Convert.ToInt32(dt.Rows[i]["ParentID"]);modeldepart.DepartName = dt.Rows[i]["DepartName"].ToString();al.Add(modeldepart);}return al;}public int Insert(model.Depart mod){int n = 0;string s = "Insert into Depart(DepartName,ParentID) values('" + mod.DepartName + "'," + mod.ParentID + ")";n = sql.Execsql(s);return n;}public ArrayList GetList(){ArrayList al = new ArrayList();string s = "select * from Depart where DepartID!=1";DataTable dt = null;dt = sql.Execusql(s);for (int i = 0; i < dt.Rows.Count; i++){model.Depart modeldepart = new model.Depart();modeldepart.DepartID = Convert.ToInt32(dt.Rows[i]["DepartID"]);modeldepart.ParentID = Convert.ToInt32(dt.Rows[i]["ParentID"]);modeldepart.DepartName = dt.Rows[i]["DepartName"].ToString();al.Add(modeldepart);}return al;}public DataTable GetDepartList(string strwhere){DataTable dt = null;string s = "select * from View2 where " + strwhere;if (strwhere == "")s = "select * from View2";dt = sql.Execusql(s);return dt;}public DataTable GetDepartByID(int id){DataTable dt = null;string s = "select * from Depart where DepartID="+id;dt = sql.Execusql(s);return dt;}public int Update(model.Depart mod){int n = 0;string s = "Update Depart Set DepartName='" + mod.DepartName + "',ParentID=" + mod.ParentID + " where DepartID=" + mod.DepartID;n = sql.Execsql(s);return n;}public int Delete(int id){int n = 0;string s = "delete from Depart where DepartID="+id;n = sql.Execsql(s);return n;}#endregion 成员方法}}b.Device.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.SqlClient;namespace DAL{public class Device{sqlhelp sql = new sqlhelp();public Device(){ }#region 成员方法public DataTable GetDeviceByID(int deviceid){string s = "select * from Device where DeviceID=" + deviceid;DataTable dt = null;dt = sql.Execusql(s);return dt;}public int Insert(model.Device modeldevice){int n = 0;string s = "Insert into Device values(" + modeldevice.DeviceID + ",'" + modeldevice.DeviceTypeID + "'," + modeldevice.TypeID + ","+modeldevice.ClassID+",'" + modeldevice.DeviceState + "'," + modeldevice.Price + "," + eOfYear+ "," + modeldevice.SVP+ ",'" + modeldevice.Manufacture+ "','"+modeldevice.StockDate+"')";n = sql.Execsql(s);return n;}public DataTable GetList(string strwhere){DataTable dt = null;string s = "Select * from View1 where " + strwhere;if (strwhere == ""){s = "select * from View1";}dt = sql.Execusql(s);return dt;}public DataTable GetDeviceList(string strwhere){DataTable dt = null;string s = "Select * from Device where " + strwhere;if (strwhere == ""){s = "select * from Device";}dt = sql.Execusql(s);return dt;}public int Delete(int id){int n = 0;string s = "delete from Device where DeviceID=" +id;n = sql.Execsql(s);return n;}public int Update(model.Device mod){int n = 0;string s = "Update Device set DeviceState='" + mod.DeviceState + "',TypeID="+mod.TypeID+",ClassID="+mod.ClassID+", DeviceTypeID='" + mod.DeviceTypeID + "',Price=" + mod.Price + ",SVP=" + mod.SVP + ",StockDate='" + mod.StockDate + "',Manufacture='" + mod.Manufacture + "',UseOfYear=" + eOfYear + " where DeviceID=" + mod.DeviceID ;n = sql.Execsql(s);return n;}#endregion 成员方法}}c.DeviceClass.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.SqlClient;namespace DAL{public class DeviceClass{sqlhelp sql = new sqlhelp();public DeviceClass(){ }#region 成员方法public DataTable GetClass(int id){string s = "select * from DeviceClass where TypeID=" + id;DataTable dt = null;dt = sql.Execusql(s);return dt;}/// <summary>/// 得到设备类别列表/// </summary>/// <returns></returns>public DataTable getDeviceClassList(){string sqlstr = "select * from DeviceClass";DataTable dt = sql.Execusql(sqlstr);return dt;}/// <summary>/// 条件查询/// </summary>/// <param name="strClassId"></param>/// <param name="strClassName"></param>/// <param name="strTypeID"></param>/// <returns></returns>public DataTable findDeviceClassList(string strClassId,string strClassName,string strTypeID){if (strClassId.Trim().Length == 0) {strClassId = "%";}if (strClassName.Trim().Length == 0){strClassName = "%";}if (strTypeID.Trim().Length == 0){strTypeID = "%";}string sqlstr = "select * from DeviceClass where ClassId like '%" + strClassId.Trim() + "%' and ClassName like '%" + strClassName.Trim() + "%' and TypeID like '%" + strTypeID.Trim() + "%'";DataTable dt = sql.Execusql(sqlstr);return dt;}/// <summary>/// 得到一条设备类别记录/// </summary>/// <param name="id"></param>/// <returns></returns>public model.DeviceClass getDeviceClassModel(int id){model.DeviceClass model = new model.DeviceClass();string sqlstr = "select * from DeviceClass where ClassID=" + id;DataTable dt = sql.Execusql(sqlstr);if (dt.Rows.Count > 0){if (dt.Rows[0]["ClassID"].ToString() != ""){model.ClassID = int.Parse(dt.Rows[0]["ClassID"].ToString());}model.ClassName = dt.Rows[0]["ClassName"].ToString();return model;}else{return null;}}/// <summary>/// 修改设备类别记录/// </summary>/// <param name="model"></param>/// <returns></returns>public int updateDeviceClass(model.DeviceClass model){int n = 0;string sqlstr = "update DeviceClass set ClassName='" + model.ClassName + "',TypeID="+model.TypeID+" where ClassID=" + model.ClassID;n = sql.Execsql(sqlstr);return n;}/// <summary>/// 删除一条设备类别记录/// </summary>/// <param name="id"></param>/// <returns></returns>public int deleteDeviceClass(int id){int n = 0;string sqlstr = "delete from DeviceClass where ClassID=" + id;n = sql.Execsql(sqlstr);return n;}/// <summary>/// 获得类别最大编号/// </summary>/// <returns></returns>public int getMaxId() {int n = 0;string sqlstr = "select max(ClassID) from DeviceClass";DataTable dt = sql.Execusql(sqlstr);n = Convert.ToInt32(dt.Rows[0][0]);return n;}/// <summary>/// 增加一个设备类别记录/// </summary>/// <param name="model"></param>/// <returns></returns>public int addDeviceClass(model.DeviceClass model) {int n = 0;int id = getMaxId() + 1;string sqlstr = "insert into DeviceClass(ClassID,ClassName,TypeID) values(" + id + ",'" + model.ClassName + "'," + model.TypeID + ")";n = sql.Execsql(sqlstr);return n;}#endregion 成员方法}}d.DeviceType.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.SqlClient;namespace DAL{public class DeviceType{sqlhelp sql = new sqlhelp();public DeviceType(){ }#region 成员方法public DataTable GetTypeList(){DataTable dt = null;string s = "select * from DeviceType";dt = sql.Execusql(s);return dt;}/// <summary>/// 得到设备类型列表/// </summary>/// <returns></returns>public DataTable getDeviceTypeList(){string sqlstr = "select * from DeviceType";DataTable dt=sql.Execusql(sqlstr);return dt;}/// <summary>/// 得到一条设备记录/// </summary>/// <param name="id"></param>/// <returns></returns>public model.DeviceType getDeviceTypeModel(int id) {model.DeviceType model = new model.DeviceType();string sqlstr = "select * from DeviceType where TypeID="+id;DataTable dt = sql.Execusql(sqlstr);if (dt.Rows.Count > 0){if (dt.Rows[0]["TypeID"].ToString() != ""){model.TypeID = int.Parse(dt.Rows[0]["TypeID"].ToString());}model.TypeName = dt.Rows[0]["TypeName"].ToString();return model;}else{return null;}}/// <summary>/// 修改设备类型记录/// </summary>/// <param name="model"></param>/// <returns></returns>public int updateDeviceType(model.DeviceType model) {int n = 0;string sqlstr = "update DeviceType set TypeName='" + model.TypeName + "' where TypeID="+model.TypeID;n=sql.Execsql(sqlstr);return n;}/// <summary>/// 插入一条设备类型记录/// </summary>/// <param name="model"></param>/// <returns></returns>public int addDeviceType(model.DeviceType model) {int n = 0;string sqlstr = "insert into DeviceType(TypeName) values('" + model.TypeName + "')";n = sql.Execsql(sqlstr);return n;}/// <summary>/// 删除一条设备类型记录/// </summary>/// <param name="id"></param>/// <returns></returns>public int deleteDeviceType(int id) {int n = 0;string sqlstr = "delete from DeviceType where TypeID=" + id;n = sql.Execsql(sqlstr);return n;}/// <summary>/// 条件查询/// </summary>/// <param name="strUserID"></param>/// <param name="strLevelID"></param>/// <returns></returns>public DataTable findDeviceTypeList(string strTypeID, String strTypeName){string sqlstr = "";if (strTypeID.Trim().Length == 0){if (strTypeName.Trim().Length == 0){strTypeName = "%";}sqlstr = "select * from DeviceType where TypeName like '%" + strTypeName.Trim() + "%'";}else{if (strTypeName.Trim().Length == 0){strTypeName = "%";}sqlstr = "select * from DeviceType where TypeID=" +Convert.ToInt32(strTypeID.Trim()) + " and TypeName like '%" + strTypeName.Trim() + "%'";}DataTable dt = sql.Execusql(sqlstr);return dt;}#endregion 成员方法}}e.R epairInfo.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.SqlClient;namespace DAL{public class RepairInfo{sqlhelp sql = new sqlhelp();public RepairInfo(){ }#region 成员方法public int Insert(model.RepairInfo mod){int n = 0,m=0 ;string s = "Insert into RepairInfo(DeviceID,RepairPeople,BeginRepairDate,RepairPrice,RepairOfTime,FaultCause,Remark) values("+mod.DeviceID+",'"+mod.RepairPeople+"','"+mod.BeginRepairDate+"',"+mod.RepairPrice+","+mo d.RepairOfTime+",'"+mod.FaultCause+"','"+mod.Remark+"')";n = sql.Execsql(s);if (mod.Remark == "正在维修"){s = "Update Device set DeviceState='故障'where DeviceID=" + mod.DeviceID;m = sql.Execsql(s);}else{s = "Update Device set DeviceState='空闲' where DeviceID=" + mod.DeviceID;m = sql.Execsql(s);}if (m == 1 && n == 1){ n = 1; }else{ n = 0; }return n;}public DataTable GetList(string strwhere){DataTable dt = null;string s = "Select * from RepairInfo where " + strwhere;if (strwhere == ""){s = "select * from RepairInfo";}dt = sql.Execusql(s);return dt;}public DataTable GetRepairInfoByID(int id){DataTable dt = null;string s = "select * from RepairInfo where RepairID=" + id;dt = sql.Execusql(s);return dt;}public int Update(model.RepairInfo mod){int n = 0,m=0;string s = "update RepairInfo set DeviceID=" + mod.DeviceID + ",RepairPeople='" + mod.RepairPeople + "',BeginRepairDate='" + mod.BeginRepairDate + "',Repairprice=" + mod.RepairPrice + ",RepairOfTime=" + mod.RepairOfTime + ",FaultCause='" + mod.FaultCause + "',Remark='" + mod.Remark + "' where RepairID=" + mod.RepairID + "";n = sql.Execsql(s);if (mod.Remark == "正在维修"){s = "Update Device set DeviceState='故障'where DeviceID=" + mod.DeviceID;m = sql.Execsql(s);}else{s = "Update Device set DeviceState='空闲' where DeviceID=" + mod.DeviceID;m = sql.Execsql(s);}if (m == 1 && n == 1){ n = 1; }else{ n = 0; }return n;}public int Delete(int id){int n = 0;string s = "delete from RepairInfo where RepairID=" + id;n = sql.Execsql(s);return n;}#endregion 成员方法}}f.TransInfo.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;using System.Data;using System.Data.SqlClient;namespace DAL{public class TransInfo{sqlhelp sql = new sqlhelp();public TransInfo(){ }#region 成员方法public int Insert(model.TransInfo mod){int n = 0,m=0;string s = "Insert into TransInfo(DeviceID,DepartID,UsePeople,BeginTransDate,UseOfMonth,Remark)values("+mod.DeviceID+","+mod.DepartID+",'"+ePeople+"','"+mod.BeginTransDate+"',"+e OfMonth+",'"+mod.Remark+"')";n = sql.Execsql(s);if(mod.Remark=="未归还")s = "Update Device set DeviceState='忙碌' where DeviceID="+mod.DeviceID;m = sql.Execsql(s);if (m == 1 && n == 1)n = 1;elsen = 0;return n;}public DataTable GetList(string strwhere){DataTable dt = null;string s = "select * from View3 where" + strwhere;if (strwhere == ""){s = "select * from View3";}dt = sql.Execusql(s);return dt;}public DataTable GetTransByID(int id){DataTable dt = null;string s = "select * from TransInfo where TransID=" + id;dt = sql.Execusql(s);return dt;}public int Update(model.TransInfo mod){int n = 0,m=0;string s = "update TransInfo set DepartID="+mod.DepartID+",UsePeople='"+ePeople+"',BeginTransDate='"+mod.BeginTransDate+" ',UseOfMonth="+eOfMonth+",Remark='"+mod.Remark+"' where TransID="+mod.TransID;n = sql.Execsql(s);if (mod.Remark == "未归还")s = "Update Device set DeviceState='忙碌' where DeviceID=" + mod.DeviceID;elses = "Update Device set DeviceState='空闲' where DeviceID=" + mod.DeviceID;m = sql.Execsql(s);if (m == 1 && n == 1)n = 1;elsen = 0;return n;}public int Delete(int id){int n = 0;string s = "delete from TransInfo where TransID=" + id;n = sql.Execsql(s);return n;}#endregion 成员方法}}g.UserLevel.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;namespace DAL{public class UserLevel{sqlhelp sql = new sqlhelp();public UserLevel(){ }#region 成员方法/// <summary>/// 得到用户权限列表/// </summary>/// <returns></returns>public DataTable getUsersLevelList(){string sqlstr = "select * from UserLevel";DataTable dt = sql.Execusql(sqlstr);return dt;}#endregion 成员方法}}h.Users.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;namespace DAL{public class Users{sqlhelp sql = new sqlhelp();public Users(){ }#region 成员方法/// <summary>/// 得到用户列表/// </summary>/// <returns></returns>public DataTable getUsersList(){string sqlstr = "select * from Users";DataTable dt = sql.Execusql(sqlstr);return dt;}/// <summary>/// 或得一条用户记录/// </summary>/// <param name="id"></param>/// <returns></returns>public ers getUsersModel(string id){ers model = new ers();string sqlstr = "select * from Users where UserID='" + id+"'";DataTable dt = sql.Execusql(sqlstr);if (dt.Rows.Count > 0){erID = dt.Rows[0]["UserID"].ToString();erPwd = dt.Rows[0]["UserPwd"].ToString();model.LevelID = dt.Rows[0]["LevelID"].ToString();return model;}else{return null;}}/// <summary>/// 获得下一个用户编号/// </summary>/// <returns></returns>public string getNextUsersId(){string idstr = "";string sqlstr = "select max(UserID) from Users";DataTable dt = sql.Execusql(sqlstr);int id1 = Convert.ToInt32(dt.Rows[0][0])+1;idstr = id1+"";return idstr;}/// <summary>/// 插入一条用户记录/// </summary>/// <param name="model"></param>/// <returns></returns>public int addUser(ers model) {int n = 0;string sqlstr = "insert into Users values('" + erID + "','" + erPwd + "','" + model.LevelID + "')";n = sql.Execsql(sqlstr);return n;}/// <summary>/// 修改一个用户/// </summary>/// <param name="model"></param>/// <returns></returns>public int updateUser(ers model) {int n = 0;string sqlstr = "update Users set UserPwd='"+erPwd+"',LevelID='"+model.LevelID+"' where UserID='"+erID+"'";n=sql.Execsql(sqlstr);return n;}/// <summary>/// 删除一个用户/// </summary>/// <param name="id"></param>/// <returns></returns>public int deleteUser(string id) {int n = 0;string sqlstr = "delete from Users where UserID='" + id + "'";n = sql.Execsql(sqlstr);return n;}/// <summary>/// 条件查询/// </summary>/// <param name="strUserID"></param>/// <param name="strLevelID"></param>/// <returns></returns>public DataTable findUsersList(string strUserID,String strLevelID){if (strUserID.Trim().Length ==0) {strUserID = "%"; }if (strLevelID.Trim().Length == 0) {strLevelID = "%";}string sqlstr = "select * from Users where UserID like '%" + strUserID.Trim() + "%' and LevelID like '%" + strLevelID.Trim() + "%'";DataTable dt = sql.Execusql(sqlstr);return dt;}。
北邮操作系统进程管理实验报告及源代码

进程管理实验报告1. 实验目的:(1)加深对进程概念的理解, 明确进程和程序的区别;(2)进一步认识并发执行的实质;(3)分析进程争用资源的现象, 学习解决进程互斥的方法;(4)了解Linux系统中进程通信的基本原理。
2. 实验预备内容(1)阅读Linux的sched.h源码文件, 加深对进程管理概念的理解;(2)阅读Linux的fork()源码文件, 分析进程的创建过程。
3.环境说明本次实验使用的是win7下的VMWare workstation虚拟机, 安装了ubuntu系统在ubuntu系统下使用code::blocks IDE编写代码并执行程序的4.实验内容:1.进程的创建:(1)实验题目和要求:编写一段程序, 使用系统调用fork() 创建两个子进程。
当此程序运行时, 在系统中有一个父进程和两个子进程活动。
让每一个进程在屏幕上显示一个字符:父进程显示字符“a”, 子进程分别显示字符“b”和“c”。
试观察记录屏幕上的显示结果, 并分析原因。
(2)程序设计说明:参照书上的例子进行设计, 详见源代码(3)程序运行结果截图:(4)程序分析:a,b,c随机出现, 因为父进程与两个子进程之间并没有同步措施, 所以a,b,c随机打印出来, 也就是三个进程的活动次序是随机进行的, 不同的系统可能有不同的进程调度方式。
(5)源程序:#include<sys/types.h>#include<stdio.h>#include<unistd.h>int main(){pid_t pid1,pid2;if((pid1=fork())<0){printf("Fork Failed.\n");exit(-1);}else if((pid1=fork())==0)printf("b\n");else{if((pid2=fork())<0){printf("Fork Failed.\n");exit(-1);}else if((pid2=fork())==0)printf("c\n");else{wait(NULL);printf("a\n");exit(0);}}return 0;}2.进程的控制:要求一:(1)实验题目和要求:修改已经编写的程序, 将每个进程输出一个字符改为每个进程输出一句话, 再观察程序执行时屏幕上出现的现象, 并分析原因。
操作系统课程设计报告题目及代码

题目一模拟操作系统设计设计一个模拟操作系统管理程序,实现以下管理功能:1.内存管理功能2.文件管理功能3.磁盘管理功能题目二虚拟存储器各页面置换算法的实现与比较内容:设计一个虚拟存储区和内存工作区,通过产生一个随机数的方法得到一个页面序列,假设内存给定的页面数由键盘输入,分别计算使用下述各方法时的内存命中率:先进先出算法〔FIFO〕、最近最少使用算法〔LRU〕、最正确淘汰算法〔OPT〕、最少页面算法〔LFU〕等。
题目三文件系统设计通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能及内部实现。
内容:为Linu*系统设计一个简单的二级文件系统,以实现以下功能:1.可以实现以下几条命令(1)login 用户登录(2)dir 文件目录列表(3)creat 创立文件(4)delete 删除文件(5)open 翻开文件(6)close 关闭文件(7)read 读文件(8)write 写文件2.实验提示〔1〕首先确定文件系统的数据构造:主目录、子目录及活动文件等。
主目录和子目录都以文件的形式存放在磁盘,这样便于查找和修改。
〔2〕用户创立的文件,可以编号存储于磁盘上。
如file0、file1、file2……等,并以编号作为物理地址,在目录中进展登记。
[清华大学?操作系统教程? *丽芬编著题目四设计一个按时间片轮转法进程CPU调度的程序。
提示:〔1〕假设系统有5个进程,每个进程用一个进程控制块PCB来代表,PCB中包含进程名、指针、到达时间、估计运行时间、进程状态表。
其中,进程名即为进程进标识。
〔2〕为每一个进程设计一个要示运行时间和到达时间。
〔3〕按照进程到达的先后顺序排成一个循环队列,再设一个队首指针指向第一个到达的进程首址。
〔4〕执行处理机调度时,开场选择队首的第一个进程运行。
另外再设一个当前运行进程指针,指向当前正运行的进程。
〔5〕由于本实验是模拟实验,所以对被选中进程并不实际启运运行,只是执行:a.估计驼行时间减1b.输出当前运行进程的名字。
C语言课程设计-设备管理系统

一份可以在vc6.0等软件上运行的C语言代码清单,其中一个主函数,设置多个调用函数,分别实现排序,输入,读取等功能模块。
#include<stdio.h>#include<stdlib.h>#include<ctype.h>#include<string.h>#define feipeineicun(p) do{ p=(shebei*)malloc(sizeof(shebei));if(p==NULL){printf("内存分配失败!");exit(-1);} }while(0)#define len sizeof(shebei)typedef struct time{int year;int mouth;int day;}time;typedef struct shebei{int bianhao;char mingcheng[15];char zhonglei[15];int danjia;int niandai;char chanjia[15];char baofei[10];time time;struct shebei *next;}shebei;//extern int//函数声明下面void caidan(shebei *top);int xuanze();shebei *xinxiluru(shebei *top); void neirongxianshi(shebei *top); void xinxichazhao(shebei *top); shebei *xinxixiugai(shebei *top); shebei *xinxishanchu(shebei *top); shebei *shebeibaofei(shebei *top);shebei *xinxipaixu(shebei *top);void fenleitongji(shebei *top);void tongmingguibing(shebei *top);void baocun(shebei *top);//#include"头文件.h"void main(){shebei *top;printf("\t************************************************* *********\n");printf("\t----------------------------------------------------------\n");printf("\t************************************************* *********\n");printf("\t--------\n");printf("\t**** 欢迎进入单位仪器设备基本信息管理系统****\n");printf("\t--------\n");printf("\t************************************************* *********\n");printf("\t----------------------------------------------------------\n");printf("\t************************************************* *********\n");do{ top=(shebei*)malloc(sizeof(shebei));if(top==NULL){printf("内存分配失败!");exit(-1);} }while(0);top->next=NULL;caidan(top);}//#include"头文件.h"void caidan(shebei *top){while(1){switch(xuanze()){case 1:top=xinxiluru(top);break;case 2:neirongxianshi(top);break;case 3:xinxichazhao(top);break;case 4:top=xinxixiugai(top);break;case 5:top=xinxishanchu(top);break;case 6:top=shebeibaofei(top);break;case 7:top=xinxipaixu(top);break;case 8:tongmingguibing(top); //先同名合并fenleitongji(top);break;case 9:baocun(top);break;default:printf("退出操作,拜拜!\n");exit(0);}}}int xuanze(){int n;printf("\n1.增加记录\n");printf("2.查看信息\n");printf("3.查找信息\n");printf("4.修改信息\n");printf("5.删除信息\n");printf("6.设备报废\n");printf("7.信息排序\n");printf("8.分类统计\n");printf("9.保存信息\n");printf("按0退出\n");for(;;){scanf("%d",&n);if(n<0||n>9)printf("\n输入错误,请重新输入:");elsebreak;}return n;}//#include"头文件.h"shebei *shebeibaofei(shebei *top){shebei *p;char b[15],c;p=top->next;if(top->next==NULL){printf("没有信息记录,返回菜单选择\n");return top;}printf("请输入要报废的设备名称:");scanf("%s",b);while(p){if(strcmp(b,p->mingcheng)==0){if(strcmp(p->baofei,"是")!=0){printf("将要报废的设备信息如下:\n");printf("设备编号设备名称设备种类设备单价购进年代生产厂家是否报废\n");printf("%d %s %s %d %d %s %s\n",p->bianhao,p->mingcheng,p->zhonglei,p->danjia,p->niandai,p->chan jia,p->baofei);printf("是否确认报废?(Y/N):");scanf("%c",&c);scanf("%c",&c);if(c=='y'||c=='Y'){strcpy(p->baofei,"是");printf("此设备已被报废\n");}}elseprintf("此设备已经为报废状态\n");break;}p=p->next;}if(p==NULL)printf("没有找到该设备信息\n");printf("按任意键返回主菜单\n");getchar();getchar();return top;}//#include"头文件.h"shebei *xinxiluru(shebei *top){int n;shebei *p,*q;p=top;while(p->next!=NULL){p=p->next;}printf("请输入设备信息:\n输入0则结束\n"); do{feipeineicun(q);printf("请输入设备的编号");scanf("%d",&q->bianhao);if(q->bianhao==0)break;printf("请输入设备的名称");scanf("%s",q->mingcheng);printf("请输入设备的种类");scanf("%s",q->zhonglei);printf("请输入设备的单价");scanf("%d",&q->danjia);printf("请输入设备的年代");scanf("%d",&q->niandai);printf("请输入设备的生产产家");scanf("%s",q->chanjia);printf("请输入设备的报废情况\n");printf("0.没有报废\n1.已经报废\n");for(;;){scanf("%d",&n);if(n!=0&&n!=1)printf("输入错误,请重新输入:\n");elsebreak;}if(n==0){strcpy(q->baofei,"否");q->time.year=q->time.day=q->time.day=0;}else{strcpy(q->baofei,"是");printf("请输入报废时间(格式为2015 10 12):");scanf("%d%d%d",&q->time.year,&q->time.mouth,&q->time.day);}p->next=q;p=q;p->next=NULL;}while(1);free(q);printf("录入完毕,按任意键返回主菜单\n");getchar();getchar();return top;}//#include"头文件.h"void neirongxianshi(shebei *top){shebei *p;p=top->next;if(top->next){printf("设备信息如下:\n");printf("设备编号设备名称设备种类设备单价购进年代生产厂家是否报废\n");}elseprintf("记录为空,返回菜单\n");while(p){printf("%d %s %s %d %d %s %s\n",p->bianhao,p->mingcheng,p->zhonglei,p->danjia,p->niandai,p->chan jia,p->baofei);p=p->next;}printf("按任意键返回主菜单\n");getchar();getchar();}//#include"头文件.h"void baocun(shebei *top){FILE *fp;shebei *p;p=top->next;if(top->next==NULL){printf("没有东西可存!\n");return;}if((fp=fopen("shebei.txt","wb"))==NULL) {printf("\n");exit(1);}while(p){fwrite(p,len,1,fp);p=p->next;}fclose(fp);printf("信息已经存入文件,请继续\n"); }//#include"头文件.h"void xinxichazhao(shebei *top){shebei *p;int a,n;char b[15];p=top->next;if(top->next==NULL){printf("没有记录信息,返回菜单选择\n");return;}printf("查找方式:\n");printf("1.按编号查找\n2.按名称查找\n");for(;;){scanf("%d",&n);if(n!=2&&n!=1)printf("输入错误,请重新输入:\n");elsebreak;}if(n==1){printf("请输入设备编号:");scanf("%d",&a);while(p){if(a==p->bianhao){printf("查找到的设备信息如下:\n");printf("设备编号设备名称设备种类设备单价购进年代生产厂家是否报废\n");printf("%d %s %s %d %d %s %s\n",p->bianhao,p->mingcheng,p->zhonglei,p->danjia,p->niandai,p->chan jia,p->baofei);break;}p=p->next;}}elseif(n==2){printf("请输入设备名称:");scanf("%s",b);while(p){if(strcmp(b,p->mingcheng)==0){printf("查找到的设备信息如下:\n");printf("设备编号设备名称设备种类设备单价购进年代生产厂家是否报废");printf("%d %s %s %d %d %s %s\n",p->bianhao,p->mingcheng,p->zhonglei,p->danjia,p->niandai,p->chan jia,p->baofei);break;}p=p->next;}}if(p==NULL)printf("没有找到该信息\n");printf("按任意键返回主菜单\n");getchar();getchar();}//#include"头文件.h"shebei *xinxipaixu(shebei *top){shebei *p,*q,*s,*t;t=top;s=p=top->next;if(p==NULL){printf("内容为空,按任意键返回菜单选择");getchar();getchar();return top;}if(p->next==NULL){printf("排序完毕,返回主菜单\n");return top;}while(p){q=p->next;while(q){if(p->danjia>q->danjia){s->next=q->next;t->next=q;q->next=p;p=q;q=q->next;}else{s=s->next;q=q->next;}}t=t->next;s=p=p->next;}printf("排序完毕,返回主菜单\n");return top;}//#include"头文件.h"shebei *xinxishanchu(shebei *top){shebei *p,*q;int a;p=top->next;q=top;if(top->next==NULL){printf("没有可删除的信息,按任意键返回菜单选择\n");getchar();getchar();return top;}printf("请输入要删除的设备编号:");scanf("%d",&a);while(p){if(a==p->bianhao){printf("\n删除的设备信息如下:\n");printf("设备编号设备名称设备种类设备单价购进年代生产厂家是否报废\n");printf("%d %s %s %d %d %s %s\n",p->bianhao,p->mingcheng,p->zhonglei,p->danjia,p->niandai,p->chan jia,p->baofei);if(p->next==NULL){q->next=NULL;free(p);}else{q->next=p->next;free(p);}break;}q=q->next;p=p->next;}if(p==NULL)printf("此设备不存在,按任意键返回\n");elseprintf("----删除成功,按任意键返回----\n");getchar();getchar();return top;}//#include"头文件.h"void tongmingguibing(shebei *top){shebei *p,*q,*s,*t,*j,*k;int flag;t=top;s=p=top->next;if(p==NULL){// printf("内容为空,按任意键返回上级");getchar();getchar();return;}if(p->next==NULL){// printf("排序完毕,返回上级\n");return;}while(p){flag=0;q=p->next;while(q){if(strcmp(p->mingcheng,q->mingcheng)==0){s->next=q->next;t->next=q;q->next=p;p=q;q=s->next;}else{if(flag==0){j=q;k=s;flag++;}s=s->next;q=q->next;}}if(flag!=0){p=j;t=s;}else{//printf("归并完毕,返回主菜单\n");return;}}// printf("归并完毕,返回主菜单\n");return;}void fenleitongji(shebei *top){shebei *p,*q,*s;int flag,n=0;p=top->next;s=p;if(top->next==NULL){printf("内容为空,按任意键返回菜单选择");getchar();getchar();return;}//tongmingguibing(top);while(p){flag=0;n=1;q=p->next;while(q){if(strcmp(p->mingcheng,q->mingcheng)==0)n++;else{if(flag==0)s=q;flag++;}q=q->next;}printf("名称为%s的设备数量为%d\n",p->mingcheng,n);if(flag!=0)p=s;elsereturn;}}//#include"头文件.h"shebei *xinxixiugai(shebei *top){shebei *p;int a,m,n;char b[15];p=top->next;if(top->next==NULL){printf("没有可供修改的信息,返回菜单选择\n");return top;}printf("请输入修改方式:\n");printf("1.按编号\n2.按名称\n");for(;;){scanf("%d",&n);if(n!=2&&n!=1)printf("输入错误,请重新输入:\n");elsebreak;}if(n==1){printf("请输入要修改的设备编号:");scanf("%d",&a);while(p){if(a==p->bianhao){printf("查找到要修改的设备信息如下:\n");printf("设备编号设备名称设备种类设备单价购进年代生产厂家是否报废\n");printf("%d %s %s %d %d %s %s\n",p->bianhao,p->mingcheng,p->zhonglei,p->danjia,p->niandai,p->chan jia,p->baofei);printf("请重新输入设备的编号");scanf("%d",&p->bianhao);printf("请重新输入设备的名称");scanf("%s",p->mingcheng);printf("请重新输入设备的种类");scanf("%s",p->zhonglei);printf("请重新输入设备的单价");scanf("%d",&p->danjia);printf("请重新输入设备的年代");scanf("%d",&p->niandai);printf("请重新输入设备的生产产家"); scanf("%s",p->chanjia);printf("请重新输入设备的报废情况\n"); printf("0.没有报废\n1.已经报废\n");for(;;){scanf("%d",&m);if(m!=0&&m!=1)printf("输入错误,请重新输入:\n");elsebreak;}if(m==0){strcpy(p->baofei,"否");p->time.year=p->time.day=p->time.day=0;}else{strcpy(p->baofei,"是");printf("请输入报废时间(格式为2015 10 12):");scanf("%d%d%d",&p->time.year,&p->time.mouth,&p->time.day);}break;}p=p->next;}}elseif(n==2){printf("请输入要修改的设备名称:");scanf("%s",b);while(p){if(strcmp(b,p->bianhao)==0){printf("查找到要修改的设备信息如下:\n");printf("设备编号设备名称设备种类设备单价购进年代生产厂家是否报废\n");printf("%d %s %s %d %d %s %s\n",p->bianhao,p->mingcheng,p->zhonglei,p->danjia,p->niandai,p->chan jia,p->baofei);printf("请重新输入设备的编号");scanf("%d",&p->bianhao);printf("请重新输入设备的名称");scanf("%s",p->mingcheng);printf("请重新输入设备的种类");scanf("%s",p->zhonglei);printf("请重新输入设备的单价");scanf("%d",&p->danjia);printf("请重新输入设备的年代");scanf("%d",&p->niandai);printf("请重新输入设备的生产产家"); scanf("%s",p->chanjia);printf("请重新输入设备的报废情况\n"); printf("0.没有报废\n1.已经报废\n");for(;;){scanf("%d",&m);if(m!=0&&m!=1)printf("输入错误,请重新输入:\n");elsebreak;}if(m==0){ strcpy(p->baofei,"否");p->time.year=p->time.day=p->time.day=0; }else{strcpy(p->baofei,"是");printf("请输入报废时间(格式为2015 10 12):");scanf("%d%d%d",p->time.year,p->time.mouth,p->time.day);}break;}p=p->next;}}if(p==NULL)printf("不存在你要修改的信息\n");printf("按任意键返回主菜单\n");getchar();getchar();return top;}。
C语言课程设计设备管理系统

#i n c l u d e<> #include <>#include <>#include <>voidmenu();no,d[tot].dname,d[tot].downer,d[tot].ddpartment,d[tot].dam ount,d[tot].dtime,d[tot].dprice)!=EOF){d[tot++].dlt=0;}fclose(fp);}void write(){int i;FILE *fp=fopen("D:\\","w");for (i=0;i<tot;i++)if (!d[i].dlt)fprintf(fp,"%s %s %s %s %s %s %s\n",d[i].dno,d[i].dname,d[i].down er,d[i].ddpartment,d[i].damount,d[i].dtime,d[i].dprice);fclose(fp);}void menu(){char a;while (1){puts("★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★请选择需要的功能:★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★");puts("1.查看所有设备信息\n2.修改设备信息\n3.删除设备信息\n4.增加设备信息\n5.统计设备信息\n 0.退出系统");a=getch();switch (a){case '0':return;case '1':{lkp();break;}case '2':{chng();break;}case '3':{dlt();break;}case '4':{add();break;}case '5':{count();break;}default:printf("您输入的信息有误!请检查后重新输入\n"); }}}void lkp(){system("cls");int i;printf(" 设备号设备名称领用人所属部门数量购买时间价格\n");for(i=0;i<tot;i++){if(d[i].dlt==0)printf("%20s%20s%20s%20s%20s%20s%20s\n",d[i].dno,d[i].dname,d[ i].downer,d[i].ddpartment,d[i].damount,d[i].dtime,d[i].dprice);}}void chng(){system("cls");char s,ch[20];int i,j;printf("请输入您要修改的设备的编号:(0键返回菜单栏)\n");printf(" 设备号设备名称领用人所属部门数量购买时间价格\n");for(i=0;i<tot;i++){printf("%4d.%20s%20s%20s%20s%20s%20s%20s\n",i+1,d[i].dno,d[i]. dname,d[i].downer,d[i].ddpartment,d[i].damount,d[i].dtime,d[i].dp rice);}while (1){scanf("%s",ch);i=atoi(ch);if (i>=1 && i<=tot)break;else if(i==0){system("cls");return;}else puts("您输入的信息有误!请检查后重新输入\n");}if(d[i-1].dlt==1){printf("该设备已经被删除!\n");return;}printf("请输入您要修改的内容的编号:\n1.设备号\n2.设备名称\n3.领用人\n4.所属部门\n5.数量\n6.购买时间\n7.价格\n0.返回菜单栏\n");while (1){scanf("%s",ch);j=atoi(ch);if (j>=1 && j<=7)break;else if(i==0){system("cls");return;}else puts("您输入的信息有误!请检查后重新输入\n");}printf("请输入您要修改的信息:\n");switch (j)case 1:{scanf("%s",d[i-1].dno);break;}case 2:{scanf("%s",d[i-1].dname);break;}case 3:{scanf("%s",d[i-1].downer);break;}case 4:{scanf("%s",d[i-1].ddpartment);break;}case 5:{scanf("%s",d[i-1].damount);break;}case 6:{scanf("%s",d[i-1].dtime);break;}case 7:{scanf("%s",d[i-1].dprice);break;}printf("请问是否要继续修改(Y/N)\n");while (1){getchar();gets(&s);if (s=='Y'||s=='N'||s=='y'||s=='n')break;else puts("您输入的信息有误!请检查后重新输入\n");}if(s=='Y'||s=='y')chng();else if(s=='N'||s=='n')system("cls");}void dlt(){system("cls");char ch[20],s;int i;printf("请输入你要删除的设备的编号:(0键返回菜单烂)\n");printf(" 设备号设备名称领用人所属部门数量购买时间价格\n");for(i=0;i<tot;i++){if(d[i].dlt==0)printf("%4d.%20s%20s%20s%20s%20s%20s%20s\n",i+1,d[i].dno,d[i]. dname,d[i].downer,d[i].ddpartment,d[i].damount,d[i].dtime,d[i].dp rice);}while (1){scanf("%s",ch);i=atoi(ch);if (i>=1 && i<=tot)break;else if(i==0){system("cls");return;}else puts("您输入的信息有误!请检查后重新输入\n");}d[i-1].dlt=1;printf("请问是否要继续删除(Y/N)\n");while (1){getchar();gets(&s);if (s=='Y'||s=='N'||s=='y'||s=='n')break;else puts("您输入的信息有误!请检查后重新输入\n");}if(s=='Y'||s=='y')dlt();else if(s=='N'||s=='n')system("cls");}void add(){char s;system("cls");printf("请输入您要添加的设备号:\n");scanf("%s",d[tot].dno);printf("请输入您要添加的设备的名称:\n");scanf("%s",d[tot].dname);printf("请输入您要添加的设备的领用人:\n");scanf("%s",d[tot].downer);printf("请输入您要添加的设备的所属部门:\n");scanf("%s",d[tot].ddpartment);printf("请输入您要添加的设备的数量:\n");scanf("%s",d[tot].damount);printf("请输入您要添加的设备的购买时间:\n");scanf("%s",d[tot].dtime);printf("请输入您要添加的设备的价格:\n");scanf("%s",d[tot].dprice);d[tot].dlt=0;tot+=1;printf("请问是否要继续添加(Y/N)\n");while (1){getchar();gets(&s);if (s=='Y'||s=='N'||s=='y'||s=='n')break;else puts("您输入的信息有误!请检查后重新输入\n");}if(s=='Y'||s=='y')add();else if(s=='N'||s=='n')system("cls");}void count(){system("cls");char ch[20],temp[20],s;int i,j,n;sum=0;printf("请选择统计的方式:\n1.所有设备的总价值。
操作系统课程设计 设备管理实现 源代码

#include <stdio.h>#include "iostream.h"#include "stdlib.h"#include "string.h"struct PCB{int id;char name[10];int size;struct PCB *next;};struct PCB *running;struct PCB *ready;struct PCB *blocked;struct PCB *q;struct PCB *p;int id=1;int size;char name[10];////////////////////////////////////////////////////////////////////////////////////// struct DCT{ //设备char name[10];int busy;PCB * occupied;PCB * waiting;struct DCT *next;struct COCT* coct; //上级控制器};struct COCT{ //控制器char name[10];int busy;PCB * occupied;PCB * waiting;struct COCT *next;struct CHCT* chct; //控制器的上级通道};struct CHCT{ //通道char name[10];int busy;PCB * occupied;PCB * waiting;struct CHCT *next;//////////////////////////////////////////////////////////////////////////////////////struct DCT * dcts;struct COCT *cocts;struct CHCT *chcts;void enqueue(int id,char *name,int size,struct PCB *head){ struct PCB *node=(struct PCB *)malloc(sizeof(struct PCB));node->next=0;node->id=id;strcpy(node->name,name);node->size=size;struct PCB *tmp=head;while(tmp->next!=0)tmp=tmp->next;tmp->next=node;}struct PCB * dequeue(struct PCB *head){struct PCB * tmp=head->next;if(head->next!=0){head->next=head->next->next;tmp->next=0;}return(tmp);}void createProcess(){printf("\nname: ");scanf("%s",name);printf("size: ");scanf("%d",&size);printf("\n");enqueue(id++,name,size,ready);if(running==0){running=dequeue(ready);}}void switchProcess(){if(running!=0&&ready->next!=0)enqueue(running->id,running->name,running->size,ready);running=dequeue(ready);}elseprintf("没有可切换的进程\n");}void blockProcess(){if(running==0)printf("没有可阻塞的进程\n");else{enqueue(running->id,running->name,running->size,blocked);running=0;if(ready->next==0)printf("没有可执行的进程\n");elserunning=dequeue(ready);}}void wakeupProcess(){if(blocked->next==0)printf("没有可激活的进程");else{enqueue(blocked->next->id,blocked->next->name,blocked->next->size,ready); dequeue(blocked);if(running==0)running=dequeue(ready);}}void terminateProcess(){ //结束进程if(running==0){printf("没有需要结束的进程\n");}else{running=dequeue(ready);}}void displayProcessstatus(){printf("--------就绪态--------\n");if(ready->next==0)printf("当前没有进程在该状态\n");if(ready->next!=0){q=ready->next;while(ready->next!=0){printf("%s",ready->next->name);printf(" %d\n",ready->next->size);ready->next=ready->next->next;}ready->next=q;}printf("--------执行状态--------\n");if(running==0) printf("当前没有进程在该状态\n");if(running!=0){printf("%s",running->name);printf(" %d\n",running->size);}printf("--------阻塞状态--------\n");if(blocked->next==0) printf("当前没有进程在该状态\n\n");if(blocked->next!=0){p=blocked->next;while(blocked->next!=0){printf("%s",blocked->next->name);printf(" %d\n",blocked->next->size);blocked->next = blocked->next->next;}blocked->next=p;}}//////////////////////////////////////////////////////////////////////////////////struct DCT * findDCT(char name[]) //设备分配时找到要添加的设备{struct DCT *temp = dcts;while(temp->next!=NULL){temp = temp->next;if(strcmp(temp->name,name)==0)return temp;}return NULL;}struct CHCT * findChannel(char name[]){struct CHCT *temp = chcts;while(temp->next!=NULL){temp = temp->next;if(strcmp(temp->name,name)==0)return temp;}return NULL;}struct COCT * findController(char name[]){struct COCT *temp = cocts;while(temp->next!=NULL){temp = temp->next;if(strcmp(temp->name,name)==0)return temp;}return NULL;}void addProcesstoWaiting(struct PCB * waiting,struct PCB *p)//进入进程等待队列{struct PCB *temp = waiting;while(temp->next!=NULL){temp = temp->next;}//temp->next = p;//+++++++++++++++++++++++++++++++++++++++++++++++++temp->next = new struct PCB;temp->next->id = p->id;strcpy(temp->next->name,p->name);temp->next->size = p->size;temp->next->next = NULL;//++++++++++++++++++++++++++++++++++++++++++++++++++ }void add(struct PCB * head,struct PCB * node){ //入队列struct PCB *tmp=head;while(tmp->next!=0)tmp=tmp->next;tmp->next=node;}struct PCB * getFirst(struct PCB *head){ //获得队列里的第一个进程return head->next;}void allocateCHCT(struct CHCT* chct,PCB *p)//分配CHCT{if(chct->occupied!=0){printf("不能分配通道\n");addProcesstoWaiting(chct->waiting,p);}else{chct->occupied=p;printf("分配成功!\n");}add(blocked,p);if(ready!=0)running=dequeue(ready);elserunning=0;}//**************************************************void allocateCOCT(struct COCT* coct,PCB *p){if(coct->occupied!=0){printf("不能分配控制器\n");addProcesstoWaiting(coct->waiting,p);add(blocked,p);if(ready!=0)running=dequeue(ready);elserunning=0;return;}else{coct->occupied=p;allocateCHCT(coct->chct,p);}}void allocateDCT(){char nameDCT[10];printf("请输入设备名称:");scanf("%s",nameDCT);struct DCT * dct=findDCT(nameDCT); struct PCB * p = running;if(dct!=NULL&&p!=NULL){if(dct->occupied!=0){printf("不能分配设备\n"); addProcesstoWaiting(dct->waiting,p); add(blocked,p);if(ready!=0)running=dequeue(ready);elserunning=0;return;}else{dct->occupied=p;allocateCOCT(dct->coct,p);//++++++++++++++++++++++++++++ /*add(blocked,p);if(ready!=0)running=dequeue(ready);elserunning=0;return;*///+++++++++++++++++++++++++++++ }}elseprintf("发生错误!\n");}void releaseCHCT(char *name,struct CHCT* chct,struct PCB* p) //?????? {if(p!=NULL)addProcesstoWaiting(chct->waiting,p);if(strcmp(name,chct->occupied->name)==0){if(chct->waiting->next!=NULL){chct->occupied = dequeue(chct->waiting);}else{chct->occupied = NULL;}}}void releaseCOCT(char *name,struct COCT* coct,struct PCB* p){if(p!=NULL)addProcesstoWaiting(coct->waiting,p);if(strcmp(name,coct->occupied->name)==0){if(coct->waiting->next!=NULL){coct->occupied = dequeue(coct->waiting);}else{coct->occupied = NULL;}releaseCHCT(name,coct->chct,coct->occupied);}}void releaseDCT(){char nameDCT[10];printf("请输入要释放的设备名称:\n");scanf("%s",nameDCT);char nameP[10];printf("请输入要释放的进程名称:\n");scanf("%s",nameP);struct DCT *temp = findDCT(nameDCT);if(strcmp(temp->occupied->name,nameP)==0){if(temp->waiting->next!=NULL){temp->occupied = dequeue(temp->waiting);}else{temp->occupied = NULL;}releaseCOCT(nameP,temp->coct,temp->occupied);}else{printf("没有对应的设备和进程!");}}void addChannel(char name[]){struct CHCT * temp=(struct CHCT *)malloc(sizeof(struct CHCT));strcpy(temp->name,name);temp->next=0;temp->busy=0;temp->waiting = new struct PCB;temp->waiting->next = NULL;//temp->waiting=0;temp->occupied=0;struct CHCT * head=chcts; //进入了chcts队列while(head->next!=0)head=head->next;head->next=temp;}void addController(char *name,struct CHCT * chct){ //增加控制器struct COCT * temp=(struct COCT *)malloc(sizeof(struct COCT));strcpy(temp->name,name);temp->next=0;temp->busy=0;temp->waiting = new struct PCB;temp->waiting->next = NULL;//temp->waiting=0; //+temp->occupied=0;temp->chct= chct;struct COCT * head=cocts; //进入了cocts队列while(head->next!=0)head=head->next;head->next=temp;}void addDevice(char *name,struct COCT * coct){ //增加设备struct DCT * temp=(struct DCT *)malloc(sizeof(struct DCT));strcpy(temp->name,name);temp->next=0;temp->busy=0;temp->waiting = new struct PCB;temp->waiting->next = NULL;//temp->waiting=0;temp->occupied=0;temp->coct= coct;struct DCT * head=dcts;while(head->next!=0)head=head->next;head->next=temp;}//添加设备++++++++++++++++++++++++++++++++++void add_dct(){char newDCT[10];printf("请输入新设备的名字:\n");scanf("%s",newDCT);char newCOCT[10];printf("请输入要添加到的控制器的名字:\n");scanf("%s",newCOCT);addDevice(newDCT,findController(newCOCT));}//添加控制器void add_coct(){char newCOCT[10];printf("请输入新控制器的名字:\n");scanf("%s",newCOCT);char newCHCT[10];printf("请输入要添加到的通道的名字:\n");scanf("%s",newCHCT);addController(newCOCT,findChannel(newCHCT));}//添加通道void add_chct(){char newCHCT[10];printf("请输入新的通道的名字:\n");scanf("%s",newCHCT);addChannel(newCHCT);}//++++++++++++++++++++++++++++++++++//+++++++++++++++++删除操作++++++++++++++++++++ //删除设备void deleteDCT(char nameDCT[]){//char nameDCT[10];//int i=0;//printf("请输入要删除DCT的名字:");//scanf("%s",nameDCT);struct DCT * temp = findDCT(nameDCT);struct DCT * head = dcts;if(temp==NULL){printf("没有对应的设备!\n");return ;}elsewhile(head->next!=0){if(strcmp(temp->name,head->next->name)==0){if(temp->occupied!=NULL)printf("此设备现在正在使用不能删除\n");elsehead->next=head->next->next;//i++;break;}elsehead=head->next;}}//删除控制器void deleteCOCT(char nameCOCT[]){struct COCT *temp=findController(nameCOCT);struct COCT *head=cocts;if(temp==NULL){printf("没有对应的控制器\n");return;}elsewhile(head->next!=0){if(strcmp(temp->name,head->next->name)==0){if(temp->occupied!=NULL)printf("此控制器现在正在使用不能删除\n");else{//deleteDCT(temp->);head->next=head->next->next;}break;}head=head->next;}}//删除通道void deleteCHCT(char nameCHCT[]){struct CHCT *temp=findChannel(nameCHCT);struct CHCT *head=chcts;if(temp==NULL){printf("没有对应的通道\n");return;}elsewhile(head->next!=0){if(strcmp(temp->name,head->next->name)==0){if(temp->occupied!=NULL)printf("此通道现在正在使用不能删除\n");else{//deleteDCT(temp->);head->next=head->next->next;}//i++;break;}head=head->next;}}//+++++++++++++++++++++++++++++++++++++++++++void displayDCT(){struct DCT * dct;struct COCT *coct;struct CHCT *chct = chcts;struct PCB *pcb;//------------------------------while(chct->next!=NULL){chct = chct->next;printf(" %s(",chct->name);if(chct->occupied!=0)printf("%s",chct->occupied->name);printf(")");pcb = chct->waiting->next;//waiting是头结点,pcb指向队列第一个进程while(pcb!=NULL){printf("[%s]",pcb->name);pcb = pcb->next;}printf("\n");//----------------------------------------------------coct = cocts;while(coct->next!=NULL){coct = coct->next;if(strcmp(coct->chct->name,chct->name)==0){printf(" %s(",coct->name);if(coct->occupied!=0)printf("%s",coct->occupied->name);printf(")");pcb = coct->waiting->next;while(pcb!=NULL){printf("[%s]",pcb->name);pcb = pcb->next;}printf("\n");//-----------------------------------------dct = dcts;while(dct->next!=NULL){dct = dct->next;if(strcmp(dct->coct->name,coct->name)==0){printf(" %s(",dct->name);if(dct->occupied!=0)printf("%s",dct->occupied->name);printf(")");pcb = dct->waiting->next;while(pcb!=NULL){printf("[%s]",pcb->name);pcb = pcb->next;}printf("\n");}}}}}}//////////////////////////////////////////////////////////////////////void main(){dcts=(struct DCT *)malloc(sizeof(struct DCT));dcts->next=0;cocts=(struct COCT *)malloc(sizeof(struct COCT));cocts->next=0;chcts=(struct CHCT *)malloc(sizeof(struct CHCT));chcts->next=0;addChannel("chct1");addChannel("chct2");addController("coct1",findChannel("chct1"));addController("coct2",findChannel("chct1"));addController("coct3",findChannel("chct2"));addDevice("dct1",findController("coct1"));addDevice("dct2",findController("coct1"));addDevice("dct3",findController("coct2"));//addDevice("dct4",findController("coct2"));addDevice("dct4",findController("coct3"));ready=(struct PCB *)malloc(sizeof(struct PCB));blocked=(struct PCB *)malloc(sizeof(struct PCB));ready->next=0;blocked->next=0;A:while(1){int choice;int function;printf("1:创建进程\n");printf("2:切换进程\n");printf("3:阻塞进程\n");printf("4:唤醒进程\n");printf("5:结束进程\n");printf("6:显示进程\n");printf("7:设备管理\n");printf("0:exit\n");scanf("%d",&choice);switch(choice){case 1:createProcess();break;case 2:switchProcess();break; case 3:blockProcess();break;case 4:wakeupProcess();break; case 5:terminateProcess();break; case 6:displayProcessstatus();break; case 7:while(1){printf("1:设备分配\n");printf("2:设备释放\n");printf("3:显示\n");printf("4:添加新的设备\n");printf("5:添加新的控制器\n"); printf("6:添加新的通道\n");printf("7:删除设备\n");printf("8:删除控制器\n");printf("9:删除通道\n");printf("0:返回主界面\n");scanf("%d",&function);switch(function){case 1:allocateDCT();break;case 2:releaseDCT();break;case 3:displayDCT();break;case 4:add_dct();break;case 5:add_coct();break;case 6: add_chct();break;case 7:char nameDCT[10];printf("请输入要删除DCT的名字:\n");scanf("%s",nameDCT);deleteDCT(nameDCT);break; case 8:char nameCOCT[10];printf("请输入要删除COCT的名字:\n");scanf("%s",nameCOCT);deleteCOCT(nameCOCT);break;case 9:char nameCHCT[10];printf("请输入要删除CHCT的名字:\n");scanf("%s",nameCHCT);deleteCHCT(nameCHCT);break;case 0:goto A;break;}}case 0:exit(0);break;}}}。
操作系统设备管理实验代码

#include<iostream>using namespace std;//typedef struct node{char name[10];char devname[10];struct node *next;}PCB;//进程PCB * run;//执行队列//typedef struct Node{char identify[10];//标识int state;//设备状态PCB *blocking;//阻塞队列指针}CHCT;//通道控制表CHCT * CH1, * CH2;//typedef struct NOde{char identify[10];//标识int state;//设备状态CHCT * chct;//CHCTPCB *blocking;//阻塞队列指针struct NOde *next;}COCT;//控制器控制表COCT * cohead;typedef struct NODe{char identify[10];//标识char type;//设备类型int state;//设备状态int times;//重复执行次数PCB *blocking;//阻塞队列指针COCT * coct;//COCT}DCT;//设备控制表typedef struct NODE{char type;//类别char identify[10];//标识DCT * dct;//DCT//驱动入口地址struct NODE *next;}SDT;//系统设备表SDT * head;SDT * checkdel;void Init(){PCB * ch1block,* ch2block,* co1block,* co2block,* co3block,* d1block,* d2block,* d3block,* d4block;COCT * CO1,* CO2,* CO3;DCT *d1,*d2,*d3,*d4;SDT *s1,*s2,*s3,*s4;//=================================================run=(PCB *)malloc(sizeof(PCB));run->next=NULL;CH1=(CHCT *)malloc(sizeof(CHCT));CH2=(CHCT *)malloc(sizeof(CHCT));strcpy(CH1->identify,"ch1");strcpy(CH2->identify,"ch2");CH1->state=0;CH2->state=0;ch1block=(PCB *)malloc(sizeof(PCB));ch1block->next=NULL;ch2block=(PCB *)malloc(sizeof(PCB));ch2block->next=NULL;CH1->blocking=ch1block;CH2->blocking=ch2block;cohead=(COCT *)malloc(sizeof(COCT));cohead->next=NULL;CO1=(COCT *)malloc(sizeof(COCT));cohead->next=CO1;CO1->next=NULL;CO2=(COCT *)malloc(sizeof(COCT));CO1->next=CO2;CO2->next=NULL;CO3=(COCT *)malloc(sizeof(COCT));CO2->next=CO3;CO3->next=NULL;CO1->state=0;CO2->state=0;CO3->state=0;co1block=(PCB *)malloc(sizeof(PCB));co1block->next=NULL;co2block=(PCB *)malloc(sizeof(PCB));co2block->next=NULL;co3block=(PCB *)malloc(sizeof(PCB));co3block->next=NULL;strcpy(CO1->identify,"co1");strcpy(CO2->identify,"co2");strcpy(CO3->identify,"co3");CO1->chct=CH1;CO2->chct=CH1;CO3->chct=CH2;CO1->blocking=co1block;CO2->blocking=co2block;CO3->blocking=co3block;//===================================================== d1block=(PCB *)malloc(sizeof(PCB));d2block=(PCB *)malloc(sizeof(PCB));d3block=(PCB *)malloc(sizeof(PCB));d4block=(PCB *)malloc(sizeof(PCB));d1block->next=NULL;d2block->next=NULL;d3block->next=NULL;d4block->next=NULL;d1=(DCT *)malloc(sizeof(DCT));strcpy(d1->identify,"P");d2=(DCT *)malloc(sizeof(DCT));strcpy(d2->identify,"T");d3=(DCT *)malloc(sizeof(DCT));strcpy(d3->identify,"K");d4=(DCT *)malloc(sizeof(DCT));strcpy(d4->identify,"M");d1->coct=CO1;d2->coct=CO2;d3->coct=CO3;d4->coct=CO3;d1->state=0;d2->state=0;d3->state=0;d4->state=0;d1->type='o';d2->type='o';d3->type='i';d4->type='i';d1->blocking=d1block;d2->blocking=d2block;d3->blocking=d3block;d4->blocking=d4block;//================================head=(SDT *)malloc(sizeof(SDT));head->next=NULL;s1=(SDT *)malloc(sizeof(SDT));s2=(SDT *)malloc(sizeof(SDT));s3=(SDT *)malloc(sizeof(SDT));s4=(SDT *)malloc(sizeof(SDT));head->next=s1;s1->next=s2;s2->next=s3;s3->next=s4;s4->next=NULL;s1->dct=d1;s2->dct=d2;s3->dct=d3;s4->dct=d4;strcpy(s1->identify,"P");strcpy(s2->identify,"T");strcpy(s3->identify,"K");strcpy(s4->identify,"M");s1->type='o';s2->type='o';s3->type='i';s4->type='i';//testSDT *temp=head->next;}//============================================ //添加设备时候,添加新控制器,把控制器加到最后void Addcotrol (COCT *temp){COCT *cotemp=cohead;while(cotemp->next!=NULL){cotemp=cotemp->next;}cotemp->next=temp;temp->next=NULL;}//查看所有控制器,选择所要连接的控制器void Showallco(){COCT *temp=cohead->next;while(temp!=NULL){cout<<temp->identify<<" ";temp=temp->next;}cout<<endl;}//查找要连接的控制器COCT * Findco(char a[]){COCT * temp=cohead->next;while(temp!=NULL){if(!strcmp(temp->identify,a)){return temp;}temp=temp->next;}return temp;}//删除设备时候,判断是不是同时删除控制器,等于1删,0不删int sf_deleteco(char a[],char b[]){SDT *temp;temp=head->next;while(temp!=NULL){if((strcmp(temp->identify,a))&&((!strcmp(temp->dct->coct->identify,b)))) {return 0;}temp=temp->next;}return 1;}//删除设备的时候同时删除控制器void Deletecotrol(COCT *te){COCT * temp=cohead;while(temp->next!=te){temp=temp->next;}temp->next=te->next;delete(te);}//添加设备,查找设备是不是已经存在int sf_exist(char a[]){SDT *temp;if(head->next==NULL){return 0;}else{temp=head->next;while(temp!=NULL){if(!strcmp(a,temp->identify)){checkdel=temp;return 1;}temp=temp->next;}return 0;}}//申请设备时候,如果忙,将设备挂到等待队列void Addwaitpcb(PCB * p1,PCB * p2){PCB *temp=p1;while(temp->next!=NULL){temp=temp->next;}temp->next=p2;p2->next=NULL;}//回收设备时候,对PCB的操作void Deletepcb(char a[]){PCB * temp2=run->next,* temp=run;while(temp2!=NULL){if(!strcmp(temp2->devname,a)){temp->next=temp2->next;delete(temp2);break;}temp=temp2;temp2=temp->next;}}//判断等待队列是不是空int sf_pcbnull(PCB *temp){if(temp->next==NULL){return 0;}else return 1;}//查看所有设备状态void Showmenu(){PCB * ptemp=run->next;SDT * temp=head->next;DCT * dtemp;COCT * cotemp;CHCT * chtemp;cout<<"SDT"<<" i/o"<<" COCT"<<" CHCT"<<endl;while(temp!=NULL){dtemp=temp->dct;cotemp=dtemp->coct;chtemp=cotemp->chct;cout<<dtemp->identify<<"["<<dtemp->state<<"]"<<"\t"<<dtemp->type<<"\t"<<cote mp->identify<<"["<<cotemp->state<<"]"<<"\t"<<chtemp->identify<<"["<<chtemp->state<<"]"<<endl;temp=temp->next;}while(ptemp!=NULL){cout<<"进程"<<ptemp->name<<"申请了设备"<<ptemp->devname<<endl;ptemp=ptemp->next;}cout<<endl;}//设备独立性时候查找要查找的类型是不是存在int sf_typeexist(char ch){SDT *temp;if(head->next==NULL){return 0;}else{temp=head->next;while(temp!=NULL){if(temp->type==ch){return 1;}else temp=temp->next;}}return 0;}//增加设备void Adddevice(){cout<<"增加设备"<<endl;SDT * stemp,* s2temp;COCT *cotemp;DCT *temp;char temptype;PCB *tempblock,* cotempblock;char choice;char eqary[10],coary[10];cin>>eqary;//此处查找SDT,看是否所要添加的设备已经存在if(sf_exist(eqary)==1){//设备已经存在cout<<"设备已存在"<<endl;}else{//设备不存在tempblock=(PCB *)malloc(sizeof(PCB));tempblock->next=NULL;temp=(DCT *)malloc(sizeof(DCT));strcpy(temp->identify,eqary);temp->blocking=tempblock;temp->state=0;cout<<"输入设备类型"<<endl;cin>>temptype;temp->type=temptype;stemp=(SDT *)malloc(sizeof(SDT));stemp->next=NULL;stemp->dct=temp;stemp->type=temptype;strcpy(stemp->identify,eqary);s2temp=head;while(s2temp->next!=NULL){s2temp=s2temp->next;}s2temp->next=stemp;stemp->next=NULL;cout<<"是否添加控制器?(y/n)"<<endl;cin>>choice;if((choice=='y')||(choice=='Y')){ //添加新控制器cout<<"输入控制器名称:"<<endl;cin>>coary;cotempblock=(PCB *)malloc(sizeof(PCB));cotempblock->next=NULL;cotemp=(COCT *)malloc(sizeof(COCT));cotemp->next=NULL;strcpy(cotemp->identify,coary);cotemp->state=0;cotemp->blocking=cotempblock;Addcotrol(cotemp);temp->coct=cotemp;cout<<"请选择连接的通道:1/2"<<endl;int i;cin>>i;if(i==1){cotemp->chct=CH1;}else{cotemp->chct=CH2;}}else{//不添加控制器Showallco();cout<<"输入连接控制器的名称:"<<endl;cin>>coary;cotemp=Findco(coary);temp->coct=cotemp;}cout<<"设备"<<eqary<<"添加成功!"<<endl;}}//删除设备void Deletedevice(){//删除设备的时候同时删除pcb Deletepcbcout<<"删除设备"<<endl;COCT * temp;SDT * stemp;char chary[10];char tempary[10];cin>>chary;if(sf_exist(chary)==0){cout<<"删除设备不存在!"<<endl;}else if(checkdel->dct->state==1){cout<<"设备正在使用无法删除"<<endl;}else{//sf_deleteco();Deletepcb(chary);strcpy(tempary,checkdel->dct->coct->identify);if(sf_deleteco(chary,tempary)){//删除控制器temp=checkdel->dct->coct;Deletecotrol(temp);}stemp=head;while(stemp->next!=checkdel){stemp=stemp->next;}stemp->next=checkdel->next;delete(checkdel);cout<<"设备"<<chary<<"删除成功!"<<endl;}}//申请设备void Applydevice(){cout<<"申请设备"<<endl;PCB *ptemp;DCT *dtemp;COCT *cotemp;CHCT *chtemp;char pname[10],eqname[10];cin>>pname>>eqname;if(sf_exist(eqname)==0){cout<<"设备不存在!"<<endl;}else{//checkdel(对应要申请设备的指针)ptemp=(PCB *)malloc(sizeof(PCB));strcpy(ptemp->name,pname);ptemp->next=NULL;dtemp=checkdel->dct;//====================此处是否要考虑状态的改变========================if(dtemp->state==1){//设备忙Addwaitpcb(dtemp->blocking,ptemp);cout<<"进程"<<ptemp->name<<"被挂在设备"<<dtemp->identify<<"的等待队列上!"<<endl;}else{//设备不忙cotemp=dtemp->coct;if(cotemp->state==1){//控制器忙Addwaitpcb(cotemp->blocking,ptemp);cout<<"进程"<<ptemp->name<<"被挂在控制器"<<cotemp->identify<<"的等待队列上!"<<endl;}else{//控制器不忙chtemp=cotemp->chct;if(chtemp->state==1){//通道忙Addwaitpcb(chtemp->blocking,ptemp);cout<<"进程"<<ptemp->name<<"被挂在通道"<<chtemp->identify<<"的等待队列上!"<<endl;}else{//通道不忙Addwaitpcb(run,ptemp);dtemp->state=1;cotemp->state=1;chtemp->state=1;strcpy(ptemp->devname,eqname);cout<<"进程"<<pname<<"申请设备"<<eqname<<"成功!"<<endl;}}}}}//回收设备void Recycledevice(){cout<<"回收设备"<<endl;// char ch;char eqname[10];DCT *dtemp;COCT *cotemp;CHCT *chtemp;PCB *pctemp1,*pctemp2;cin>>eqname;if(sf_exist(eqname)==0){//设备不存在cout<<"要回收的设备不存在!"<<endl;}else if(checkdel->dct->state==0){//设备存在,但是不需要回收cout<<"设备处于闲状态,不需要回收!"<<endl;}else{//需要回收Deletepcb(eqname);//dtemp=checkdel->dct;cotemp=dtemp->coct;chtemp=cotemp->chct;dtemp->state=0;cotemp->state=0;chtemp->state=0;if(sf_pcbnull(chtemp->blocking)){//如果通道等待队列不空pctemp1=chtemp->blocking;pctemp2=pctemp1->next;pctemp1->next=pctemp2->next;pctemp2->next=NULL;strcpy(pctemp2->devname,eqname);Addwaitpcb(run,pctemp2);dtemp->state=1;cotemp->state=1;chtemp->state=1;}else{if(sf_pcbnull(cotemp->blocking)){//如果控制器的等待队列不空pctemp1=cotemp->blocking;pctemp2=pctemp1->next;pctemp1->next=pctemp2->next;pctemp2->next=NULL;strcpy(pctemp2->devname,eqname);Addwaitpcb(run,pctemp2);dtemp->state=1;cotemp->state=1;chtemp->state=1;}else{if(sf_pcbnull(dtemp->blocking)){//如果设备的等待队列不空pctemp1=dtemp->blocking;pctemp2=pctemp1->next;pctemp1->next=pctemp2->next;pctemp2->next=NULL;strcpy(pctemp2->devname,eqname);Addwaitpcb(run,pctemp2);dtemp->state=1;cotemp->state=1;chtemp->state=1;}}}cout<<"设备"<<eqname<<"回收成功!"<<endl;}}//设备独立性=======void Independence(){cout<<"设备独立性--申请设备:"<<endl;//cout<<"申请设备"<<endl;char type;char pname[10];SDT * temp,* temp2;PCB *ptemp;DCT *dtemp;COCT *cotemp;CHCT *chtemp;cin>>pname>>type;if((type=='o')||(type=='i')){//=========还得考虑要申请的类型是不是存在============================ if(sf_typeexist(type)==0){cout<<"要申请的该类设备不存在!"<<endl;}else{temp=head->next;while(temp!=NULL){if((temp->type==type)&&(temp->dct->state==0)){//当设备类型相同,并且设备空闲temp2=temp;break;}else if((temp->type==type)){temp2=temp;}temp=temp->next;}sf_exist(temp2->identify);//================================================ =ptemp=(PCB *)malloc(sizeof(PCB));strcpy(ptemp->name,pname);ptemp->next=NULL;dtemp=checkdel->dct;//要用了这个函数才可以========================================================================== if(temp2->dct->state==0){//当设备不忙时候cotemp=dtemp->coct;if(cotemp->state==1){//控制器忙Addwaitpcb(cotemp->blocking,ptemp);cout<<"进程"<<ptemp->name<<"被挂在控制器"<<cotemp->identify<<"的等待队列上!"<<endl;}else{//控制器不忙chtemp=cotemp->chct;if(chtemp->state==1){//通道忙Addwaitpcb(chtemp->blocking,ptemp);cout<<"进程"<<ptemp->name<<"被挂在通道"<<chtemp->identify<<"的等待队列上!"<<endl;}else{//通道不忙Addwaitpcb(run,ptemp);dtemp->state=1;cotemp->state=1;chtemp->state=1;strcpy(ptemp->devname,temp2->identify);cout<<"进程"<<pname<<"申请设备"<<temp2->identify<<"成功!"<<endl;}}}else{//当设备忙时候Addwaitpcb(dtemp->blocking,ptemp);cout<<"进程"<<ptemp->name<<"被挂在设备"<<dtemp->identify<<"的等待队列上!"<<endl;}}}else{cout<<"输入设备的类型错误!"<<endl;}}//void Show(){cout<<"A :增加设备"<<endl;cout<<"D :删除设备"<<endl;cout<<"S :申请设备"<<endl;cout<<"H :回收设备"<<endl;cout<<"I :设备独立性"<<endl;cout<<"Q :退出"<<endl;}//int main(){ cout<<" 操作系统实验三 "<<endl; cout<<" "<<endl; cout<<"相关指令:"<<endl;Init();Show();char choice;while(true){Showmenu();cout<<"选择指令:"<<endl;cin>>choice;switch(choice){case 'a':case 'A':Adddevice();break;case 'd':case 'D':Deletedevice();break;case 's':case 'S':Applydevice();break;case 'h':case 'H':Recycledevice();break;case 'q':case 'Q':exit(0);case 'i':case 'I':Independence();break;default :cout<<"指令错误"<<endl;break;}}return 0;}。
实验设备管理系统设计C源代码

源代码#include <iostream>#include <cstdlib>#include <string>#include <fstream>#include <sstream>using namespace std;class Node //结点类,用于存放客户的各种信息{publiclong id; //实验设备编号string name; //名称string type //随便类型string suoshushiyanshi; //实验设备所属实验室string buytime; //购买时间string price; //价格string amount; //库存量string shengchanriqi; //生产日期string shiyongshouming; //使用寿命string weight; //重量Node *next; //指向下一结点的指针Node(long id=000,string nname="noname",string ntype="unkown".string nsuoshushiyanshi="unknow",string nbuytime="unknow",string nprice="unknow",string namount="unknow",string nshengchanriqi="unkown",string nshiyongshouming="unknow",string nweight="unknow");//构造函数,初始化变量};class Link//Link 类,把数据以链表的形式存储,链表的每个结点为一个Node对象{private:Node *head;//链表的头指针,为Node对象类型的指针public:Link();//构造函数,初始化变量bool link IsEmpty();//判断链表是否为空void linkInsert(Node *newnode);//向链表中插入新的结点bool linkDelete(int nid);//从链表中删除的结点void linkClear();//清空链表中数据void linkView();//查看链表中数据Node* linkFind(int nid);//在链表中查找id为nid结点,返回指向该结点的指针Node* getHead();//获取头指针};Node::Node(long nid,string nname,string ntype,string nsuoshushiyanshi,string nbuytime,string nprice,string namount,string nshengchanriqi,string nshiyongshouming,string nweight){id=nid;name=nname;type=ntypesuoshushiyanshi=nsuoshushiyanshi;buytime=nbuytime;price=nprice;amount=namount;shengchanriqi=nshengchanriqi;shiyongshouming=nshiyongshouming;weight= nweight;next=NULL;}Link::Link() //构造函数,初始化链表为空{head=NULL;}bool Link::linkIsEmpty() //判断链表是否为空,空则返回true{return(head==NULL);}void Link::linkInsert(Node *newnode)//按id值从小到大的顺序,插入新的结点{Node *pl;if(!head)//链表为空的情况head=newnode;else if((head->id)>(newnode->id))//插入到第一个结点的情况{newnode->next=head;head=newnode;}else//插入到第二个及以后情况{pl=head;while(1){if(!(pl->next)){pl->next=newnode;break;}else if((pl->next)->id>newnode->id){newnode->next=pl ->next;pl ->next=newnode;break;}pl=pl ->next;}}}bool Link::linkDelete(int nid)//删除结点,成功则返回true {Node *p;if(head==NULL)//链表为空的情况return false;if(head->id==nid)//删除的为第一个结点的情况{head=head->next;return true}p=head;while(p->next)//删除的为第二个及以后的情况{if((p->next)->id==nid){p->next=(p->next)->next;return true;}p=p->next;}return false;}void Link::linkClear()//清空链表{head=NULL;}Node* Link::linkFind(int nid)//查找id 值为nid的结点,返回该结的的指针{Node *p=head; //没找到符合条件的结点的话,返回的指针值为NULL while(p){if(p->id==nid)break;elsep=p->next;}return p;}void Link::linkView()//显示链表数据{Node *p=head;while(p){cout<<"实验设备编号:"<<p->id<<endl;cout<<"名称:"<<p->name<<endl;cout<<"类型:"<<p->type<<endl;cout<<"所属实验室:"<<p->suoshushiyanshi<<endl;cout<<"购买时间:"<<p->buytime<<endl;cout<<"价格:"<<p->price<<endl;cout<<"库存量:"<<p->amount<<endl;cout<<"生产日期:"<<p->shenchanriqi<<endl;cout<<"使用寿命:"<<p->shiyongshouming<<endl;cout<<"重量:"<<p->weight<<endl;cout<<"*******************************************************"<<endl;p=p->next;}}Node* Link::getHead(){return head;}//类Shebei,用于包装Link类及菜单操作class Shebei{private:Link clink;//Link 对象成员bool k;//记录数据是否被修改public:Shebei();void ShebeiClear();void ShebeiInsert();void ShebeiDelete();void ShebeiFind();void ShebeiModify();void ShebeiView();void ShebeiLoad();void ShebeiSave();void Exit();//退出程序void showMenu();//显示菜单void showTip();//显示操作提示void doMenu(char n);//执行相应菜单项功能};Shebei:: Shebei(){Link();//调用成员对象的构造函数ShebeiLoad();从文件中读取数据,创建链表k=false;//设置数据被修改}void Shebei::ShebeiClear()//清空设备记录{clink.linkClear();cout<<"成功清空设备信息记录!"<<endl;}void Shebei::ShebeiInsert()//插入设备记录{Node *p=new Node();cout<<"请输入设备编号:";cin>>p->id;cout<<"请输入名称:";cin>>p->name;cout<<"请输入类型:";cin>>p->type;cout<<"请输入所属实验室:";cin>>p->suoshushiyanshi;cout<<"请输入购买时间:";cin>>p->buytime;cout<<"请输入价格:";cin>>p->price;cout<<"请输入库存量:";cin>>p->amount;cout<<"请输入生产日期:";cin>>p->shengchanriqi;cout<<"请输入设备重量:";cin>>p->weight;if(!clink.linkFind(p->id))判断设备信息是否存在{clink.linkInsert(p);k=true;}elsecout<<"设备编号为"<<p->id<<"的设备已存在,插入失败! "<<endl; }void Shebei::ShebeiDelete()//删除设备记录{long i;if(clink.linkIsEmpty())//链表为空的情况cout<<"没有设备记录! "<<endl;else{cout<<"请输入设备编号:";cin>>i;if(clink.linkDelete(i)){cout<<"成功删除编号为"<<i<<"的实验记录!"<<endl;k=true;}elsecout<<"没有找到编号为"<<i<<"的设备!"<<endl;}}void Shebei::ShebeiFind()//查找某编号的设备记录{long n;Node *p;if(clink.linkIsEmpty())//链表为空的情况cout<<"没有设备记录!"<<endl;else{cout<<"请输入要查找的设备的编号:";cin>>n;p=clink.linkFind(n);//获得找到的结点的指针if(p)//指针值不为NULL时{cout<<"实验设备编号:"<<p->id<<endl;cout<<"名称:"<<p->name<<endl;cout<<"类型:"<<p->type<<endl;cout<<"所属实验室:"<<p->suoshushiyanshi<<endl;cout<<"购买时间:"<<p->buytime<<endl;cout<<"价格:"<<p->price<<endl;cout<<"库存量:"<<p->amount<<endl;cout<<"生产日期:"<<p->shengchanriqi<<endl;cout<<"使用寿命:"<<p->shiyongshouming<<endl;cout<<"重量:"<<p->weight<<endl;}else//指针值为NULL时cout<<"没有找到编号为"<<n<<"的设备记录!"<<endl;}}void Shebei::ShebeiModify()//修改某编号的设备信息{long n;Node *p;if(clink.linkIsEmpty())//链表为空的情况cout<<"没有设备记录!"<<endl;else{cout<<"请输入要修改的设备的编号:";cin>>n;p=clink.linkFind(n);//获得找到的结点的指针if(p)//指针值不为NULL时{cout<<"实验设备编号:"<<p->id<<endl;cout<<"名称:"<<p->name<<endl;cout<<"类型:"<<p->type<<endl;cout<<"所属实验室:"<<p->suoshushiyanshi<<endl;cout<<"购买时间:"<<p->buytime<<endl;cout<<"价格:"<<p->price<<endl;cout<<"库存量:"<<p->amount<<endl;cout<<"生产日期:"<<p->shengchanriqi<<endl;cout<<"使用寿命:"<<p->shiyongshouming<<endl;cout<<"重量:"<<p->weight<<endl;cout<<"请输入新的设备信息"<<endl;cout<<"请输入设备编号:";cin>>p->id;cout<<"请输入名称:";cin>>p->name;cout<<"请输入类型:";cin>>p->type;cout<<"请输入所属实验室:";cin>>p->suoshushiyanshi;cout<<"请输入购买时间:";cin>>p->buytime;cout<<"请输入价格:";cin>>p->price;cout<<"请输入库存量:";cin>>p->amount;cout<<"请输入生产日期:";cin>>p->shengchanriqi;cout<<"请输入设备重量:";cin>>p->weight;}else//指针值为NULL时cout<<"没有找到编号为"<<n<<"的设备记录! "<<endl;}}void Shebei::ShebeiView()//显示所有设备的记录{if(clink.linkIsEmpty())cout<<"没有设备记录! "<<endl;elseclink.linkView();}void Shebei::ShebeiLoad()//从文件中读入数据,创建链表{Node *p;long nid;ifstream infile("data.txt");if(!infile)cerr<<"错误:数据文件不能打开!\n";elsewhile(infile>>nid)//读取客户,直到读空{p=new Node();p->id=nid;infile>>p->name>>" "; //读取名称infile>>p->type>>" ";infile>>p->suoshushiyanshi>>" ";infile>>p->buytime>>" ";infile>>p->price>>" ";infile>>p->amount>>" ";infile>>p->shengchanriqi>>" ";infile>>p->weight>>" ";clink.linkInsert(p);}infile.close(); //关闭文件}void Shebei::ShebeiSave()//将数据保存到文件{Node *p=clink.getHead();ofstream outfile("data.txt");if(!outfile)cerr<<"错误:数据文件不能打开!\n";else{while(p){outfile<<p->id<<" "<<p->name<<" "<<p->type<<""<<p->suoshushiyanshi<<" "<<p->buytime<<" "<<p->price<<" "<<p->amount<<" "<<p->shengchanriqi<<" "<<p->weight<<" "<<endl;p=p->next;}k=false;cout<<"保存成功!"<<endl;}}void Shebei::Exit(){char s='Y';if(k)//判断数据是否修改{cout<<"数据已经改变,是否保存?(y/n):"cin>>s;if(s=='Y'lls=='Y')ShebeiSave();}cout<<"已安全退出,";system("pause");exit(0);}void Shebei::showMenu()cout<<"\t\t*********************************************"<<endl; cout<<"\t\t* 实验室设备管理系统*"<<endl;cout<<"\t\t* *"endl;cout<<"\t\t* 1--清除所有设备信息*"endl;cout<<"\t\t* *"endl;cout<<"\t\t* 2--添加:增加一个设备到设备信息中*"endl;cout<<"\t\t* *"endl;cout<<"\t\t* 3--删除:删除指定设备数据*"endl;cout<<"\t\t* *"endl;cout<<"\t\t* 4--查找:根据设备数据查找设备信息*"endl;cout<<"\t\t* *"endl;cout<<"\t\t* 5--修改:修改指定设备信息*"endl; cout<<"\t\t* *"endl; cout<<"\t\t* 6--显示:在屏幕上显示所有设备的消息*"endl; cout<<"\t\t* *"endl; cout<<"\t\t* 7--存储:将设备信息保存在一个文件中*"endl; cout<<"\t\t* *"endl; cout<<"\t\t* 0--退出:安全的退出本系统*"endl;cout<<"\t\t********************************************"endl;}void Shebei::showTip(){cout<<endl;cout<<"*****************操作完成***************"<<endl;cout<<"***************选择0--7继续************"<<endl;cout<<"请选择:";}void Shebei::doMenu(char n){switch(n){case'1':ShebeiClear();break;case'2':ShebeiInsert();break;case'3':ShebeiDelete();break;case'4':ShebeiFind();break;case'5':ShebeiModify();break;case'6':ShebeiView();break;case'7':ShebeiSave();break;case'0':Exit();break;default:cout<<"输入错误!";}}int main(){Shebei c;char i='9';c.showMenu();cout<<"欢迎来到实验室设备管理系统:"<<endl;cout<<"请选择你需要的服务:";while(1){cin>>i;system("cls"); //清除屏幕c.showMenu();c.doMenu(i);c.showTip();}return 0;}。
操作系统课程设计(设备管理)

操作系统课程设计(设备管理)目录一引言 (1)1.1课程设计题目 (1)1.2课程设计的目的 (1)1.3小组人数 (1)1.4编程语言 (1)1.5课程设计内容 (1)1.6界面设计如图 (1)二课程设计任务及要求 (2)2.1设计任务 (2)2.2设计要求 (2)三算法及数据结构 (2)3.1算法的总体思想(流程) (2)3.2 Equipment模块 (3)3.2.1 功能 (3)3.2.2 数据结构 (3)3.3 cpu模块 (4)3.3.1 功能 (4)3.3.2 数据结构 (4)3.3.3 算法 (4)3.4 form1模块 (4)3.4.1 功能 (4)3.4.2 算法 (5)四程序设计与实现 (5)4.1 程序流程图 (5)4.2 基本思想 (6)4.3 定义的公共变量或数据结构 (7)4.4 实验部分代码 (7)4.5 运行截图 (12)4.6 使用说明 (15)五总结 (15)六参考文献 (15)一引言1.1课程设计题目实现一个模拟操作系统。
1.2课程设计的目的通过模拟操作系统原理的实现,加深对操作系统工作原理理解,进一步了解操作系统的实现方法,并可练习合作完成系统的团队精神和提高程序设计能力。
1.3小组人数建议3~4人一组共同完成模拟系统的实现。
1.4编程语言建议使用VC、VB、C#、Java等Windows环境下的程序设计语言,以借助这些语言环境来模拟硬件的一些并行工作。
1.5课程设计内容模拟采用多道程序设计方法的单用户操作系统,该操作系统包括进程管理、存储管理、设备管理、文件管理和用户接口四部分。
1.6界面设计如图图1.1二课程设计任务及要求2.1设计任务设计一个设备管理分配程序,按先来先服务的算法,对设备进行分配。
2.2设计要求设备管理主要包括设备的分配和回收。
(1)模拟系统中有A、B、C三种独占型设备,A设备3个,B设备2个,C设备1个。
(2)因为模拟系统比较小,因此只要设备表设计合理即可。
设备管理系统源代码及运行界面

源代码及运行界面1.model类a.Depart.csusing System。
using System.Collections.Generic。
using System.Linq。
using System.Text。
namespace model{public class Depart{public Depart(){ }#region modelprivate int _departid。
private string _departname。
private int _parentid。
public int DepartID{set {_departid = value。
}get { return _departid。
}}public string DepartName{set { _departname = value。
}get { return _departname。
}}public int ParentID{set { _parentid = value。
}get { return _parentid。
}}#endregion model}}b.Device.csusing System。
using System.Collections.Generic。
using System.Linq。
using System.Text。
namespace model{public class Device{public Device(){ }#region modelprivate int _deviceid。
private string _devicetypeid。
private int _typeid。
private int _classid。
private string _devicestate。
private Single _price。
private int _useofyear。
操作系统设备管理代码

实验报告课程名称操作系统实验项目设备管理专业计算机科学与技术班级计科4班姓名秦宇学号******** 指导教师于桂玲实验成绩2012年6月13日实验5 设备管理一、实验目的理解设备管理的概念和任务,掌握独占设备的分配、回收等主要算法的原理并编程实现。
二、实验内容编写程序实现对独占设备的分配与回收的模拟。
三、实验要求1、实现设备分配、回收、显示系统中设备信息的功能。
2、通过设备类表和设备表记录系统中设备信息、以便进行设备分配。
3、设备类表记录系统中全部设备的情况,每个设备类占一个表目,设备类表的数据结构如表1所示。
4、为每一个设备配置一张设备控制表,用于记录本设备的情况。
设备控制表的数据结构如图2所示。
4、程序中建立分配设备和回收设备函数。
5、设系统有3类设备,每类设备的设备数分别为2、3、4。
6、要求键盘输入作业名、作业所需设备类和设备相对号。
程序代码:#include "iostream"#include "string"#include "vector"using namespace std;typedef struct node{string ID; //进程名string equipment; //申请的设备名struct node *next;}PCB;typedef struct{string channelID; //通道标识符bool state; //通道状态PCB *use; //正在使用该通道的进程 PCB *blockqueue; //阻塞队首}CHCT;typedef struct{string controllerID; //控制器标示bool state; //控制器状态CHCT *front; //通道表指针PCB *use; //正在使用该控制器的进程 PCB *blockqueue; //阻塞队首}COCT;typedef struct{char type; //设备类型string equipmentID; //设备名bool state; //设备状态COCT *front; //控制器指针PCB *use; //正在使用该设备的进程 PCB *blockqueue; //阻塞队首}DCT;typedef struct{char type; //设备类型string equipmentID; //设备名DCT *dct; //设备的DCT}SDT;DCT *k=new DCT; //键盘的DCTDCT *m=new DCT;DCT *p=new DCT;DCT *t=new DCT;COCT *c1=new COCT;COCT *c2=new COCT;COCT *c3=new COCT;CHCT *h1=new CHCT;CHCT *h2=new CHCT;CHCT *h3=new CHCT;int check(char cmd){switch(cmd){case 'c': //申请return 1;case 'C':return 1;case 'd': //删除return 2;case 'D':return 2;case 'a': //添加return 3;case 'A':return 3;case 'f': //释放return 4;case 'F':return 4;case 'q':return -1;case 'Q':return -1;case 'p':return 5;case 'P':return 5;default:return 0;}}void init(vector<SDT> &SDT_table){SDT_table[0].equipmentID="M";SDT_table[0].type='2'; //鼠标是第二类设备 SDT_table[0].dct=m; //设备的DCT表位置SDT_table[1].equipmentID="K";SDT_table[1].type='1';SDT_table[1].dct=k;SDT_table[2].equipmentID="P"; SDT_table[2].type='3';SDT_table[2].dct=p;SDT_table[3].equipmentID="T"; SDT_table[3].type='4';SDT_table[3].dct=t;h1->blockqueue=NULL;h1->channelID="通道1";h1->state=true;h1->use=NULL;h2->blockqueue=NULL;h2->channelID="通道2";h2->state=true;h2->use=NULL;c1->blockqueue=NULL;c1->controllerID="控制器1"; c1->state=true;c1->front=h1;c1->use=NULL;c2->blockqueue=NULL;c2->controllerID="控制器2"; c2->state=true;c2->front=h1;c2->use=NULL;c3->blockqueue=NULL;c3->controllerID="控制器3"; c3->state=true;c3->front=h2;c3->use=NULL;k->blockqueue=NULL;k->equipmentID="K";k->state=true; //可用k->type='1';k->front=c1;k->use=NULL;m->blockqueue=NULL;m->equipmentID="M";m->state=true;m->type='2';m->front=c1;m->use=NULL;p->blockqueue=NULL;p->equipmentID="P";p->state=true;p->type='3';p->front=c2;p->use=NULL;t->blockqueue=NULL;t->equipmentID="T";t->state=true;t->type='4';t->front=c3;t->use=NULL;}int main(){char cmd;DCT *temp_dct;COCT *temp_coct;CHCT *temp_chct;int l=0;string ID;string name;vector<SDT> SDT_table(4); //设备表vector<DCT> DCT_table(4); //设备vector<COCT> COCT_table(3); //控制器表// vector<SDT>::size_type size_SDT_table; //设备表长度init(SDT_table); //设备表的初始化DCT u=*k;DCT_table.push_back(u);u=*m;DCT_table.push_back(u);u=*p;DCT_table.push_back(u);u=*t;DCT_table.push_back(u);COCT cu=*c1;COCT_table.push_back(cu);cu=*c2;COCT_table.push_back(cu);cu=*c3;COCT_table.push_back(cu);cout<<"目前设备:K:键盘(1) M:鼠标(2) P:打印机(3) T:显示器(4)"<<endl; cout<<"c 申请使用设备。
操作系统文件系统实验报告后附源代码

目录1 课程设计简介 (1)1.1 课程设计的目的 (1)1.2 课程设计内容 (1)2 数据结构的设计 (2)2.1 预定义 (2)2.2 结构体 (2)2.3 全局变量和函数 (2)3 功能模块(或算法)描述 (5)3.1 模块划分 (4)3.2 模块流程图 (6)4 程序运行结果 (8)5心得体会 (9)参考文献 (10)附源代码 (11)1 课程设计简介1.1 课程设计的目的课程设计目的使学生熟悉文件管理系统的设计方法;加深对所学各种文件操作的了解及其操作方法的特点。
通过模拟文件系统的实现,深入理解操作系统中文件系统的理论知识, 加深对教材中的重要算法的理解。
同时通过编程实现这些算法,更好地掌握操作系统的原理及实现方法,提高综合运用各专业课知识的能力。
1.2 课程设计内容课程设计内容设计一个简单的多用户文件系统。
即①在系统中用一个文件来模拟一个磁盘;②此系统至少有:Create、delete、open、close、read、write等和部分文件属性的功能。
③实现这个文件系统。
④能实际演示这个文件系统。
基本上是进入一个界面(此界面就是该文件系统的界面)后,可以实现设计的操作要求。
1)设计一个10个用户的文件系统,每次用户可保存10个文件,一次运行用户可以打开5个文件。
2)程序采用二级文件目录(即设置主目录MFD)和用户文件目录(UFD)。
另外,为打开文件设置了运行文件目录(AFD)。
3)为了便于实现,对文件的读写作了简化,在执行读写命令时,只需改读写指针,并不进行实际的读写操作。
4)因系统小,文件目录的检索使用了简单的线性搜索。
5)文件保护简单使用了三位保护码:允许读写执行、对应位为1,对应位为0,则表示不允许读写、执行。
6)程序中使用的主要设计结构如下:主文件目录和用户文件目录(MFD、UFD),打开文件目录(AFD)即运行文件目录,如图5.1所示。
2 数据结构的设计2.1 预定义#define BLOCKSIZ 512 //磁盘块的大小#define DA TABLKNUM 512 //数据块的数目#define BLKGRUPNUM 50 //数据块组包含多少数据块#define P_N_BLOCKS 15 //inode节点中指向数据块的指针个数#define GROUPNUM DATABLKNUM/BLKGRUPNUM+1 //数据块组组数#define DINODESIZ 512 //磁盘i结点区的大小(空间32×512)#define DINODENUM 32 //磁盘i结点区的块数#define SYSOPENFILE 40#define DIRNUM 32 //一个目录下的最多目录和文件的总和数#define DIRSIZ 14 //文件、目录名的长度(字节)#define UPWDSIZ 15 //密码的长度#define UNAMSIZ 15 //用户名的长度#define PWDSIZ sizeof(struct pwd) //密码结构的长度#define PWDNUM BLOCKSIZ/PWDSIZ //密码数据空间的大小(pwd为单位)#define NOFILE 20 //一个用户最多可以打开的文件数目#define DINODESTART 4*BLOCKSIZ//i结点区的开始地址-inodes table ,1引导2超块3block bitmap 4inode bitmap#define DA TASTART (2+DINODENUM)*BLOCKSIZ //数据区的开始地址#define DATASTARTNO 36 //数据区开始指针#define DIMODE_EMPTY 00000/*可以用的空间*/#define DIMODE_FILE 00001#define DIMODE_DIR 00002#define DIMODE_PASSWD 00004#define GRUP_0 0 //管理员组#define GRUP_1 1#define GRUP_2 2#define GRUP_4 42.2 结构体//磁盘i结点结构,struct inode{// char di_name[DIRSIZ];unsigned __int16 di_ino; /*磁盘i节点标识*/unsigned __int16 di_number; /*关联文件数,当为0时表示删除文件*/unsigned __int16 di_mode; /*存取权限*/unsigned __int16 di_uid; /*磁盘i节点用户id*/unsigned __int16 di_gid; /*磁盘i节点权限组id*/ //1管理员组2用户组unsigned __int32 di_size; /*文件大小*/unsigned __int32 di_ctime; /* Creation time */unsigned __int32 di_mtime; /* Modification time */unsigned __int16 di_block[P_N_BLOCKS]; /* 一组block 指针*/};// 目录项结构struct direct{char d_name[DIRSIZ]; /*目录名(14字节)*/__int16 d_ino; /*目录号*/};//超级快结构struct super_block{unsigned __int16 s_inodes_count; /* inodes 计数*/unsigned __int16 s_blocks_count; /* blocks 计数*/unsigned __int16 s_r_blocks_count; /* 保留的blocks 计数*/unsigned __int16 s_free_blocks_count; // 空闲的blocks 计数unsigned __int16 s_free_inodes_count; /* 空闲的inodes 计数*/unsigned __int16 s_free_blocks_group[GROUPNUM];//新增一个数组来记录每个数据块组中的空闲数据块计数unsigned __int16 s_first_data_block; /* 第一个数据block */unsigned __int16 s_log_block_size; /* block 的大小*/unsigned __int16 s_blocks_per_group; /* 每block group 的block 数量*/unsigned __int16 s_inodes_per_group; /* 每block group 的inode 数量*/};// 用户密码struct pwd{unsigned __int8 p_uid;unsigned __int8 p_gid;char username[UNAMSIZ];/*用户名新加的*/char password[UPWDSIZ];};// 目录结构struct dir{struct direct direct[DIRNUM];__int16 size;};2.3 全局变量和函数// 全局变量unsigned __int8 di_bitmap[DINODENUM]; // 硬盘inode节点位图1表示已使用0表示未使用unsigned __int8 bk_bitmap[DA TABLKNUM]; // 数据块block位图struct super_block filsys; //超级块struct pwd pwd[PWDNUM];FILE *fd; //文件指针struct inode *cur_inode; //i节点当前目录指针struct inode *inodetemp; //i节点指针const char fsystemname[20]="Linux.EXT2"; //模拟硬盘的文件名struct direct dir_buf[BLOCKSIZ / sizeof(struct direct)]; //目录数组char cmdhead[20];//cmd 的头表示所在哪个文件夹、int i_lock=0;//inode位图锁可能会多线程int b_lock=0;//block位图锁struct pwd *cur_user;/* 全局函数*/extern int Format();//格式化磁盘extern int Install();//启动,安装文件系统struct inode * read_inode(int);//install里面读取文件dinodestruct direct * read_dir_data(int);//读取存储文件夹的物理块extern void showdir();//命令dirint Enterdir(char[]);//进入某个文件夹命令-- cd 文件名int Fd_dirfile(char[]);//查找当前目录里的文件没找到返回-1 找到返回inode号int Iscmd(char[]);//判断是否两个字符串的命令void two_cmd(char[],char[]);//两个字符串的命令int creat(char[]);//创建文件void changeinode();//交换指针char * ReadFile(char[]);//读取文件int mkdir(char[]);//创建文件夹void showbitmap();//显示位图int deletefd(char[]);//删除文件int editfile(char[]);//编辑文件int rename(char[]);//重命名void showhelp();//命令帮助void login();void logout();int access();//权限判断/*磁盘i节点的分配与释放(当一个新文件被建立的时候,在给该文件分配磁盘存储区之前,应为该文件分配存放该文件说明信息的磁盘i节点,当从文件系统中删除某个文件时,应首先删除它的磁盘i节点项。
操作系统课设+代码

}
}
cout<<"请输入用户密码:";
cin>>pass;
cout<<"请输入用户权限(0普通用户,1管理员):";
cin>>pride;
strcpy(,name); //将新用户姓名复制到文件
strcpy(newuser.pass,pass); //将新用户通过的限权复制到文件
⑤读文件
文件打开后可顺序读文件中的记录,读长度表示本次操作需读的记录个数。
⑥写文件
执行写文件操作时要区分两种情况,第一种是在执行create后要求写,第二种是执行open后要求写。对第二种情况可认为用户要对一个已建立好的文件进行修改。一个文件可以分多次写,既可按记录顺序写也可随机写一个记录,采用顺序写时可省略记录号。
4.打开文件
5.关闭文件
6.当前路径
六、设计体会
第一、操作系统这次课程设计让我学习到了很多计算机方面的知识,在一定程度上对以前的所学知识进一步加深,更让我有了一个系统的了解。操作系统是计算机的软件的系统的基础,具有一定平台性。掌握好这门课程对我们以后学习计算机知识有一个很大的帮助。
第二、本次课程设计让我明白了编写程序一定要动手去写代码,进行实际操作方可进取得一定性的进步,还要进行与组内人员之间的团结与协作。共同学习,共同克服困难,取得共同进步。
};
struct findex *fidx=NULL;//全局变量,文件索引链
//定义目录表项
struct dreitem
{
char name[25];//目录或者文件名称
int share; //共享数
int parent;//上层目录
操作系统设备管理代码

#include<iostream>#include<string>#include<vector>#include<iomanip>using namespace std;struct coct;typedef struct pcb{int type; //设备类型string name; //设备名string usename; //进程名pcb *next;} pcb;typedef struct chct //通道{string name; //使用进程名int type; //标示bool busy;vector<coct*> COCThand; //COCT 指针pcb *CHCTwaithand; //阻塞队列pcb *CHCTwaitbotton;vector<pcb*> CHCTwait;chct *next;} chct;typedef struct coct //控制表{string name; //使用进程名int type; //控制器标识bool busy;chct *CHCThand;pcb *COCTwaithand;pcb *COCTwaitbooton;vector<pcb*> COCTwait;coct *next;} coct;typedef struct dct //设备控制表{int type; //类string name; //设备名string usename; //使用进程名bool wait;bool busy;coct *COCThand; //指向COCTpcb *DCTwaithand; //阻塞队列pcb *DCTwaitbotton;vector<pcb*> DCTwait;dct *next;} dct;typedef struct sdt//系统设备表{int type; //类型string name; //设备名dct *DCThand; //DCTsdt *next;} sdt;template <class Type>class Stack{private:Type *stackhand;Type *stackbotton;int num;public:Stack();bool isempty(); //Type* gethand(); //头指针bool pop_hand(); //头出void push_back(Type *t); //后入Type* getat(int n); //类型查找Type* getat(string name); //名字查找Type* returnat(int n); //返回N的对象bool deleteat(string name); //删除int getnum();void show();};template <class Type>Stack<Type>::Stack(){num=0;Type *t=new Type;stackhand=stackbotton=t;stackhand->next=NULL;stackbotton->next=NULL;}template <class Type>int Stack<Type>::getnum(){return num;}template <class Type>Type* Stack<Type>::returnat(int n){Type *t=NULL;if(n>num) return t;t=stackhand;while(n--!=0){t=t->next;}return t;}template <class Type>Type* Stack<Type>::getat(int n){Type *t=stackhand->next;while(t){if(t->type==n)return t;t=t->next;}return t;}template <class Type>Type* Stack<Type>::getat(string name) {Type *t=stackhand->next;while(t){if(t->pare(name)==0)return t;t=t->next;}return t;}template <class Type>bool Stack<Type>::isempty(){return num==0;}template <class Type>Type* Stack<Type>::gethand(){if(num==0) cout<<"错误!头空"<<endl;return stackhand->next;}template <class Type>bool Stack<Type>::deleteat(string name){Type *s,*t=stackhand->next;s=stackhand;while(t){if(t->pare(name)==0){s->next=t->next;delete t;num--;return true;}s=t;t=t->next;}return false;}template <class Type>bool Stack<Type>::pop_hand(){if(num<=0){cout<<"错误!空!"<<endl;return false;}else{Type *t;t=stackhand->next;stackhand->next=t->next;delete t;num--;return true;}}template <class Type>void Stack<Type>::push_back(Type *t){stackbotton->next=t;num++;stackbotton=t;stackbotton->next=NULL;}template <class Type>void Stack<Type>::show(){Type *t=stackhand->next;while(t){cout<<setiosflags(ios::left)<<setw(14)<<t->name;cout<<setiosflags(ios::left)<<setw(14)<<t->type<<endl;t=t->next;}}class devicemanager{private:Stack<dct> DCT; // 控制表Stack<sdt> SDT; //设备表Stack<coct> COCT;Stack<chct> CHCT;public:devicemanager();bool in();void Nameapply(string usename,string name); //申请设备void Typeapply(string usename,int n); //申请类设备void Retdevice(string name); //返回设备void Deletedevice(string name); //删除设备void Adddevice(string name,int devicetype,int cocttype);void show();void help();};devicemanager::devicemanager():SDT(),COCT(),CHCT(),DCT() {sdt *s1=new sdt;sdt *s2=new sdt;sdt *s3=new sdt;sdt *s4=new sdt;dct *d1=new dct;dct *d2=new dct;dct *d3=new dct;dct *d4=new dct;coct *co1=new coct;coct *co2=new coct;coct *co3=new coct;chct *ch1=new chct;chct *ch2=new chct;s1->name="Mouse";s1->type=1;s1->DCThand=d1;SDT.push_back(s1);s2->type=1;s2->name="Keyboard";s2->DCThand=d2;SDT.push_back(s2);s3->type=0;s3->name="Displayer";s3->DCThand=d3;SDT.push_back(s3);s4->type=0;s4->name="Printer";s4->DCThand=d4;SDT.push_back(s4);//////////////////////////////////SDT初始化d1->type=s1->type;d1->name=s1->name;d1->busy=false;d1->wait=false;d1->usename="";d1->DCTwaithand=NULL;d1->DCTwaitbotton=NULL;d1->COCThand=co1;DCT.push_back(d1);d2->type=s2->type;d2->name=s2->name;d2->busy=false;d2->wait=false;d2->usename="";d2->DCTwaithand=NULL;d2->DCTwaitbotton=NULL;d2->COCThand=co1;DCT.push_back(d2);d3->type=s3->type;d3->name=s3->name;d3->busy=false;d3->wait=false;d3->usename="";d3->DCTwaithand=NULL;d3->DCTwaitbotton=NULL;d3->COCThand=co2;DCT.push_back(d3);d4->type=s4->type;d4->name=s4->name;d4->busy=false;d4->wait=false;d4->usename="";d4->DCTwaithand=NULL;d4->DCTwaitbotton=NULL;d4->COCThand=co3;DCT.push_back(d4);///////////////////////////////dct 初始化co1->type=1;co1->busy=false;co1->COCTwaithand=NULL; co1->COCTwaitbooton=NULL; co1->name="";co1->CHCThand=ch1; COCT.push_back(co1);co2->type=2;co2->busy=false;co2->COCTwaithand=NULL; co2->COCTwaitbooton=NULL; co2->name="";co2->CHCThand=ch2; COCT.push_back(co2);co3->type=3;co3->busy=false;co3->COCTwaithand=NULL;co3->COCTwaitbooton=NULL;co3->name="";co3->CHCThand=ch2;COCT.push_back(co3);/////////////////////////////COCT初始化ch1->type=1;ch1->name="";ch1->busy=false;ch1->CHCTwaithand=NULL;ch1->CHCTwaitbotton=NULL;ch1->COCThand.push_back(co1);CHCT.push_back(ch1);ch2->type=2;ch2->name="";ch2->busy=false;ch2->CHCTwaithand=NULL;ch2->CHCTwaitbotton=NULL;ch2->COCThand.push_back(co2);ch2->COCThand.push_back(co3);CHCT.push_back(ch2);/////////////////////////////CHCT初始化}void devicemanager::Nameapply(string usename,string name) {if(SDT.getat(name)==NULL){cout<<"无此设备:"<<name<<endl;return;}dct *t=SDT.getat(name)->DCThand;if(!(t->busy)){t->busy=true;t->usename=usename;if(!t->COCThand->busy){t->COCThand->name=usename;t->COCThand->busy=true;t->wait=true;if(!(t->COCThand->CHCThand->busy)){t->COCThand->CHCThand->name=usename;t->COCThand->CHCThand->busy=true;}else{pcb *p=new pcb;p->usename=usename;p->name=name;t->COCThand->CHCThand->CHCTwait.push_back(p);}}else{pcb *p=new pcb;p->usename=usename;p->name=name;t->COCThand->COCTwait.push_back(p);}}else{pcb *p=new pcb;p->usename=usename;p->name=name;t->DCTwait.push_back(p);}}void devicemanager::Typeapply(string usename,int n){if(SDT.getat(n)==NULL){cout<<"无此类设备!"<<endl;return ;}for(int i=0;i<SDT.getnum();i++){if(SDT.returnat(i+1)->type==n&&!SDT.returnat(i+1)->DCThand->busy) {Nameapply(usename,SDT.returnat(i+1)->name);return;}}Nameapply(usename,SDT.getat(n)->name);}void devicemanager::Adddevice(string name ,int devicetype,int cocttype) {sdt *s=new sdt;dct *d=new dct;SDT.push_back(s);DCT.push_back(d);s->name=name;s->type=devicetype;s->DCThand=d;d->name=name;d->type=devicetype;d->usename="";d->busy=false;d->wait=false;d->DCTwaithand=NULL;d->DCTwaitbotton=NULL;d->COCThand=COCT.getat(cocttype);}void devicemanager::Deletedevice(string name){if(!SDT.deleteat(name)){cout<<"SDT无此设备!"<<endl;}if(!DCT.deleteat(name)){cout<<"DCT无此设备!"<<endl;}}bool devicemanager::in(){cout<<">";string s;cin>>s;if(pare("-h")==0){help();return true;}else if(pare("-exit")==0){return false;}else if(pare("-add")==0)string t1;int t2,t3;cin>>t1;cin>>t2;cin>>t3;Adddevice(t1,t2,t3);return true;}else if(pare("-del")==0){string t;cin>>t;Deletedevice(t);return true;}else if(pare("-nameapply")==0) {string t1,t2;cin>>t1;cin>>t2;Nameapply(t1,t2);return true;}else if(pare("-typeapply")==0) {string t1;int t2;cin>>t1;cin>>t2;Typeapply(t1,t2);return true;}else if(pare("-ret")==0){string t;cin>>t;Retdevice(t);return true;}else if(pare("-show")==0) {show();return true;else{cout<<"--输入错误:"<<s<<"?"<<endl;return true;}}void devicemanager::Retdevice(string name){if(SDT.getat(name)==NULL){cout<<"设备名输入错误!"<<endl;return;}dct *t=SDT.getat(name)->DCThand;if(!t->busy){cout<<"设备未被使用!"<<endl;return;}if(t->COCThand->pare(t->usename)!=0){cout<<"无法归还未结束的设备申请(设备未取得COCT使用权)!"<<endl;return;}if(t->COCThand->CHCThand->pare(t->COCThand->name)!=0){cout<<"无法归还未结束的设备申请(设备未取得CHCT使用权)!"<<endl;return;}if(t->DCTwait.size()==0){t->busy=false;t->usename="";}else{t->busy=true;t->usename=t->DCTwait.at(0)->usename;t->DCTwait.erase(t->DCTwait.begin());}coct *co=t->COCThand;if(co->COCTwait.size()==0){if(t->busy){co->name=t->usename;co->busy=true;}else{co->busy=false;co->name="";}}else{co->busy=true;co->name=co->COCTwait.at(0)->usename;co->COCTwait.erase(co->COCTwait.begin());if(t->busy){pcb *p=new pcb;p->usename=t->usename;p->name=t->name;co->COCTwait.push_back(p);}}chct *ch=co->CHCThand;if(ch->CHCTwait.size()==0){if(co->busy){ch->name=co->name;ch->busy=true;}else{ch->busy=false;ch->name="";}}else{ch->busy=true;ch->name=ch->CHCTwait.at(0)->usename;ch->CHCTwait.erase(ch->CHCTwait.begin());if(co->busy){pcb *p=new pcb;p->usename=co->name;ch->CHCTwait.push_back(p);}}}void devicemanager::show(){cout<<"连接状态:"<<endl;for(int i=0;i<DCT.getnum();i++){cout<<DCT.returnat(i+1)->name<<"("<<DCT.returnat(i+1)->type<<")"<<" -> 控制器"<<DCT.returnat(i+1)->COCThand->type<<"->通道"<<DCT.returnat(i+1)->COCThand->CHCThand->type<<endl;}cout<<"--------SDT---------"<<endl;cout<<setiosflags(ios::left)<<setw(14)<<"设备名";cout<<setiosflags(ios::left)<<setw(14)<<"设备类型"<<endl;SDT.show();cout<<"--------DCT---------"<<endl;cout<<setiosflags(ios::left)<<setw(14)<<"类型";cout<<setiosflags(ios::left)<<setw(14)<<"进程";cout<<setiosflags(ios::left)<<setw(14)<<"状态";cout<<setiosflags(ios::left)<<setw(14)<<"阻塞队列"<<endl;for(int i=0;i<DCT.getnum();i++){cout<<setiosflags(ios::left)<<setw(14)<<DCT.returnat(i+1)->name;cout<<setiosflags(ios::left)<<setw(14)<<DCT.returnat(i+1)->usename;cout<<setiosflags(ios::left)<<setw(14)<<DCT.returnat(i+1)->busy;for(int j=0;j<DCT.returnat(i+1)->DCTwait.size();j++){cout<<setiosflags(ios::left)<<setw(7)<<DCT.returnat(i+1)->DCTwait.at(j)->usename;}cout<<endl;}cout<<"--------COCT--------"<<endl;cout<<setiosflags(ios::left)<<setw(14)<<"类型";cout<<setiosflags(ios::left)<<setw(14)<<"进程";cout<<setiosflags(ios::left)<<setw(14)<<"状态";cout<<setiosflags(ios::left)<<setw(14)<<"阻塞队列"<<endl;for(int i=0;i<COCT.getnum();i++){cout<<setiosflags(ios::left)<<setw(14)<<COCT.returnat(i+1)->type;cout<<setiosflags(ios::left)<<setw(14)<<COCT.returnat(i+1)->name;cout<<setiosflags(ios::left)<<setw(14)<<COCT.returnat(i+1)->busy;for(int j=0;j<COCT.returnat(i+1)->COCTwait.size();j++){cout<<setiosflags(ios::left)<<setw(7)<<COCT.returnat(i+1)->COCTwait.at(j)->usename;}cout<<endl;}cout<<"--------CHCT--------"<<endl;cout<<setiosflags(ios::left)<<setw(14)<<"类型";cout<<setiosflags(ios::left)<<setw(14)<<"进程";cout<<setiosflags(ios::left)<<setw(14)<<"状态";cout<<setiosflags(ios::left)<<setw(14)<<"阻塞队列"<<endl;for(int i=0;i<CHCT.getnum();i++){cout<<setiosflags(ios::left)<<setw(14)<<CHCT.returnat(i+1)->type;cout<<setiosflags(ios::left)<<setw(14)<<CHCT.returnat(i+1)->name;cout<<setiosflags(ios::left)<<setw(14)<<CHCT.returnat(i+1)->busy;for(int j=0;j<CHCT.returnat(i+1)->CHCTwait.size();j++){cout<<setiosflags(ios::left)<<setw(7)<<CHCT.returnat(i+1)->CHCTwait.at(j)->usename;}cout<<endl;}}void devicemanager::help(){cout<<" 操作系统设备管理模拟程序"<<endl;cout<<" 2013 Ploge"<<endl;cout<<"操作说明:-h"<<endl;cout<<"添加设备:-add [设备名][设备类型][控制器类型]"<<endl;cout<<"删除设备设备:-del [设备名]"<<endl;cout<<"按设备名申请设备:-nameapply [进程名] [设备名]"<<endl;cout<<"按设备类型申请设备:-typeapply [进程名] [设备类型]"<<endl;cout<<"归还设备:-ret [设备名]"<<endl;cout<<"退出:-exit"<<endl;}int main(){devicemanager t;t.help();t.show();while(t.in());return 0;}。
操作系统课程设计文档及代码

1 设计目的与内容1.1 设计目的通过课程设计, 加深对操作系统对程序执行的理解,掌握操作系统的多程序运行原理,能模拟操作系统设计相应的进程调度算法,掌握操作系统的基本原理及功能, 具有初步分析实际操作系统、设计、构造和开发现代操作系统的基本能力。
1.2 设计内容1、设计进程控制块PCB表结构,分别适用于可强占的优先数调度算法和循环轮转调度算法。
2、建立进程就绪队列。
对两种不同算法编制入链子程序。
3、编制两种进程调度算法:1)可强占的优先进程调度;2)循环时间片轮转调度4、设计操作系统运行的指令。
2 设计说明2.1 需求分析设计虚拟内核实现进程的调度,实现多道程序的调度。
设计调度算法计算各个进程的优先权限来确定进程执行的次序。
进程调度程序选择一个就绪状态的进程,使之在处理器上运行。
进程的调度采用最高优先数优先的调度算法和先来先服务调度算法相结合的算法,并且采用动态优先数策略,选择进程占用处理器后该进程仅能使用一个时间片,运行完后优先数减1。
2.2 设计思路本程序用两种算法对多个进程进行调度,每个进程可有三个状态,并假设初始状态为就绪状态。
为了便于处理,程序中的某进程运行时间以时间片为单位计算。
各进程的优先数或轮转时间数以及进程需运行的时间片数的初始值均由用户给定。
在优先数算法中,优先数的值为31与运行时间的差值。
进程每执行一次,优先数减1,CPU时间片数加1,进程还需要的时间片数减1。
在轮转算法中,采用固定时间片(即:每执行一次进程,该进程的执行时间片数为已执行了1个单位),这时,CPU时间片数加1,进程还需要的时间片数减1,并排列到就绪队列的尾上。
设计程序指令,MOV n //把整数n赋给累加器ASAV m //把累加器A的值存入地址MADD n //从累加器A的值减去整数n,结果送到累加器A。
SUB n //从累加器A的值减去整数n,结果送到累加器A。
MUL n //从累加器A的值乘以整数n,结果送到累加器A。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include <stdio.h>#include "iostream.h"#include "stdlib.h"#include "string.h"struct PCB{int id;char name[10];int size;struct PCB *next;};struct PCB *running;struct PCB *ready;struct PCB *blocked;struct PCB *q;struct PCB *p;int id=1;int size;char name[10];////////////////////////////////////////////////////////////////////////////////////// struct DCT{ //设备char name[10];int busy;PCB * occupied;PCB * waiting;struct DCT *next;struct COCT* coct; //上级控制器};struct COCT{ //控制器char name[10];int busy;PCB * occupied;PCB * waiting;struct COCT *next;struct CHCT* chct; //控制器的上级通道};struct CHCT{ //通道char name[10];int busy;PCB * occupied;PCB * waiting;struct CHCT *next;};//////////////////////////////////////////////////////////////////////////////////////struct DCT * dcts;struct COCT *cocts;struct CHCT *chcts;void enqueue(int id,char *name,int size,struct PCB *head){ struct PCB *node=(struct PCB *)malloc(sizeof(struct PCB));node->next=0;node->id=id;strcpy(node->name,name);node->size=size;struct PCB *tmp=head;while(tmp->next!=0)tmp=tmp->next;tmp->next=node;}struct PCB * dequeue(struct PCB *head){struct PCB * tmp=head->next;if(head->next!=0){head->next=head->next->next;tmp->next=0;}return(tmp);}void createProcess(){printf("\nname: ");scanf("%s",name);printf("size: ");scanf("%d",&size);printf("\n");enqueue(id++,name,size,ready);if(running==0){running=dequeue(ready);}}void switchProcess(){if(running!=0&&ready->next!=0){enqueue(running->id,running->name,running->size,ready);running=dequeue(ready);}elseprintf("没有可切换的进程\n");}void blockProcess(){if(running==0)printf("没有可阻塞的进程\n");else{enqueue(running->id,running->name,running->size,blocked);running=0;if(ready->next==0)printf("没有可执行的进程\n");elserunning=dequeue(ready);}}void wakeupProcess(){if(blocked->next==0)printf("没有可激活的进程");else{enqueue(blocked->next->id,blocked->next->name,blocked->next->size,ready);dequeue(blocked);if(running==0)running=dequeue(ready);}}void terminateProcess(){ //结束进程if(running==0){printf("没有需要结束的进程\n");}else{running=dequeue(ready);}}void displayProcessstatus(){printf("--------就绪态--------\n");if(ready->next==0)printf("当前没有进程在该状态\n");if(ready->next!=0){q=ready->next;while(ready->next!=0){printf("%s",ready->next->name);printf(" %d\n",ready->next->size);ready->next=ready->next->next;}ready->next=q;}printf("--------执行状态--------\n");if(running==0) printf("当前没有进程在该状态\n");if(running!=0){printf("%s",running->name);printf(" %d\n",running->size);}printf("--------阻塞状态--------\n");if(blocked->next==0) printf("当前没有进程在该状态\n\n");if(blocked->next!=0){p=blocked->next;while(blocked->next!=0){printf("%s",blocked->next->name);printf(" %d\n",blocked->next->size);blocked->next = blocked->next->next;}blocked->next=p;}}//////////////////////////////////////////////////////////////////////////////////struct DCT * findDCT(char name[]) //设备分配时找到要添加的设备{struct DCT *temp = dcts;while(temp->next!=NULL){temp = temp->next;if(strcmp(temp->name,name)==0)return temp;}return NULL;}struct CHCT * findChannel(char name[]){struct CHCT *temp = chcts;while(temp->next!=NULL){temp = temp->next;if(strcmp(temp->name,name)==0)return temp;}return NULL;}struct COCT * findController(char name[]){struct COCT *temp = cocts;while(temp->next!=NULL){temp = temp->next;if(strcmp(temp->name,name)==0)return temp;}return NULL;}void addProcesstoWaiting(struct PCB * waiting,struct PCB *p)//进入进程等待队列{struct PCB *temp = waiting;while(temp->next!=NULL){temp = temp->next;}//temp->next = p;//+++++++++++++++++++++++++++++++++++++++++++++++++temp->next = new struct PCB;temp->next->id = p->id;strcpy(temp->next->name,p->name);temp->next->size = p->size;temp->next->next = NULL;//++++++++++++++++++++++++++++++++++++++++++++++++++ }void add(struct PCB * head,struct PCB * node){ //入队列struct PCB *tmp=head;while(tmp->next!=0)tmp=tmp->next;tmp->next=node;}struct PCB * getFirst(struct PCB *head){ //获得队列里的第一个进程return head->next;}void allocateCHCT(struct CHCT* chct,PCB *p)//分配CHCT{if(chct->occupied!=0){printf("不能分配通道\n");addProcesstoWaiting(chct->waiting,p);}else{chct->occupied=p;printf("分配成功!\n");}add(blocked,p);if(ready!=0)running=dequeue(ready);elserunning=0;}//**************************************************void allocateCOCT(struct COCT* coct,PCB *p){if(coct->occupied!=0){printf("不能分配控制器\n");addProcesstoWaiting(coct->waiting,p);add(blocked,p);if(ready!=0)running=dequeue(ready);elserunning=0;return;}else{coct->occupied=p;allocateCHCT(coct->chct,p);}}void allocateDCT(){char nameDCT[10];printf("请输入设备名称:");scanf("%s",nameDCT);struct DCT * dct=findDCT(nameDCT);struct PCB * p = running;if(dct!=NULL&&p!=NULL){if(dct->occupied!=0){printf("不能分配设备\n");addProcesstoWaiting(dct->waiting,p);add(blocked,p);if(ready!=0)running=dequeue(ready);elserunning=0;return;}else{dct->occupied=p;allocateCOCT(dct->coct,p);//++++++++++++++++++++++++++++/*add(blocked,p);if(ready!=0)running=dequeue(ready);elserunning=0;return;*///+++++++++++++++++++++++++++++ }}elseprintf("发生错误!\n");}void releaseCHCT(char *name,struct CHCT* chct,struct PCB* p) //?????? {if(p!=NULL)addProcesstoWaiting(chct->waiting,p);if(strcmp(name,chct->occupied->name)==0){if(chct->waiting->next!=NULL){chct->occupied = dequeue(chct->waiting);}else{chct->occupied = NULL;}}}void releaseCOCT(char *name,struct COCT* coct,struct PCB* p){if(p!=NULL)addProcesstoWaiting(coct->waiting,p);if(strcmp(name,coct->occupied->name)==0){if(coct->waiting->next!=NULL){coct->occupied = dequeue(coct->waiting);}else{coct->occupied = NULL;}releaseCHCT(name,coct->chct,coct->occupied);}}void releaseDCT(){char nameDCT[10];printf("请输入要释放的设备名称:\n");scanf("%s",nameDCT);char nameP[10];printf("请输入要释放的进程名称:\n");scanf("%s",nameP);struct DCT *temp = findDCT(nameDCT);if(strcmp(temp->occupied->name,nameP)==0){if(temp->waiting->next!=NULL){temp->occupied = dequeue(temp->waiting);}else{temp->occupied = NULL;}releaseCOCT(nameP,temp->coct,temp->occupied);}else{printf("没有对应的设备和进程!");}}void addChannel(char name[]){struct CHCT * temp=(struct CHCT *)malloc(sizeof(struct CHCT));strcpy(temp->name,name);temp->next=0;temp->busy=0;temp->waiting = new struct PCB;temp->waiting->next = NULL;//temp->waiting=0;temp->occupied=0;struct CHCT * head=chcts; //进入了chcts队列while(head->next!=0)head=head->next;head->next=temp;}void addController(char *name,struct CHCT * chct){ //增加控制器struct COCT * temp=(struct COCT *)malloc(sizeof(struct COCT));strcpy(temp->name,name);temp->next=0;temp->busy=0;temp->waiting = new struct PCB;temp->waiting->next = NULL;//temp->waiting=0; //+temp->occupied=0;temp->chct= chct;struct COCT * head=cocts; //进入了cocts队列while(head->next!=0)head=head->next;head->next=temp;}void addDevice(char *name,struct COCT * coct){ //增加设备struct DCT * temp=(struct DCT *)malloc(sizeof(struct DCT));strcpy(temp->name,name);temp->next=0;temp->busy=0;temp->waiting = new struct PCB;temp->waiting->next = NULL;//temp->waiting=0;temp->occupied=0;temp->coct= coct;struct DCT * head=dcts;while(head->next!=0)head=head->next;head->next=temp;}//添加设备++++++++++++++++++++++++++++++++++void add_dct(){char newDCT[10];printf("请输入新设备的名字:\n");scanf("%s",newDCT);char newCOCT[10];printf("请输入要添加到的控制器的名字:\n");scanf("%s",newCOCT);addDevice(newDCT,findController(newCOCT));}//添加控制器void add_coct(){char newCOCT[10];printf("请输入新控制器的名字:\n");scanf("%s",newCOCT);char newCHCT[10];printf("请输入要添加到的通道的名字:\n");scanf("%s",newCHCT);addController(newCOCT,findChannel(newCHCT));}//添加通道void add_chct(){char newCHCT[10];printf("请输入新的通道的名字:\n");scanf("%s",newCHCT);addChannel(newCHCT);}//++++++++++++++++++++++++++++++++++//+++++++++++++++++删除操作++++++++++++++++++++ //删除设备void deleteDCT(char nameDCT[]){//char nameDCT[10];//int i=0;//printf("请输入要删除DCT的名字:");//scanf("%s",nameDCT);struct DCT * temp = findDCT(nameDCT);struct DCT * head = dcts;if(temp==NULL){printf("没有对应的设备!\n");return ;}elsewhile(head->next!=0){if(strcmp(temp->name,head->next->name)==0){if(temp->occupied!=NULL)printf("此设备现在正在使用不能删除\n");elsehead->next=head->next->next;//i++;break;}elsehead=head->next;}}//删除控制器void deleteCOCT(char nameCOCT[]){struct COCT *temp=findController(nameCOCT);struct COCT *head=cocts;if(temp==NULL){printf("没有对应的控制器\n");return;}elsewhile(head->next!=0){if(strcmp(temp->name,head->next->name)==0){if(temp->occupied!=NULL)printf("此控制器现在正在使用不能删除\n");else{// deleteDCT(temp->);head->next=head->next->next;}break;}head=head->next;}}//删除通道void deleteCHCT(char nameCHCT[]){struct CHCT *temp=findChannel(nameCHCT);struct CHCT *head=chcts;if(temp==NULL){printf("没有对应的通道\n");return;}elsewhile(head->next!=0){if(strcmp(temp->name,head->next->name)==0){if(temp->occupied!=NULL)printf("此通道现在正在使用不能删除\n");else{// deleteDCT(temp->);head->next=head->next->next;}//i++;break;}head=head->next;}}//+++++++++++++++++++++++++++++++++++++++++++void displayDCT(){struct DCT * dct;struct COCT *coct;struct CHCT *chct = chcts;struct PCB *pcb;//------------------------------while(chct->next!=NULL){chct = chct->next;printf(" %s(",chct->name);if(chct->occupied!=0)printf("%s",chct->occupied->name);printf(")");pcb = chct->waiting->next;//waiting是头结点,pcb指向队列第一个进程while(pcb!=NULL){printf("[%s]",pcb->name);pcb = pcb->next;}printf("\n");//----------------------------------------------------coct = cocts;while(coct->next!=NULL){coct = coct->next;if(strcmp(coct->chct->name,chct->name)==0){printf(" %s(",coct->name);if(coct->occupied!=0)printf("%s",coct->occupied->name);printf(")");pcb = coct->waiting->next;while(pcb!=NULL){printf("[%s]",pcb->name);pcb = pcb->next;}printf("\n");//-----------------------------------------dct = dcts;while(dct->next!=NULL){dct = dct->next;if(strcmp(dct->coct->name,coct->name)==0){printf(" %s(",dct->name);if(dct->occupied!=0)printf("%s",dct->occupied->name);printf(")");pcb = dct->waiting->next;while(pcb!=NULL){printf("[%s]",pcb->name);pcb = pcb->next;}printf("\n");}}}}}}//////////////////////////////////////////////////////////////////////void main(){dcts=(struct DCT *)malloc(sizeof(struct DCT));dcts->next=0;cocts=(struct COCT *)malloc(sizeof(struct COCT));cocts->next=0;chcts=(struct CHCT *)malloc(sizeof(struct CHCT));chcts->next=0;addChannel("chct1");addChannel("chct2");addController("coct1",findChannel("chct1"));addController("coct2",findChannel("chct1"));addController("coct3",findChannel("chct2"));addDevice("dct1",findController("coct1"));addDevice("dct2",findController("coct1"));addDevice("dct3",findController("coct2"));//addDevice("dct4",findController("coct2"));addDevice("dct4",findController("coct3"));ready=(struct PCB *)malloc(sizeof(struct PCB));blocked=(struct PCB *)malloc(sizeof(struct PCB));ready->next=0;blocked->next=0;A:while(1){int choice;int function;printf("1:创建进程\n");printf("2:切换进程\n");printf("3:阻塞进程\n");printf("4:唤醒进程\n");printf("5:结束进程\n");printf("6:显示进程\n");printf("7:设备管理\n");printf("0:exit\n");scanf("%d",&choice);switch(choice){case 1:createProcess();break;case 2:switchProcess();break;case 3:blockProcess();break;case 4:wakeupProcess();break;case 5:terminateProcess();break;case 6:displayProcessstatus();break;case 7:while(1){printf("1:设备分配\n");printf("2:设备释放\n");printf("3:显示\n");printf("4:添加新的设备\n");printf("5:添加新的控制器\n");printf("6:添加新的通道\n");printf("7:删除设备\n");printf("8:删除控制器\n");printf("9:删除通道\n");printf("0:返回主界面\n");scanf("%d",&function);switch(function){case 1:allocateDCT();break;case 2:releaseDCT();break;case 3:displayDCT();break;case 4:add_dct();break;case 5:add_coct();break;case 6: add_chct();break;case 7:char nameDCT[10];printf("请输入要删除DCT的名字:\n");scanf("%s",nameDCT);deleteDCT(nameDCT);break;case 8:char nameCOCT[10];printf("请输入要删除COCT的名字:\n");scanf("%s",nameCOCT);deleteCOCT(nameCOCT);break;case 9:char nameCHCT[10];printf("请输入要删除CHCT的名字:\n");scanf("%s",nameCHCT);deleteCHCT(nameCHCT);break;case 0:goto A;break;}}case 0:exit(0);break;}}}。