回 帖 发 新 帖 刷新版面

主题:等待中。。。。。。

有三个并发进程,R负责从输入设备读入信息并传送给M,M将信息加工并传送给R,R将打印
输出,写出下列条件下的并发程序。
(1)双缓冲区,每个区大小为K.
(2)单缓冲区,其大小为K.
大家用c语言可以编下这个程序么?

回复列表 (共4个回复)

沙发

我想你应该到C语言专区,那样会比较容易得到答案!!

板凳

属于生产者-消费者问题
#include<stdio.h>
#include<math.h>
int buffer[4];
int wait[100];
p()
{
    printf("1:producer\n");
    printf("2:consumer\n");
    printf("0:exit\n");
}

main()
{
    int a=1,empty=4,full=0,i=0,j=0,t=0,m,b,k=0;
    clrscr();
    p();
    while(a!=0)
    {
        printf("please input a number(0,1,2) to a: ");
        scanf("%d",&a);
        switch(a)
        {
            case 1:
            printf("input the producer's number: ");
            scanf("%d",&m);
            empty=empty-1;
            if(empty<0)
            {
                wait[t]=m;
                printf("the buffer is full\n");
                printf("%d is in wait queue\n",wait[t]);
                t=(t+1) % 100;
                break;
            }
    d:      buffer[i]=m;
            i=(i+1) % 4;
            full=full+1;
            if(full<=0)
                goto c;
            break;
   
            case 2:
            full=full-1;
            if(full<0)
            {
                printf("the buffer is empty\n");
                break;
            }
            else
    c:      {
                printf("the consumer is  ");
                b=buffer[j];
                printf("%d\n",b);
                j=(j+1) % 4;
                empty=empty+1;
                if(empty<=0)
                {
                    m=wait[k];
                    k=(k+1) % 100;
                    goto d;
                }
            }
            break;
            case 0:
            break;
        }
    }
}

操作系统课程设计做过
结构差不多 自己改成符合要求的代码吧

3 楼

楼上的你写的是什么东西啊?[em18]

4 楼

看起来好难啊!
[em10][em19]

我来回复

您尚未登录,请登录后再回复。点此登录或注册