标题:模拟linux的进程创建
取消只看楼主
ldy1204
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2008-9-1
 问题点数:0 回复次数:0 
模拟linux的进程创建
#include "iostream.h"
#include "string.h"
#include "stdlib.h"
#define NUM 10


struct PCB{
    int id;     //进程标识符

    char * name;    //进程标识符
    char *status;    //进程状态

    PCB *next;            //当前队列指针
    PCB * all_q_next;  //总链指针
    int prio;        //进程优先级
    
}pcb[10];



PCB * ready_q_head=0;
PCB * all_q_head=0;    //总链头指针

void insert_all(PCB *);
void insert_ready(PCB *);

int search(char * name)   //查找有无同名PCB块
{
    PCB * p;
    p=(struct PCB *)malloc(sizeof(struct PCB));
    p=all_q_head;

    if(!p)       //队列为空
        return 1;

    cout<<"a";
    if (strcmp(p->name,name)&&(p!=0))  //名字不等  
        p=p->next;

    if(!p)    return 0;  //有同名

    return 1;     //无同名

}

int pcbcreate()
{

    PCB * pcb_addr;
    int i;
    int j;

    char name[10];
    cout<<"\ninput the process name you want to create:";


    cin>>name;

    pcb_addr=(struct PCB *)malloc(sizeof(struct PCB));

    cout<<"bb";

    if(!search(name))
    {
        cout<<"there has been this procss!\n";
        exit(-1);
    }

    for(i=0; (i<NUM)&&(pcb[i].id!=-1);i++) ; //查找空闲pcb块

    if (i==10)    //无空闲pcb块存在
    {
        cout<<"there has no empty pcbs!";
        exit(-1);
    }

    pcb_addr=&pcb[i] ;    //pcb块的地址
    if (!strcmp(name,"system"))
    { //如果是系统进程
        pcb_addr->id=1;
        strcpy(pcb_addr->status,"system");
        pcb_addr->prio=-127;
    }  
    else
     {//如果是用户进程
         pcb_addr->id=i;
         pcb_addr->name=name;
         strcpy(pcb_addr->status,"ready");
         cout<<"\ninput the priority of your process:\n";
         cin>>pcb_addr->prio;
     }
    insert_all(pcb_addr);
    insert_ready(pcb_addr);

    cout<<"create successfully!\n"
        <<"the informationa of the process are as following:\n"
        <<"id:"<<pcb_addr->id
        <<"name:"<<pcb_addr->name
        <<"priority:"<<pcb_addr->prio;

    return 1;
    
}

void insert_all(PCB * q)  //插入总链队列
{
    PCB * p;
    p=(struct PCB *)malloc(sizeof(struct PCB));
    p=all_q_head;
    while (p->all_q_next)
        p=p->all_q_next;
    p=q;
    q->all_q_next=0;
}

void insert_ready(PCB *q)//按优先级从小到大插入就绪队列
{
    PCB * p, *t ;
    p=(struct PCB *)malloc(sizeof(struct PCB));
    t=(struct PCB *)malloc(sizeof(struct PCB));
    p=t=ready_q_head;
    while (p->prio<q->prio)
    {
        t=p;
        p=p->next;
    }
    t->next=q;
    q->next=p;
}

void kill()    //假设通过进程名来撤销进程
{
    int idd;
    PCB * p,*t;
    p=(struct PCB *)malloc(sizeof(struct PCB));
    t=(struct PCB *)malloc(sizeof(struct PCB));

    cout<<"input the process id you want to kill";
    cin>>idd;

    p=ready_q_head;
    while (p->id!=idd)        //修改就绪队列指针队列

    {
        t=p;
        p=p->next;
    }
    t->next=p->next;
    
    p=all_q_head;          //修改总链指针
    while (p->id!=idd)
    {
        t=p;
        p=p->all_q_next;
    }
    t=p->all_q_next;
    
    delete p;
    
    cout<<"kill the process sucessfully!";
    
}
void main()
{
    for (int i=0; i<10; i++)
    {
        pcb[i].id=-1;
        pcb[i].name=0;
        pcb[i].next=0;
        pcb[i].prio=127;
        pcb[i].all_q_next=0;
        pcb[i].status=0;
    }
    pcbcreate();
    kill();
}

运行时出现调bug的,把main函数里面的赋值去掉就可以了,不知道为什么,麻烦哪位大虾帮忙看下啊,急啊!
收到的鲜花
  • sunkaidong2008-11-11 10:17 送鲜花  8朵   附言:很不错的东西,期待你把进程线程的东西都弄透 ...
搜索更多相关主题的帖子: linux 进程 模拟 
2008-11-10 22:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-243485-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.162517 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved