体系结构通道模拟程序

来源:互联网 发布:晨曦计价软件下载 编辑:程序博客网 时间:2024/06/05 02:27
/*++
Module Name:   channl.c
Abstract:
    1 通道完成一次数据输入输出的过程需三步(如图一所示):
(1) 在用户程序中使用访管指令进入管理程序,由CPU通过管理程序组织一个通道程序,并启 动通道;
(2) 通道处理机执行通道程序,完成指定的数据输入输出工作;
(3) 通道程序结束后第二次调用管理程序对输入输出请求进行处理每完成一次输入输出工作, 
CPU只需要两次调用管理程序,大大减少了对用户程序的打扰。
    

Author:

    YourName (YourEmail) 2007-06-16

Revision History:
  传值问题????

--
*/

#include
<iostream.h>
#include
<string.h>
#include
<memory.h>
#include
<stdlib.h>

#define actDeviceNum  2  //可用设备的数量device[actDeviceNum]
#define MAX_CAP  30  //设备每个数据区可以装的字节数

/////////////////////////////////////////////////////////////////////
//定义全局变量
enum Check_sign{ NONE,INIT,FINISH };  //定义中断类型的判断条件

typedef 
struct channalManager  //通道处理机定义    %%c++结构体内部可以定义函数
{
    
int interrupt;
   
// run(channalManager);   //结构体里面能调用结构体本身不? 可以好像,哈
}
ChanMan;

typedef 
struct device  //设备定义
{
    
int actCap;
    
char data[MAX_CAP];
}
Device;
 
/*/////////////初始化设备参数////////////////   
void Init_device(struct device)  //初始化完成的数组如何传递出去的问题???????????
{   
    int i;
    for(i=0;i<actDeviceNum;i++)
    {
        memset(device[i].data,'/0',MAX_CAP);  //初始化设备内容为空
        device[i].actCap=(50+rand()%10);  //假设随机
    }
}
*/
/////////////////////////////////////////////////////
//函数声明部分
void run(struct channalManager);
int MaxDev(Device device[actDeviceNum]);
char *str_get(char **str,int n);
void printDevice(int i,Device device[]);
void memoryToDevice(char** mem,int MaxDevCap ,Device device[],ChanMan channalManager);
 
