过程控制块

时间:2011-02-08 21:46:21

标签: operating-system

我的教授问这个问题是为了做作业。

查阅Linux手册或Microsoft Windows手册。在您选择的操作系统中查找过程控制块(过程描述符)的实际内容。

我知道一般PCB包含

流程编号或流程ID

流程状态

程序计数器

CPU寄存器

CPU调度信息

内存管理信息

会计信息

I / O状态信息

我正在尝试查找手册,但我没有在网上看到它。任何人都可以帮我这个。

5 个答案:

答案 0 :(得分:5)

根据我在Linux中的理解,PCB或流程描述符由内核动态分配,不能直接从用户空间读取。

IBM的developerWorks库有一篇很好的文章,向您展示如何为Linux创建内核模块并访问task_struct struct。

http://www.ibm.com/developerworks/linux/library/l-linux-process-management/index.html

希望这有帮助。


edit:在Linux v2.6.37中定义的task_struct:http://lxr.linux.no/#linux+v2.6.37/include/linux/sched.h#L1182

由谷歌提供。 :)

答案 1 :(得分:1)

对于Linux,启动here(并转到更新版本的Linux内核)。虽然陈旧,但这应该足以帮助您找到答案。

答案 2 :(得分:1)

对于Windows,您可以查看名为“PEB”的用户模式等效项。可以使用'!peb'使用WinDbg进行检查。

答案 3 :(得分:0)

这可以帮到你。我以前发布过它但出于安全原因我不得不取消帐户。

        #include <iostream>
        #include <sstream>
        #include <vector>
        #include <list>
        #include <string>
        #include "proc.h"
        #include "rq.h"

        using namespace std;


        // creates a vector of Queues
        void load_queue(vector<rq> &queue, unsigned int num_of_queues)
        {
            for(int pos=0; pos<num_of_queues; pos++)
            {
                queue.push_back(rq());
            }
        }

        //returns the biggest queue position in the vector
        unsigned int get_biggest_queue_pos(vector<rq> queues)
        {
            unsigned int queue_pos = 0; // Pos of smaller queue. Default 0.
            unsigned int queue_size = queues.at(queue_pos).size(); // Its size
            for(unsigned int pos = 0; pos < queues.size(); pos++)
            {
                if(queues.at(pos).size() > queue_size)
                {
                    queue_pos = pos;
                    queue_size = queues.at(pos).size();
                }
            }
            return queue_pos; // Return smaller queue position in vector.
        }



        // Returns the smaller queue position in the vector.
        unsigned int get_smaller_queue_pos(vector<rq> queues)
        {
            unsigned int queue_pos = 0; // Pos of smaller queue. Default 0.
            unsigned int queue_size = queues.at(queue_pos).size(); // Its size
            for(unsigned int pos = 0; pos < queues.size(); pos++)
            {
                if(queues.at(pos).size() < queue_size)
                {
                    queue_pos = pos;
                    queue_size = queues.at(pos).size();
                }
            }
            return queue_pos; // Return smaller queue position in vector.
        }

        //Searches for a PID in entire system and erases it.
        bool get_pid_process(vector<rq> &queue, int id)
        {
            vector <proc> temp;
            proc process(0,0,0);
            int check;
            int number=id;
            int size=queue.size();

            /*checks rest of queues, including ready queues and device queues*/
            for(int i=0; i<size; i++)
            {
                temp=queue[i].get_queue();
                int size1=temp.size();
                for(int j=0; j<size1; j++)
                {
                    process=temp.at(j);
                    check=process.get_pid();
                    if(check==number)
                    {
                        cout<<"Process found in a queue and being erased..."<<endl;
                        queue[i].remove_process(j);
                        float cputime=process.get_burst();
                        float burstime=process.get_average();
                        cout<<"Process information:"<<endl;
                        cout<<"PID: "<<number<<endl<<"Acc CPU time: "<<cputime<<endl<<"Average burst time: "<<burstime<<endl;
                        cout<<"Process killed"<<endl<<endl;
                        return true;
                    }
                }
            }

            /*Checks all CPUs in ready queues*/
            for(int k=0; k<size; k++)
            {
                temp=queue[k].get_cpu();
                process=temp.front();
                check=process.get_pid();
                if(check==number)
                {
                    cout<<"Process found in a CPU and being erased..."<<endl;
                    float cputime=process.get_burst();
                    float burstime=process.get_average();
                    queue[k].clear_cpu();
                    cout<<"Process information:"<<endl;
                    cout<<"PID: "<<number<<endl<<"Acc CPU time: "<<cputime<<endl<<"Average burst time: "<<burstime<<endl;
                    cout<<"Process killed"<<endl<<endl;
                    return true;//ends if PID is found in CPU and returns true
                }
            }
            return false;
        }

        int main()
        {
            cout << "Sys gen starting..."<<endl;
            cout<<"Please enter the amount of CPUs (between 1 and 9)"<<endl;
            int cpu;
            cin>>cpu;
            while(cpu<1 || cpu>9)
            {
                cout<<"Invalid value, please re-enter amount:"<<endl;
                cin>>cpu;
            }

            cout<<"Enter amount of PRINTERs:"<<endl;
            int pr;
            cin>>pr;
            cout<<"Enter amount of  DISKs:"<<endl;
            int di;
            cin>>di;
            cout<<"Enter amount of CD/RWs:"<<endl;
            int cd;
            cin>>cd;
            int devices=(cpu*2)+pr+di+cd;//amount of devices(queues and CPU's)
            int PID=0;
            int PCB=0;
            vector<rq> RQ;  //vector of ready queues
            vector<rq> PQ; //vector of printer queues
            vector<rq> DQ; //vector of disk queues
            vector<rq> CQ; //vector of CD/RW queues

            load_queue(RQ, cpu);
            load_queue(PQ, pr); //load printer queues to PQ vector
            load_queue(DQ, di); // ...
            load_queue(CQ, cd); // ...


            cout << "Running section..." <<endl<< endl;
            cout<<"Start inputting commands; To terminate program press E or e at any time:"<<endl;
            while(true)
            {
                string device;//for name of device
                string enter;//for input
                cin>>enter;
                int correspond=0;
                int smaller;
                char num11=enter[0];//1st position character
                char input=enter[1];//2nd position  char
                char num=enter[2];//3rd position    char
                int num1=num11 -'0';//1st position integer
                int n=num -'0';//3rd position       int
                int inp=input - '0';//2nd position  int
                int empty;

                /*creates a new process and puts it into emptiest Ready Queue*/
                if(num11=='A')
                {
                    smaller=get_smaller_queue_pos(RQ);  //determines which ready queue has less processes 
                    RQ[smaller].create_process(smaller, PID, PCB);
                    PID++;
                    PCB++;
                    if(RQ[smaller].get_cpu().size() == 0)
                    {
                        RQ[smaller].cpu();
                    }
                }

                /*terminates a process from a particular CPU*/
                else if(input=='t') 
                {
                    if(num1>0 && num1<=cpu)
                    {
                        RQ[num1-1].terminate();
                        PCB--;
                        RQ[num1-1].cpu();
                    }
                    else 
                    {
                        cout<<"Invalid entry"<<endl;
                    }
                }

                /*Prints out a snapshot of a particular device Q or the ready queues*/
                else if(num11 == 'S')
                {
                    cout << "Select a queue: "<<endl;
                    char sele;
                    cin >> sele;
                    switch(sele)
                    {
                        case 'p':
                            if(pr==0)
                            {
                                cout<<"There are no printer queues created."<<endl;
                            }
                            else
                            {
                                device="Printer";
                                cout<<"A snapshot of your "<<device<<" queues:"<<endl<<"PCB\tPID\tLENGTH\tLOCATION\tFILE NAME\tUsed CPU time(ms)\tAverage Burst time(ms)"<<endl;
                                for(int i = 0; i < PQ.size(); i++)
                                {
                                    cout<<device<<" "<<i+1<<":"<<endl;
                                    PQ.at(i).snapshot(device);
                                }
                            }
                            break;
                        case 'r':
                            cout<<endl<<"___A Snapshot of your Ready Queue(s)___"<<endl;
                            for(int i = 0; i < RQ.size(); i++)
                            {
                                cout<<"Ready queue "<<i+1<<":"<<endl;
                                RQ[i].snapshot();
                            }
                                break;
                        case 'd':
                            if(di==0)
                            {
                                cout<<"There are no disk queues created."<<endl;
                            }
                            else
                            {
                                device="Disk";
                                cout<<"A snapshot of your "<<device<<" queues:"<<endl<<"PCB\tPID\tLENGTH\tLOCATION\tFILE NAME\tUsed CPU time(ms)\tAverage Burst time(ms)"<<endl;
                                for(int i = 0; i < PQ.size(); i++)
                                {
                                    cout<<device<<" "<<i+1<<":"<<endl;
                                    DQ.at(i).snapshot(device);
                                }                       
                            }
                            break;
                        case 'c':
                            if(cd==0)
                            {
                                cout<<"There are no cd/rw queues created."<<endl;
                            }
                            else
                            {
                                device="Cd/rw";
                                cout<<"A snapshot of your "<<device<<" queues:"<<endl<<"PCB\tPID\tLENGTH\tLOCATION\tFILE NAME\tUsed CPU time(ms)\tAverage Burst time(ms)"<<endl;
                                for(int i = 0; i < PQ.size(); i++)
                                {
                                    cout<<device<<" "<<i+1<<":"<<endl;
                                    CQ.at(i).snapshot(device);
                                }
                            }
                            break;
                        default:
                            cout << "No such queue; wrong option." << endl;
                            break;
                    }
                }
                /*moves a process from a particular CPU into a particular printer queue*/
                else if(input=='p')
                {
                    if(n>pr)
                    {
                        cout<<"You are exceding the amount of queues available for this device."<<endl;
                    }
                    else  
                    {

                        /*start of push */
                        vector <proc> temp1;
                        int big;
                        temp1=RQ[num1-1].get_cpu();             
                        if (temp1.empty())
                        {
                            big=get_biggest_queue_pos(RQ);
                            temp1=RQ[big].get_front();
                            temp1.at(0).change_q(num1-1);
                            RQ[num1-1].enter_to_rq(temp1.front());
                            RQ[num1-1].cpu();
                        }
                        /*end of push */

                        int ind=1;// to determines it's a printer when asking for parameters
                        vector <proc> temp;
                        temp=RQ[num1-1].get_cpu();
                        int nn=temp.at(0).get_first();//to determine if process already has parameters (if nn=1)
                        if(nn==0)
                        {
                            RQ[num1-1].get_info(ind);//input process parameters
                            RQ[num1-1].bur_info();
                            temp=RQ[num1-1].get_cpu();
                        }
                        else
                        {
                            RQ[num1-1].bur_info();
                            temp=RQ[num1-1].get_cpu();
                        }
                        RQ[num1-1].clear_cpu();
                        proc te(0,0,0);
                        te=temp.front();
                        PQ[n-1].enter_to_rq(te);
                    }
                }
                /*moves a process from a particular CPU into a particular disk queue*/
                else if(input=='d')
                {
                    if(n>di)
                    {
                        cout<<"You are exceding the amount of queues available for this device."<<endl;
                    }
                    else 
                    {   
                        /*start of push */
                        vector <proc> temp1;
                        int big;
                        temp1=RQ[num1-1].get_cpu();             
                        if (temp1.empty())
                        {
                            big=get_biggest_queue_pos(RQ);
                            temp1=RQ[big].get_front();
                            temp1.at(0).change_q(num1-1);
                            RQ[num1-1].enter_to_rq(temp1.front());
                            RQ[num1-1].cpu();
                        }
                        /*end of push */

                        int ind=0;// to determine it's a printer when asking for parameters
                        vector <proc> temp;
                        temp=RQ[num1-1].get_cpu();
                        int nn=temp.at(0).get_first();//to determine if process already has parameters (if nn=1)
                        if(nn==0)
                        {
                            RQ[num1-1].get_info(ind);//input process parameters
                            RQ[num1-1].bur_info();
                            temp=RQ[num1-1].get_cpu();
                        }
                        else
                        {
                            RQ[num1-1].bur_info();
                            temp=RQ[num1-1].get_cpu();
                        }               
                        RQ[num1-1].clear_cpu();
                        proc te(0,0,0);
                        te=temp.front();
                        DQ[n-1].enter_to_rq(te);
                    }
                }

                /*moves a process from a particular CPU into a particular cd/rw queue*/
                else if(input=='c')
                { 
                    if(n>pr)
                    {
                        cout<<"You are exceding the amount of queues available for this device."<<endl;
                    }
                    else  
                    {
                        /*start of push */
                        vector <proc> temp1;
                        int big;
                        temp1=RQ[num1-1].get_cpu();             
                        if (temp1.empty())
                        {
                            big=get_biggest_queue_pos(RQ);
                            temp1=RQ[big].get_front();
                            temp1.at(0).change_q(num1-1);
                            RQ[num1-1].enter_to_rq(temp1.front());
                            RQ[num1-1].cpu();
                        }
                        /*end of push */


                        int ind=0;// to determine it's a printer when asking for parameters
                        vector <proc> temp;
                        temp=RQ[num1-1].get_cpu();
                        int nn=temp.at(0).get_first();//to determine if process already has parameters (if nn=1)
                        if(nn==0)
                        {
                            RQ[num1-1].get_info(ind);//input process parameters
                            RQ[num1-1].bur_info();
                            temp=RQ[num1-1].get_cpu();
                        }
                        else
                        {
                            RQ[num1-1].bur_info();
                            temp=RQ[num1-1].get_cpu();
                        }
                        RQ[num1-1].clear_cpu();
                        proc te(0,0,0);
                        te=temp.front();
                        CQ[n-1].enter_to_rq(te);
                    }
                }
                /*moves a process from a particular printer queue to it's ready queue*/
                else if(num11=='P')
                {
                    if(enter=="P")
                    {
                        cout<<"missing values"<<endl;
                    }
                    else if(inp>pr)
                    {
                        cout<<"Invalid input."<<endl;
                    }
                    else if(PQ[inp-1].tamano()==0)
                    {
                        cout<<"There are no more processes in the Queue"<<endl;
                    }
                    else
                    {
                        vector<proc> temp;
                        temp=PQ[inp-1].get_device();
                        proc te(0,0,0);
                        te=temp.front();
                        correspond=te.get_q();
                        RQ[correspond].enter_to_rq(te);
                    }
                }
                /*moves a process from a particular disk queue to it's ready queue*/
                else if(num11=='D')
                {
                    if(enter=="D")
                    {
                        cout<<"missing values"<<endl;
                    }
                    else if(inp>di)
                    {
                        cout<<"Invalid input."<<endl;
                    }
                    else if(DQ[inp-1].tamano()==0)
                    {
                        cout<<"There are no more processes in the Queue"<<endl;
                    }
                    else
                    {
                        vector<proc> temp;
                        temp=DQ[inp-1].get_device();
                        correspond=DQ[inp-1].queue();
                        proc te(0,0,0);
                        te=temp.front();
                        correspond=te.get_q();
                        RQ[correspond].enter_to_rq(te);
                    }
                }
                /*moves a process from a particular cr/rw queue to it's ready queue*/
                else if(num11=='C')
                {
                    if(enter=="C")
                    {
                        cout<<"missing values"<<endl;
                    }
                    else if(inp>cd)
                    {
                        cout<<"Invalid input."<<endl;
                    }
                    else if(CQ[inp-1].tamano()==0)
                    {
                        cout<<"There are no more processes in the Queue"<<endl;
                    }
                    else
                    {
                        vector<proc> temp;
                        temp=CQ[inp-1].get_device();
                        correspond=PQ[inp-1].queue();
                        proc te(0,0,0);
                        te=temp.front();
                        correspond=te.get_q();
                        RQ[correspond].enter_to_rq(te);
                    }
                }
                /*terminates program*/
                else if(num11=='E' || num11=='e')
                {
                    return 0;
                }

                /*Moves process from CPU to it's ready queue*/
                else if(input=='T')
                {
                    if(enter=="T")
                    {
                            cout<<"missing values"<<endl;
                    }
                    else if (num1>cpu)
                    {
                        cout<<"Invalid entry, no such CPU."<<endl;
                    }
                    else if (num1==0)
                    {
                        cout<<"Invalid entry, no such CPU."<<endl;
                    }
                    else 
                    {
                        vector<proc> temp;
                        temp=RQ[num1-1].get_cpu();
                        RQ[num1-1].cpu_clear();
                        proc te(0,0,0);
                        te=temp.at(0);
                        RQ[num1-1].enter_to_rq(te);
                        RQ[num1-1].clear_cpu();
                    }
                }
                /*Kills a process determined by it's PID*/
                else if(num11=='K')
                {
                if(enter=="K")
                {
                    cout<<"missing values"<<endl;
                }
                else
                {   
                    /*Borrowed code to convert a string into an int*/       
                    /*converts the rest of numbers into a separate string*/
                    int n=enter.length();
                    char number[n-1];
                    for (int j=0; j<n; j++)
                    {
                        number[j]=enter[j+1];
                    }
                    string final=number;
                    /*end of conversion, final= string containing PID*/

                    /*convert the string final into an int*/
                    int id;
                    stringstream test(final);
                    test>>id;
                    /*end of conversion, id= int containing PID*/
                    /*End of Borrowed code to convert a string into an int*/

                    if (id > PID)   
                    {
                        cout<<"Invalid PID, no such process"<<endl;
                    }
                    else
                    {
                        /*Check if PID is in a Q or CPU and delete process*/
                        bool buscar;
                        buscar=get_pid_process(RQ, id);
                        if(buscar==false)
                        {
                            buscar=get_pid_process(PQ, id);
                        }
                        if(buscar==false)
                        {
                            buscar=get_pid_process(DQ, id);
                        }
                        if(buscar==false)
                        {
                            buscar=get_pid_process(CQ, id);
                        }
                        if(buscar==false)
                        {
                            cout<<"Process not found."<<endl;
                        }
                    }
                }
                }

                /*If invalid input*/
                else
                {
                    cout<<"Invalid input, re-enter command."<<endl<<endl;
                }
            }
            return 0;
        }

答案 4 :(得分:0)

过程控制块

每个流程都有一个流程控制块,包含有关流程的所有信息。它是一种数据结构,包含以下内容:

流程状态 - 它可以正在运行,等待等。

进程ID和父进程ID。

CPU寄存器和程序计数器。程序计数器保存要为该过程执行的下一条指令的地址。

CPU调度信息 - 例如优先级信息和指向调度队列的指针。

内存管理信息 - 例如。页表或段表。

会计信息 - 消耗的用户和内核CPU时间,帐号,限制等

I / O状态信息 - 分配的设备,打开的文件表等

要了解详细信息,我建议您从这里学习    https://coderworld109.blogspot.in/2017/12/process-control-block-pcb.html    https://coderworld109.blogspot.in/2017/12/operating-system-process-management.html