文档库

最新最全的文档下载
当前位置:文档库 > 东北大学 操作系统实验三报告

东北大学 操作系统实验三报告

操作系统实验报告

东北大学 操作系统实验三报告

东北大学 操作系统实验三报告

班级物联网1302班

学号

姓名

实验3 进程同步和通信-生产者和消费者问题模拟1. 目的:

调试、修改、运行模拟程序,通过形象化的状态显示,使学生理解进程的概念,了解同步和通信的过程,掌握进程通信和同步的机制,特别是利用缓冲区进行同步和通信的过程。通过补充新功能,使学生能灵活运用相关知识,培养创新能力。

2. 内容及要求:

1) 调试、运行模拟程序。

2) 发现并修改程序中不完善的地方。

3) 修改程序,使用随机数控制创建生产者和消费者的过程。

4) 在原来程序的基础上,加入缓冲区的写互斥控制功能,模拟多个进程存取一个公共缓冲区,当有进程正在写缓冲区时,其他要访问该缓冲区的进程必须等待,当有进程正在读取缓冲区时,其他要求读取的进程可以访问,而要求写的进程应该等待。

5) 完成1)、2)、3)功能的,得基本分,完成4)功能的加2分,有其它功能改进的再加2分

3. 程序说明:

本程序是模拟两个进程,生产者(producer)和消费者(Consumer)工作。生产者每次产生一个数据,送入缓冲区中。消费者每次从缓冲区中取走一个数据。缓冲区可以容纳8个数据。因为缓冲区是有限的,因此当其满了时生产者进程应该等待,而空时,消费者进程应该等待;当生产者向缓冲区放入了一个数据,应唤醒正在等待的消费者进程,同样,当消费者取走一个数据后,应唤醒正在等待的生产者进程。就是生产者和消费者之间的同步。

每次写入和读出数据时,都将读和写指针加一。当读写指针同样时,又一起退回起点。当写指针指向最后时,生产者就等待。当读指针为零时,再次要读取的消费者也应该等待。

为简单起见,每次产生的数据为0-99的整数,从0开始,顺序递增。两个进程的调度是通过运行者使用键盘来实现的。

4. 程序使用的数据结构

进程控制块:包括进程名,进程状态和执行次数。

缓冲区:一个整数数组。

缓冲区说明块:包括类型,读指针,写指针,读等待指针和写等待指针。

5. 程序使用说明

启动程序后,如果使用'p'键则运行一次生产者进程,使用'c'键则运行一次消费者进程。通过屏幕可以观察到两个进程的状态和缓冲区变化的情况。

6.实验流程图

开始

初始化进程控制块

初始化生产者进程

初始化消费者进程

进程调度

调度生产者进

程?

调度消费者进程?

n n

生产者处于阻塞状态

结束调度?结束n

y

y

n

结束y

缓存区已满?

生产者进程被阻

y

n 消费者处于阻塞

y

结束

y

缓冲区为空?

n

n

消费者被阻塞

往缓冲区添加一个数据生产者添加数据指

针移到下一个位置下一个位置>8下一个位置值0有消费者进程被阻塞调用消费者进程n

y

y

消费缓冲区里的一个数据

有生产者进程被阻塞调用生产者进程

消费者进程消费指针移到下一个位置

消费者进程指针与生产者进程指针相同两个指针均置0

y 下一位置置0

下一个位置>8n y

n y n n

缓冲区可用?y

阻塞该生产者n 缓冲区可用?阻塞该消费者

n y

源程序

#include

#include

#include

#define PIPESIZE 8

enum Status { RUN, WAIT, READY };

#define NORMAL 0

#define SLEEP 1

#define AWAKE 2

struct PCB

{

char name[3];

enum Status status;

int time;

};

struct waitqueue

{

struct PCB pcb;

struct waitqueue *next;

};

struct PCB *producer; /* write wait point */

struct PCB *consumer; /* read wait point */ struct waitqueue *headerqueue, *tailqueue;

int writeptr;

int readptr;

int writein, readout;

int buffer[PIPESIZE];

int empty, full;

void runp(char in[3]), runc(char in[3]), print();

int main()

{

char in[3];

writeptr = 0;

readptr = 0;

writein = 0;

empty = PIPESIZE;

full = 0;

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

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

headerqueue = (struct waitqueue *)malloc(sizeof(struct waitqueue));

headerqueue->next = NULL;

tailqueue = headerqueue;

producer->status = READY;

consumer->status = WAIT;

producer->time = consumer->time = 0;

printf("Now starting the program!\n");

printf("Press 'p1' to run PRODUCER1,Press 'p1' to run PRODUCER2 \n");

printf("Press 'c1' to run CONSUMER1,Press 'c2' to run CONSUMER2 \n");

printf("Press 'e' to exit from the program.\n");

while (1)

{

strcpy(in, "N");

while (!strcmp(in, "N"))

{

printf("\n");

scanf("%s", in);

if (strcmp(in, "e") && strcmp(in, "p1") && strcmp(in, "p2") && strcmp(in, "c1") && strcmp(in, "c2"))

{

printf("error,please input again!!!\n");

strcpy(in, "N");

}

}

if ('p' == in[0])

{

runp(in);

producer->time++;

}

else if ('c' == in[0])

{

runc(in);

consumer->time++;

}

else

{

//printf("PRODUCER product %d times\n", producer->time);

//printf("CONSUMER consumer %d times\n", consumer->time);

exit(0);

}

print();

printf("\n");

}

}

void runp(char in[3])

