操作系统

   

信息工程学院

   

13计算机信息管理

   

   

2015

6

1

实验报告

班级:         姓名:        学号(后两位):   日期:15  61

实验名称

实验一:进程管理

实验目的

1、 加深对进程概念的理解,明确进程和程序的区别。

2、 进一步认识并发执行的实质。

3、 分析进程争用资源的现象,学习解决进程互斥的方法。

实验环境

PC兼容机

Windows系统、语言自选

实验内容(算法、程序、步骤和方法)

1创建新的进程;

2、查看运行进程;

3、换出某个进程;

4、杀死运行进程以及进程之间通信等功能。

部分代码:

#include

#include

#include

#define NULL 0 

int shumu=0;

struct node

{

    int a;

    char ch;

};

struct jincheng

{

    int pid;

    int youxian;

    float luntime;

    float zhantime;

    char zhuangtai;       

    node *neirong;

    struct jincheng *next;

};

struct jincheng *neijin,*neizhi,*p,*q;

void huanchu(int a)

{

    p=neijin;

    while(p->pid!=a&&p!=NULL)

    {

        q=p;

        p=p->next;

    }

    if(p==NULL)

    {

        printf("该进程不在内存里!\n");

        return;

    }

if(p==neijin)

    {

        neijin=neijin->next;

    }

    else

    {

        q->next=p->next;         

    }

}

//杀死进程

void shasi()

{

    neizhi->next=NULL;

    printf("运行的进程已经杀死!\n");

    return;

}

int bijiao()

{

    int i,j;

    p=neijin;

    while(p!=NULL)

    {

        q=p;

        p=p->next;                

    }

    i=q->youxian;                      

    j=neizhi->next->youxian;           

    if(i>j)                             

    {

        p=neijin;

        if(p==q)                     

        {

            neijin=neizhi->next;      

            p->neirong=(node*)malloc(sizeof(node));

            p->neirong->a=9;

            p->neirong->ch='c';

            neizhi->next=p;        

            return 1;

        }

        else{                          

        

            while(p->next!=q)

            {

                p=p->next;

            }                            

            p->next=neizhi->next;        

            q->neirong=(node*)malloc(sizeof(node));

            q->neirong->a=9;

            q->neirong->ch='c';

            neizhi->next=q;               

            neizhi->next->next=NULL;

            return 1;

        }

    }

    else

        return -1;

}

void main()

{

    int zhixing();//定义函数

    void shasi();

    void chakan();

    void tongxing(int);

    neizhi=(jincheng*)malloc(sizeof(jincheng));

    neizhi->next=NULL;

    neijin=(jincheng*)malloc(sizeof(jincheng)); 

    neijin->next=NULL;

        neijin->pid=1;

    neijin->youxian=6;

    neijin->luntime=3.5;

    neijin->zhantime=3;

        neijin->neirong=(node*)malloc(sizeof(node));

    neijin->neirong=NULL;

    neijin->zhuangtai='b';

    shumu++;

    p=(jincheng*)malloc(sizeof(jincheng));

    p->next=neijin->next;

    neijin->next=p;

        p->pid=2;

    p->youxian=5;

    p->luntime=3.5;

    p->zhantime=3;

    p->neirong=(node*)malloc(sizeof(node));

    p->neirong=NULL;

    p->zhuangtai='b';

    shumu++;

        q=(jincheng*)malloc(sizeof(jincheng));

    q->next=p->next;

    p->next=q;

    q->pid=3;

    q->youxian=4;

    q->luntime=3.5;

    q->zhantime=3;

q->neirong=(node*)malloc(sizeof(node));

    q->neirong=NULL;

    q->zhuangtai='b';

    shumu++;

    int i,n=1;

    int k,j,s;

    j=zhixing();

    int creat();

    while(n==1)

    {

        printf("$¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥$\n");

        printf("               进程演示系统               \n");

        printf("$¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥$\n");

        printf("     1.创建新的进程      2.查看运行进程 \n");    

        printf("     3.换出某个进程      4.杀死运行进程 \n");    

        printf("     5.进程之间通信      6.退出系统 \n");        

        printf("$¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥$\n");

        printf("请选择(16\n");

        scanf("%d",&i);

        switch(i)

        {

        case 1:k=creat();

        if(k==1)

        printf("进程创建成功!\n");

        if(neijin->next==NULL)

        {

            printf("由于只有一个进程所以为它分配处理机.\n");

            neizhi->next=neijin;

            neijin->neirong=(node*)malloc(sizeof(node));

            neijin->neirong->a=3;

            neijin->neirong->ch='c';

            neijin=NULL;

            continue;

        }

        k=bijiao();

        if(k==1)

{

            printf("由于新进程的优先级高于正在执行的进程所以正在执行的\n");

            printf("进程让出处理机交给新进程,而它变为活动就绪!\n");

        }

     

int zhixing()

{

        int i,j;

        p=neijin;

    

        if(neizhi->next!=NULL)

        {    

            return -1;

        }    

        i=neijin->youxian;                  

        p=neijin->next;                     

        while(p!=NULL) 

        {   j=p->youxian;                  

            if(i>=j)

            {

                p=p->next;           

            }

            if(i

            {

                i=p->youxian;                           

                p=p->next;                              

            }                                               

        }

        if(neijin->youxian==i)                                      

        {

            neijin->neirong=(node*)malloc(sizeof(node));

            neijin->neirong->a=9;

            neijin->neirong->ch='c';

            neizhi->next=neijin;

            neijin=neijin->next;

            neizhi->next->next=NULL;

        }

else

        {

            p=neijin;

            while(i!=p->youxian)

            {

                q=p;

                p=p->next;

            }                                                 

            p->neirong=(node*)malloc(sizeof(node));

            p->zhuangtai='a';

            p->neirong->a=9;

            p->neirong->ch='c';

            neizhi->next=p;                                  

            q->next=p->next;                               

        }

        return 1;

}

void chakan()

{

    p=neizhi->next;

    printf("该执行进程的名字为:%d\n",p->pid);

    printf("该执行进程的的优先级:%d\n",p->youxian);

    printf("该执行进程的轮转时间为:%f\n",p->luntime);

    printf("该执行进程占用cpu的时间为:%f\n",p->zhantime);

    printf("该执行的进程内容为:\n");

    printf("%d  ",p->neirong->a);

    printf("%c",p->neirong->ch);

    printf("\n");

}

void tongxing(int a)

{

    q=neijin;

    while(q->pid!=a&&q!=NULL)

    {

        q=q->next;

    }

    if(q==NULL)

    {

        printf("所输入的进程不在内存中!\n");

        return ;

    }

    p=neizhi->next;

    q->neirong=(node*)malloc(sizeof(node));

    q->neirong->a=p->neirong->a;

    q->neirong->ch=p->neirong->ch;

    printf("通信成功!\n");

    return;

}

运行结果:

创建进程:

查看进程:

换出进程:

杀死进程:

进程通信:

小结

通过实验懂得了操作系统的基本原理和概念!!!

备注或说明

本次实验得分

1