Operating System Lab 3 device management

Source: Internet
Author: User

Purpose:

The simulation includes adding and deleting devices, allocating and recycling devices, and reflecting the independence of devices in device allocation.

Content requirements:

1. device management refers to the system structure of peripheral devices simulating computer systems. Two channels, three controllers, and four peripherals (keyboard, mouse, display, and printer) can be created ), the keyboard and mouse use a controller.

2. The device management function allows you to add and delete devices and maintain controllers.

3. device management also includes device allocation and recovery. This allows students to master the architecture and management methods of peripheral devices. When the allocation is successful, the user process uses the device, otherwise it will be blocked to a level of control table, waiting to be awakened.

Device allocation must meet the independence requirements of devices. To achieve device independence, a device-independent software layer must be designed on top of the driver. Its main functions can be divided into performing public operations on all devices, including: (a) allocating and recycling exclusive devices; (B) map the logical device name to a physical device to further find the driver for the corresponding physical device.

Functions:

1. Add Devices
2. delete a device
3. Apply for a device
4. recycling equipment
5. display the status of all devices currently

Implementation process:

# Include <iostream> # include <string. h> # include <windows. h> using namespace std; struct BLOCK/* blocking */{charname [100];/* blocking process name */charfrom [100]; BLOCK * next ;}; struct DCT/* Device control table */{charname [100];/* device name */chartype;/* device type: I/O */intstage;/* device status: 1/0 */intcoct;/* connect the Controller Number of the corresponding controller */BLOCK * BLOCK, * rear;/* BLOCK queue pointer * // * BLOCK queue tail pointer */}; struct SDT/* system device table */{charname [100];/* system device name */DCTDCT;/* corresponding device bound */} SDT [100]; struct COCT/* controller control table */{charname [100 ];/* Controller name */intstage;/* controller status: 1/0 */intchct;/* connection Channel Number */BLOCK * BLOCK, * rear; /* blocking queue * // * blocking queue tail pointer */} COCT [100]; struct CHCT/* channel control table */{charname [100]; /* channel name */intstage;/* Status of the channel: 1/0 */BLOCK * BLOCK, * rear; /* blocking queue * // * blocking queue tail pointer */} CHCT [2]; intSDT_N = 4; intCOCT_N = 3; void init () /* initialize */{/* initialize the four original SDT and DCT devices: k m t p */strcpy (SDT [0]. name, "K"); strcpy (SDT [0]. DCT. name, SDT [0]. name); SDT [0]. DCT. type = 'I'; SDT [0]. DCT. stage = 0; SDT [0]. DCT. Coct = 0; SDT [0]. DCT. BLOCK = SDT [0]. DCT. rear = NULL; strcpy (SDT [1]. name, "M"); strcpy (SDT [1]. DCT. name, SDT [1]. name); SDT [1]. DCT. type = 'O'; SDT [1]. DCT. stage = 0; SDT [1]. DCT. coct = 0; SDT [1]. DCT. BLOCK = SDT [1]. DCT. rear = NULL; strcpy (SDT [2]. name, "T"); strcpy (SDT [2]. DCT. name, SDT [2]. name); SDT [2]. DCT. type = 'I'; SDT [2]. DCT. stage = 0; SDT [2]. DCT. coct = 1; SDT [2]. DCT. BLOCK = SDT [2]. DCT. rear = NULL; strcpy (SDT [3]. name, "P"); strcpy (SDT [3]. DCT. name, SDT [3]. name); SDT [3]. DCT. type = 'O'; SDT [3]. DCT. stage = 0; SDT [3]. DCT. coct = 2; SDT [3]. DCT. BLOCK = SDT [3]. DCT. rear = NULL;/* initialize the original three controllers of the COCT system: CO1 CO2 CO 3 */strcpy (COCT [0]. name, "CO1"); strcpy (COCT [1]. name, "CO2"); strcpy (COCT [2]. name, "O3"); COCT [0]. stage = 0; COCT [1]. stage = 0; COCT [2]. stage = 0; COCT [0]. chct = 0; COCT [1]. chct = 1; COCT [2]. chct = 1; COCT [0]. BLOCK = COCT [0]. rear = NULL; COCT [1]. BLOCK = COCT [1]. rear = NULL; COCT [2]. BLOCK = COCT [2]. rear = NULL;/* initialize the original two channels of the CHCT system: CH1 CH2 */strcpy (CHCT [0]. name, "weight"); strcpy (CHCT [1]. name, "CH2"); CHCT [0]. stage = 0; CHCT [1]. stage = 0; CHCT [0]. BLOCK = CHCT [0]. rear = NULL; CHCT [1]. BLOCK = CHCT [1]. rear = NULL;}/* Device maintenance */bool judgeSDT (char * name) {int I; for (I = 0; I <SDT_N; I ++) {if (strcmp (SDT [I]. name, name) = 0)/* if a device with duplicate names fails to be created, 0 */return false;} if (I = SDT_N) return true;} void add (char * name, char type)/* New Device */{int I; strcpy (SDT [SDT_N]. name, name); strcpy (SDT [SDT_N]. DCT. name, name); SDT [SDT_N]. DCT. stage = 0;/* set the status to 0 */SDT [SDT_N]. DCT. type = type; int choose; cout <"1. Select an existing controller. 2. Create a controller. \ n>"; cin> choose; if (choose = 1) /* select an existing controller */{cout <"The existing controller is:"; for (I = 0; I <COCT_N; I ++) cout <COCT [I]. name <"\ t"; char name [100]; cout <endl <"enter the name of the selected Controller:"; cin> name; for (I = 0; I <COCT_N; I ++) if (strcmp (COCT [I]. name, name) = 0) break; SDT [SDT_N]. DCT. coct = I;} if (choose = 2)/* new controller */{char name [100]; cout <"existing controller :"; for (I = 0; I <COCT_N; I ++) cout <COCT [I]. name <"\ t"; cout <endl <"enter the name of the new Controller:"; cin> name; strcpy (COCT [COCT_N]. name, name); for (I = 0; I <COCT_N; I ++) {if (strcmp (COCT [I]. name, name) = 0) break;} SDT [SDT_N]. DCT. coct = I; cout <"existing channel:"; for (I = 0; I <2; I ++) c Out <CHCT [I]. name <"\ t"; cout <endl <"enter the name of the selected channel:"; cin> name; for (I = 0; I <2; I ++) {if (strcmp (CHCT [I]. name, name) = 0) break;} COCT [SDT [SDT_N]. DCT. coct]. chct = I; COCT_N ++;} SDT_N ++;} void moveDCT (DCT * a, DCT * B) {strcpy (a-> name, B-> name ); a-> BLOCK = B-> BLOCK; a-> rear = B-> rear; a-> coct = B-> coct; a-> stage = B-> stage; a-> type = B-> type;} void del (char * name)/* Delete device name */{int I, j, k; for (I = 0; I <SDT_N; I ++) {If (strcmp (SDT [I]. name, name) = 0) break;} BLOCK * p, * B; p = B = COCT [SDT [I]. DCT. coct]. BLOCK; while (B) {if (strcmp (B-> from, name) = 0) {if (B = COCT [SDT [I]. DCT. coct]. BLOCK) p = COCT [SDT [I]. DCT. coct]. BLOCK = B-> next; else {p-> next = B-> next; delete B;} B = p;} p = B; if (B! = NULL) B = B-> next;} p = B = CHCT [COCT [SDT [I]. DCT. coct]. chct]. BLOCK; while (B) {if (strcmp (B-> from, name) = 0) {if (B = CHCT [COCT [SDT [I]. DCT. coct]. chct]. BLOCK) p = CHCT [COCT [SDT [I]. DCT. coct]. chct]. BLOCK = B-> next; else {p-> next = B-> next; delete B;} B = p;} p = B; if (B! = NULL) B = B-> next;} if (CHCT [COCT [SDT [I]. DCT. coct]. chct]. BLOCK = NULL) {if (COCT [SDT [I]. DCT. coct]. BLOCK! = NULL) {if (SDT [I]. DCT. BLOCK = NULL) SDT [I]. DCT. stage = 0; CHCT [COCT [SDT [I]. DCT. coct]. chct]. BLOCK = COCT [SDT [I]. DCT. coct]. BLOCK; COCT [SDT [I]. DCT. coct]. BLOCK = COCT [SDT [I]. DCT. coct]. BLOCK-> next;} else {if (SDT [I]. DCT. BLOCK! = NULL) {CHCT [COCT [SDT [I]. DCT. coct]. chct]. BLOCK = SDT [I]. DCT. BLOCK; SDT [I]. DCT. BLOCK = SDT [I]. DCT. BLOCK-> next;} else {CHCT [COCT [SDT [I]. DCT. coct]. chct]. stage = 0; COCT [SDT [I]. DCT. coct]. stage = 0; SDT [I]. DCT. stage = 0; for (j = 0; j <COCT_N; j ++) {if (COCT [SDT [I]. DCT. coct]. chct = COCT [j]. chct & j! = I) {if (COCT [j]. BLOCK! = NULL) {CHCT [COCT [SDT [I]. DCT. coct]. chct]. BLOCK = COCT [j]. BLOCK; COCT [j]. BLOCK = COCT [j]. BLOCK-> next; CHCT [COCT [SDT [I]. DCT. coct]. chct]. stage = 1; break ;}}}}}for (j = 0; j <SDT_N; j ++) {if (SDT [I]. DCT. coct = SDT [j]. DCT. coct & j! = I) break;} if (j = SDT_N) {for (k = SDT [I]. DCT. coct; k <COCT_N-1; k ++) {strcpy (COCT [k]. name, COCT [k + 1]. name); COCT [k]. stage = COCT [k + 1]. stage; COCT [k]. chct = COCT [k + 1]. chct; COCT [k]. BLOCK = COCT [k + 1]. BLOCK; COCT [k]. rear = COCT [k + 1]. rear;} for (k = 0; k <SDT_N; k ++) {if (SDT [k]. DCT. coct> SDT [I]. DCT. coct) SDT [k]. DCT. coct --;} for (k = I; k <SDT_N-1; k ++) {strcpy (SDT [k]. name, SDT [k + 1]. name); moveDCT (& SDT [k]. DCT, & SDT [k + 1]. DCT);} SDT_N --; COCT_N --;} else {for (k = I; k <SDT_N-1; k ++) {strcpy (SDT [k]. name, SDT [k + 1]. name); moveDCT (& SDT [k]. DCT, & SDT [k + 1]. DCT);} SDT_N --;}/* Device allocation */int apply (char * p, char * name)/* apply for a device, PROCESS p requests the device name */{int I, j;/* Check the device name from the past to the next */BLOCK * block; block = new BLOCK; strcpy (block-> name, p ); strcpy (block-> from, name); block-> next = NULL; for (I = 0; I <SDT_N; I ++) {if (strcmp (SDT [I]. name, name) = 0) break;} I F (SDT [I]. DCT. stage = 0)/* If the status of DCT is 0, set it to 1 and continue the query */{SDT [I]. DCT. stage = 1; if (COCT [SDT [I]. DCT. coct]. stage = 0)/* If the COCT status is 0, set it to 1 and continue the query */{COCT [SDT [I]. DCT. coct]. stage = 1; if (CHCT [COCT [SDT [I]. DCT. coct]. chct]. stage = 0)/* If the CHCT status is 0, set it to 1 */{CHCT [COCT [SDT [I]. DCT. coct]. chct]. stage = 1; if (CHCT [COCT [SDT [I]. DCT. coct]. chct]. BLOCK = NULL) CHCT [COCT [SDT [I]. DCT. coct]. chct]. rear = CHCT [COCT [SDT [I]. DCT. coct]. chct]. BLO CK = block;/* indicates the */return 1 of the device name requested by process p; /* Application successful */} else/* If the CHCT status is 1, the process needs to be blocked to the CHCT blocking queue */{CHCT [COCT [SDT [I]. DCT. coct]. chct]. rear-> next = block; CHCT [COCT [SDT [I]. DCT. coct]. chct]. rear = block; return 2;/* blocked to CHCT */} else/* If the COCT status is 1, the process needs to be congested to the coct blocking queue */{if (COCT [SDT [I]. DCT. coct]. BLOCK = NULL) COCT [SDT [I]. DCT. coct]. rear = COCT [SDT [I]. DCT. coct]. BLOCK = block; else {COCT [SDT [I]. DCT. coct]. rear-> next = block; C OCT [SDT [I]. DCT. coct]. rear = block;} return 3;/* blocked to COCT */} else/* If the status of DCT is 1, the process needs to be blocked to the blocked queue of DCT */{if (SDT [I]. DCT. BLOCK = NULL) SDT [I]. DCT. rear = SDT [I]. DCT. BLOCK = block; else {SDT [I]. DCT. rear-> next = block; SDT [I]. DCT. rear = block;} return 4;/* blocked to DCT */} bool recycle (char * t, char * name) /* t reclaim device name */{/* from the back to the front */int I, j, k; for (I = 0; I <SDT_N; I ++) {if (strcmp (SDT [I]. name, name) = 0) break;} BLOCK * B, * p ,* Q; p = B = CHCT [COCT [SDT [I]. DCT. coct]. chct]. BLOCK; while (B) {if (strcmp (B-> name, t) = 0) break; p = B; B = B-> next;} if (B! = NULL) {if (B = CHCT [COCT [SDT [I]. DCT. coct]. chct]. BLOCK) {q = CHCT [COCT [SDT [I]. DCT. coct]. chct]. BLOCK; CHCT [COCT [SDT [I]. DCT. coct]. chct]. BLOCK = B-> next; delete q;} else {p-> next = B-> next; delete B;} if (CHCT [COCT [SDT [I]. DCT. coct]. chct]. BLOCK = NULL) {if (COCT [SDT [I]. DCT. coct]. BLOCK! = NULL) {if (SDT [I]. DCT. BLOCK = NULL) SDT [I]. DCT. stage = 0; CHCT [COCT [SDT [I]. DCT. coct]. chct]. BLOCK = COCT [SDT [I]. DCT. coct]. BLOCK; COCT [SDT [I]. DCT. coct]. BLOCK = COCT [SDT [I]. DCT. coct]. BLOCK-> next;} else {if (SDT [I]. DCT. BLOCK! = NULL) {CHCT [COCT [SDT [I]. DCT. coct]. chct]. BLOCK = SDT [I]. DCT. BLOCK; SDT [I]. DCT. BLOCK = SDT [I]. DCT. BLOCK-> next;} else {CHCT [COCT [SDT [I]. DCT. coct]. chct]. stage = 0; COCT [SDT [I]. DCT. coct]. stage = 0; SDT [I]. DCT. stage = 0; for (j = 0; j <COCT_N; j ++) {if (COCT [SDT [I]. DCT. coct]. chct = COCT [j]. chct & j! = I) {if (COCT [j]. BLOCK! = NULL) {CHCT [COCT [SDT [I]. DCT. coct]. chct]. BLOCK = COCT [j]. BLOCK; COCT [j]. BLOCK = COCT [j]. BLOCK-> next; CHCT [COCT [SDT [I]. DCT. coct]. chct]. stage = 1; break ;}}return true ;}} return true ;}} void menu () {cout <"\ t has been used when there are too many other Available soon after" <endl; cout <"\ t release A, add device secrets" <endl; cout <"\ t release D, delete device secrets" <endl; cout <"\ t S, apply for device renewal" <endl; cout <"\ t ┃ R, recycle device renewal" <endl; cout <"\ t ┃ H, display the status of all current devices" <endl; cout <"\ t ┃ E, exit Warning "<endl; cout <" \ t when there are too many pending errors "<endl;} void showSDT () {int I; for (I = 0; I <SDT_N; I ++) cout <SDT [I]. name <"\ t"; cout <endl;} void showAll () {int I; cout <"\ t system device table (SDT) "<endl; cout <" \ t handle "<endl; cout <" \ t name \ t "; for (I = 0; I <SDT_N; I ++) {cout <SDT [I]. name <"\ t" ;}cout <endl <"zookeeper has been used when zookeeper has been added" <endl; cout <"\ t device control table (DCT)" <endl; cout <"\ t has been released without being released" <Dl; cout <"\ t name \ t"; for (I = 0; I <SDT_N; I ++) cout <SDT [I]. DCT. name <"\ t"; cout <endl; cout <"\ t Type \ t"; for (I = 0; I <SDT_N; I ++) cout <SDT [I]. DCT. type <"\ t"; cout <endl; cout <"\ t status \ t"; for (I = 0; I <SDT_N; I ++) cout <SDT [I]. DCT. stage <"\ t"; cout <endl; cout <"\ t corresponds to coct \ t"; for (I = 0; I <SDT_N; I ++) cout <COCT [SDT [I]. DCT. coct]. name <"\ t"; cout <endl; cout <"\ t blocking queue \ n"; for (I = 0; I <SDT_N; I ++) {BLOCK * p; p = SDT [I]. DCT. BLOCK; cout <"\ t" <SDT [I]. name <"->"; while (p) {cout <p-> name <"(from:" <p-> from <") "<" \ t "; p = p-> next;} cout <endl ;} cout <endl <"zookeeper." <endl; cout <"\ t controller control table (COCT)" <endl; cout <"\ t handle" <endl; cout <"\ t name \ t"; for (I = 0; I <COCT_N; I ++) cout <COCT [I]. name <"\ t"; cout <endl; cout <"\ t status \ t"; for (I = 0; I <COCT_N; I ++) cout <"" <COCT [I]. stage <"\ t"; c Out <endl; cout <"\ t corresponds to CHCT \ t"; for (I = 0; I <COCT_N; I ++) cout <CHCT [COCT [I]. chct]. name <"\ t"; cout <endl; cout <"\ t blocking queue \ n"; for (I = 0; I <COCT_N; I ++) {BLOCK * p; p = COCT [I]. BLOCK; cout <"\ t" <COCT [I]. name <"->"; while (p) {cout <p-> name <"(from:" <p-> from <") "<" \ t "; p = p-> next;} cout <endl ;} cout <endl <"zookeeper." <endl; cout <"\ t channel control table (CHCT)" <endl; cout <"\ t cannot exceed limit" <endl; cout <"\ t name \ t \ T "; for (I = 0; I <2; I ++) cout <CHCT [I]. name <"\ t"; cout <endl; cout <"\ t status \ t"; for (I = 0; I <2; I ++) cout <"" <CHCT [I]. stage <"\ t"; cout <endl; cout <"\ t blocking queue \ n"; for (I = 0; I <2; I ++) {BLOCK * p; p = CHCT [I]. BLOCK; cout <"\ t" <CHCT [I]. name <"->"; if (p! = NULL) p = p-> next; while (p) {cout <p-> name <"(from:" <p-> from <") "<" \ t "; p = p-> next;} cout <endl;} cout <" \ t process name for successful device allocation: "<endl; for (I = 0; I <2; I ++) {cout <" \ t "<CHCT [I]. name <"->"; if (CHCT [I]. BLOCK! = NULL) cout <CHCT [I]. BLOCK-> name <"(from" <CHCT [I]. BLOCK-> from <")" <endl; cout <endl ;}cout <endl; cout <endl <"zookeeper" <endl;} bool judgeProcess (char * name) {BLOCK * B; int I, j, k, l; for (I = 0; I <SDT_N; I ++) {B = SDT [I]. DCT. BLOCK; while (B) {if (strcmp (B-> name, name) = 0) return false; /* process with duplicate names */B = B-> next;} if (I = SDT_N) {for (j = 0; j <COCT_N; j ++) {B = COCT [j]. BLOCK; while (B) {If (strcmp (B-> name, name) = 0) return false;/* process with duplicate names */B = B-> next ;}} if (j = COCT_N) {for (k = 0; k <2; k ++) {B = CHCT [k]. BLOCK; while (B) {if (strcmp (B-> name, name) = 0) return false; /* process with duplicate names */B = B-> next;} if (k = 2) {return true; /* No duplicate process */}}} void cometrue () {char order; char process [100]; char equipment [100]; char type; cout <"Enter the command:"; cin> order; if (order = 'A' | order = 'A ') /* Add a device */{cout <"enter the name of the added device"; ci N> equipment; cout <"input device type (I/O):"; cin> type; if (judgeSDT (equipment) {add (equipment, type ); cout <"create device" <equipment <"succeeded. "<Endl;} else {while (! JudgeSDT (equipment) {cout <"Current Device:"; showSDT (); cout <"has a duplicate name device. \ N enter the device name again: "; cin> equipment;} add (equipment, type); cout <" create device "<equipment <" successful. "<Endl ;}} else if (order = 'D' | order = 'D ') /* Delete the device */{cout <"enter the name of the device to be deleted:"; cin> equipment; if (judgeSDT (equipment) {while (! JudgeSDT (equipment) {cout <"Current Device:"; showSDT (); cout <"no" <equipment <"device. \ N enter "; cin> equipment;} del (equipment); cout <" delete device "<equipment <" success. "<Endl ;}else {del (equipment); cout <" delete device "<equipment <" successful. "<Endl ;}} else if (order = 's' | order = 's')/* apply for a device */{int success; cout <"Enter the process name:"; cin> process; if (judgeProcess (process) {cout <"Enter the device to be applied for:"; cin> equipment; if (! JudgeSDT (equipment) {while (judgeSDT (equipment) {cout <"the current device has:"; showSDT (); cout <"no" <equipment <"device. \ N enter "; cin> equipment ;}} else {while (! JudgeProcess (process) {cout <"has a duplicate process. \ N enter the process name again: "; cin> process;} cout <" Enter the device to be applied: "; cin> equipment; if (judgeSDT (equipment )) {while (! JudgeSDT (equipment) {cout <"Current Device:"; showSDT (); cout <"no" <equipment <"device. \ N please enter again: "; cin> equipment ;}} success = apply (process, equipment); if (success = 1) cout <process <"applying for a device" <equipment <"succeeded. "<Endl; if (success = 2) cout <process <" is blocked on the CHCT. "<Endl; if (success = 3) cout <process <" is blocked on COCT. "<Endl; if (success = 4) cout <process <" is blocked on DCT. "<Endl;} else if (order = 'R' | order = 'R ') /* recycle device */{cout <"Enter the process name of the device to be recycled:"; cin> process; cout <"enter the name of the device to be recycled :"; cin> equipment; if (judgeSDT (equipment) {while (! JudgeSDT (equipment) {cout <"Current Device:"; showSDT (); cout <"no" <equipment <"device. \ N enter "; cin> equipment;} recycle (process, equipment); cout <process <" reclaim device "<equipment <" successful. "<Endl;} else {recycle (process, equipment); cout <process <" reclaim device "<equipment <" successful. "<Endl ;}} else if (order = 'H' | order = 'H') {showAll ();} else if (order = 'E' | order = 'E') {exit (0);} else {cout <"error command! \ N ";}} int main () {init (); showAll (); getchar (); system (" cls "); while (1) {menu (); cometrue (); showAll (); getchar (); system ("cls") ;}return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.