{

if (full >= 8)

{

struct waitqueue *search;

search = headerqueue->next;

while (search != NULL)

{

if (!strcmp(in, search->http://www.wendangku.net/doc/acc169ecb90d6c85ed3ac6a8.html))

{

printf("error!!!\n");

exit(1);

}

search = search->next;

}

producer->status = WAIT;

printf("PRODUCER %s process is waiting, can't be scheduled.\n", in);

struct waitqueue *p = (struct waitqueue *)malloc(sizeof(struct waitqueue));

strcpy(p->http://www.wendangku.net/doc/acc169ecb90d6c85ed3ac6a8.html, in);

p->pcb.status = WAIT;

p->pcb.time = producer->time + 1;

p->next = NULL;

tailqueue->next = p;

tailqueue = p;

full++;

}

else

{

writein = (writein + 1) % 100;

producer->status = RUN;

printf("run PRODUCER %s process . product %d ", in, writein);

buffer[writeptr] = writein;

if (empty>8)

{

struct waitqueue *p;

p = headerqueue->next;

printf("run CONSUMER %s process. use %d", p->http://www.wendangku.net/doc/acc169ecb90d6c85ed3ac6a8.html, buffer[writeptr]);

if (tailqueue == p)

{

tailqueue = headerqueue;

}

headerqueue->next = p->next;

free(p);

consumer->status = WAIT;

empty--;

}

else

{

if (writeptr > readptr)

{

writeptr++;

if (writeptr >= PIPESIZE)

{

writeptr = 0;

if (readptr == 0)

producer->status = WAIT;

else producer->status = READY;

}

}

else

{

writeptr++;

if (writeptr == readptr)

producer->status = WAIT;

else producer->status = READY;

}

consumer->status = READY;

empty--;

full++;

}

}

}

void runc(char in[3])

{

if (empty >= 8)

{

struct waitqueue *search;

search = headerqueue->next;

while (search != NULL)

{

if (!strcmp(in, search->http://www.wendangku.net/doc/acc169ecb90d6c85ed3ac6a8.html))

{

printf("error!!!\n");

exit(1);

}

search = search->next;

}

consumer->status = WAIT;

printf("CONSUMER %s is waiting, can't be scheduled.\n",in);

struct waitqueue *p = (struct waitqueue *)malloc(sizeof(struct waitqueue));

strcpy(p->http://www.wendangku.net/doc/acc169ecb90d6c85ed3ac6a8.html, in);

p->pcb.status = WAIT;

p->pcb.time = consumer->time + 1;

p->next = NULL;

tailqueue->next = p;

tailqueue = p;

empty++;

}

else

{

consumer->status = RUN;

readout = buffer[readptr];

printf("run CONSUMER %s process. use %d ", in, readout);

if (full>8)

{

writein = (writein + 1) % 100;

buffer[writeptr] = writein;

struct waitqueue *p;

p = headerqueue->next;

printf("run PRODUCER %s process. product %d ", p->http://www.wendangku.net/doc/acc169ecb90d6c85ed3ac6a8.html, buffer[writeptr]);

if (tailqueue == p)

{

tailqueue = headerqueue;

}

headerqueue->next = p->next;

free(p);

producer->status = WAIT;

full--;

writeptr++;

readptr++;

}

else

{

if (readptr > writeptr)

{

readptr++;

if (readptr >= PIPESIZE)

{

readptr = 0;

if (writeptr == 0)

consumer->status = WAIT;

else

consumer->status = READY;

}

else consumer->status = READY;

}

else

{

readptr++;

if (readptr == writeptr)

{

consumer->status = WAIT;

writeptr = readptr = 0;

}

else consumer->status = READY;

producer->status = READY;

}

full--;

empty++;

}

}

}

void print()

{

int i = 0;

int j = 0;

int low = 0;

int high = 0;

printf("\n");

for (i; i < PIPESIZE; i++)

printf("--------");

printf("\n");

if (readptr < writeptr)

{

for (low = 0; low < readptr; low++)

printf("| |");

for (low = readptr; low < writeptr; low++) {

printf("| %2d |", buffer[low]);

}

for (low = writeptr; low

}

else if (readptr > writeptr)

{

for (low = 0; low < writeptr; low++)

printf("| %2d |", buffer[low]);

for (low = writeptr; low < readptr; low++) printf("| |");

for (low = readptr; low < PIPESIZE; low++) {

printf("| %2d |", buffer[low]);

}

}

else if (producer->status == WAIT)

{

for (low = 0; low < PIPESIZE; low++)

{

printf("| %2d |", buffer[low]);

}

}

else

{

for (low = 0; low < PIPESIZE; low++)

{

printf("| |");

}

}

printf("\n");

for (i = 0; i < PIPESIZE; i++)

printf("--------");

printf("\n");

if (producer->status == WAIT && NULL !=headerqueue->next)

{

struct waitqueue *p = headerqueue->next;

while (NULL != p)

{

printf(" PRODUCER %s process are waitting\n", p->http://www.wendangku.net/doc/acc169ecb90d6c85ed3ac6a8.html);

p = p->next;

}

}

else

printf("PRODUCER ready \n");

if (consumer->status == WAIT &&headerqueue->next)

{

struct waitqueue *p = headerqueue->next;

while (NULL != p)

{

printf(" CONSUMER %s process are waitting\n", p->http://www.wendangku.net/doc/acc169ecb90d6c85ed3ac6a8.html);

p = p->next;

}

}

else

printf("CONSUMER ready\n ");

}

实验结果:

东北大学 操作系统实验三报告

东北大学 操作系统实验三报告

东北大学 操作系统实验三报告

东北大学 操作系统实验三报告

东北大学 操作系统实验三报告