////////////////////////////////////////////////////////
//求设备数组的最大容量/////////////   
//入口点:设备数组
//返回值:设备数组的最大容量   
int MaxDev(Device device[actDeviceNum]) 
{   
 int i;
 int MaxDevCap;
 MaxDevCap=device[0].actCap;
    for(i=0;i<actDeviceNum;i++)
    {
        if (device[i].actCap >= MaxDevCap)
            MaxDevCap=device[i].actCap;
     }
     return MaxDevCap;

 
//
void printDevice(int i,Device device[])   //打印设备显示内容
{
    cout<<"设备号:"<<i<<"内容: "<<device[i].data<<endl;
}

//取字符串中的第N个字符
char *str_get(char **str,int n)  //关于指向指针的指针问题???
{
    int i;
    char *p;
    p=*str;
    int Len=strlen(*str);
    for(i=0;i<Len;i++)
    {
         if(i==n)
         {
            return p;
         }
       else  p++;
    }
}
//传输方式为内存到设备,采用字节多路方式,设备分时复用通道的工作算法。
void memoryToDevice(char** mem,int MaxDevCap ,Device device[],ChanMan channalManager)   //传入的是指向指针数组的指针?????
{                               
    char** memory;
    memory = mem;
   
    //int actDeviceNum=strlen(mem); //????????
 channalManager.interrupt=INIT;
 run(channalManager);
    for(int fence = 0 ;fence < MaxDevCap; fence++)  //设备的最大容量为结束条件
        for(int i = 0;i < actDeviceNum ; i++)   //遍历设备传入内容
        {                                       //每个内容数组对应传入相应的设备 divice[i]=mem[i]                          
            if(fence < device[i].actCap)    //每个设备有不同的容量
            {
                device[i].data[fence] = *str_get(memory[i],fence);  //str(string,n)如何取出内存字符串中单个字符
            }                                               //没有实现这个功能的库函数、、?
            printDevice(i,device);  //如果if中字符没传进的话,这里仍然是空白区
        }
    printDevice(fence, device); //实际上这里在打印设备里的空白区
       
    channalManager.interrupt=FINISH; //置通道处理结束标志
 run(channalManager);
}

//CPU处理不同的中断的算法。
void run(struct channalManager)     //关于结构体做形参的问题???
{
    while(true)
    {
        if(channalManager.interrupt == NONE)         //cpu在执行用户程序
        {
            cout<<"The cpu is doing some thing..."<<endl;
            cout<<"The cpu is doing some thing..."<<endl;
            break;
        }
        else if(channalManager.interrupt == INIT)   //cpu开启通道中断
        {
            cout<<"CPU is interrupted"<<endl;
            cout<<"This is a I/0 Init instruction,The channalManager is init the device..."<<endl;
            break;
        }
        else if(channalManager.interrupt == FINISH) //cpu结束通道中断
        {
            cout<<"CPU is interrupted"<<endl;
            cout<<"This is a I/0 Finish instruction,The channalManager is close the device..."<<endl;
            break;
        }
    }
}
//随机延时
void delay()
{
    int i;
    for(i=0;i<rand()%500+500;i++)
        ;
    }
   
//主函数   
int main(int argc, char* argv[])
{
    cout<<"------------Demo the work flow of channel----------- "<<endl;
    char* memory[3];
    memory[0] = "love";
    memory[1] = "channel";
    memory[2] = "architecture";
 char **mem;
 *mem=memory[3];
   
    ChanMan channalManager;
 channalManager.interrupt=NONE;
    Device device[actDeviceNum];
  // Init_device(device[actDeviceNum]); //初始化设备参数
 int i;
    for(i=0;i<actDeviceNum;i++)
    {
        memset(device[i].data,'/0',MAX_CAP);  //初始化设备内容为空
        device[i].actCap=(50+rand()%10);  //假设随机
    }

   int MaxDevCap=MaxDev(device);  //数组做形参时候不用[]
   
    delay();  //延迟随机时间,CPU处理通道程序
   /* channalManager.interrupt=INIT;
    run(channalManager);
    */
     memoryToDevice(mem, MaxDevCap,device,channalManager);
 
  return 0;
}

 这里是完整的实验要求:

实验3 通道处理过程模拟
一、实验目的
通过模拟实现通道处理过程,掌握通道技术。
二、实验内容
程序1
结合数据结构的相关知识,编写通道处理过程模拟程序。
1 通道完成一次数据输入输出的过程需三步(如图一所示):
(1) 在用户程序中使用访管指令进入管理程序,由CPU通过管理程序组织一个通道程序,并启 动通道;
(2) 通道处理机执行通道程序,完成指定的数据输入输出工作;
(3) 通道程序结束后第二次调用管理程序对输入输出请求进行处理每完成一次输入输出工作, CPU只需要两次调用管理程序,大大减少了对用户程序的打扰。
图一 通道程序,管理程序和用户程序的执行时间关系
2 通道的主要功能(如图二所示):
(1)接受CPU发来的指令,选择一台指定的外围设备与通道相连接
(2)执行CPU为通道组织的通道程序
(3)管理外围设备的有关地址
(4)管理主存缓冲区的地址
(5)控制外围设备与主存缓冲区间数据交换的个数
(6)指定传送工作结束时要进行的操作
(7)检查外围设备的工作状态,是正常或故障
(8)在数据传输过程中完成必要的格式的变换
二、实例
1.传输方式为内存到设备,采用字节多路方式,设备分时复用通道的工作算法。
void memoryToDevice(char** mem) {
char** memory;
memory = mem;
for(int fence = 0 ;fence < MaxDevCap; fence++)
for(int i = 0;i < actDeviceNum ; i++)
{
if(fence < device[i].actCap)
{device[i].data[fence] = memory[i][fence];
}
printDevice();
}
printDevice();
}
2.CPU处理不同的中断的算法。
void run()
{
while(true)
{
if(channalManager.interrupt == NONE)
{
cout<<"The cpu is doing some thing..."<<endl;
cout<<"The cpu is doing some thing..."<<endl;
break;
}
if(channalManager.interrupt == INIT)
{
cout<<"CPU is interrupted"<<endl;
cout<<"This is a I/0 Init instruction,The channalManager is init the device..."<<endl;
break;
}
if(channalManager.interrupt == FINISH)
{ cout<<"CPU is interrupted"<<endl;
cout<<"This is a I/0 Finish instruction,The channalManager is close the device..."<<endl;
break;
}
}
}

3. 测试数据及运行结果
a.初始化内存块中需要传输的数据:
char* memory[3];
memory[0] = "love";
memory[1] = "channel";
memory[2] = "architecture";

b.设备中的内容为空:

c.字节多路通道程序分时为不同的设备服务:

d.通道程序执行完毕,CPU中断后关闭设备,继续执行用户程序。

 

 

没准真的不是编程的料,觉得自己脑袋不够用,o(∩_∩)o...哈哈。

原创粉丝点击