操作系统:进程通信模拟,C语言实现

第一题:用C语言利用循环队列模拟实现生产者消费者问题(不考虑同步)。buffer[LEN]数组为缓冲区,用于存储产品(消息)及模拟循环数组的数据结构。打印的菜单中需要加入个人学号姓名等信息,可以参考提供的代码进行实现。
代码如下:

`
#include<stdio.h>
#include<stdlib.h>

#define LEN 5 

int inPointer = 0;
int outPointer = 0;
int buffer[LEN];
int productNo = 0;
int bufferProductCount = 0;

void printMenu();
void Product(int buffer[], int* inPointer, int* outPointer, int* Pro);
void Comsume(int buffer[], int* inPointer, int* outPointer, int* Pro);
int isBufferFull(int* inPointer, int* outPointer, int* produrctNo);
int isBufferEmpty(int* inPointer, int* outPointer, int* productNo);
void PrintCurrentBuffer(int buffer[]);

int main()
{
	char userOpt;
	while (1)
	{
		printMenu();
		scanf_s("%c", &userOpt, sizeof(userOpt));
		getchar();
		switch (userOpt)
		{
		case '1':
			Product(buffer, &inPointer, &outPointer, &productNo);
			break;
		case '2':
			Comsume(buffer, &inPointer, &outPointer, &productNo);
			break;
		case'3':
			printf("输出缓冲区状态\n");
			PrintCurrentBuffer(buffer);
			break;
		case'q':
			return 0;
			break;
		}
	}
}

void printMenu()
{
	printf("按1生产一个产品\n");
	printf("按2消费一个产品\n");
	printf("按3显示缓冲区信息\n");
	printf("按q退出\n");
	printf("  作者信息\n");
	printf("您的选择:\n");
}
void Product(int buffer[], int* inPointer, int* outPointer, int* productNo)
{
	if (!isBufferFull(inPointer, outPointer, productNo))
	{
		buffer[*inPointer] = 1;
		(*productNo)++;
		(*inPointer)++;
		
		*inPointer = *inPointer % 5;
		
	}
	else
	{
		printf("缓冲区已满,无法生产\n");
	}
}

void Comsume(int buffer[], int* inPointer, int* outPointer, int* productNo)
{
	if (!isBufferEmpty(inPointer, outPointer, productNo))
	{
		buffer[*outPointer] = 0;
		(*outPointer)++;
		
			*outPointer = (*outPointer) % 5;
		
		
		
	}
	else
	{
		printf("缓冲区是空的\n");
	}
}

int isBufferFull(int* inPointer, int* outPointer, int* productNo)
{//满的话返回1 不满返回0
	if (*productNo == 0)
	{//第一次生产返回为不满
		return 0;
	}
	if ((*inPointer++)%5 != *outPointer)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

int isBufferEmpty(int* inPointer, int* outPointer, int* productNo)
{//空返回1,非空返回0
	if (productNo != 0)
	{
		return 0;
	}
	if (*inPointer%5 == *outPointer)
	{
		return 1;
	}
	else
	{
		return 0;
	}

}

void PrintCurrentBuffer(int buffer[])
{
	int i;
	for (i = 0;i < 5;i++)
	{
		printf("%d ", buffer[i]);
	}
	printf("\n");
}
`

第二题:模拟通过消息队列通信。假设有两个进程A和B,A和B分别有一个本进程接收到的消息队列,实现一个C语言程序,用链式存储实现A和B相互发送消息,A向B发送消息即将新消息插入到B消息队列的尾部,B接收消息即从消息队列的对头取出一个消息打印在屏幕上(消息队列为空的话显示空),A或B也可以一次性把所有发送给自己的消息接收(显示出来)。每个消息内容需要由用户手动输入一段文字。可以根据以下程序完成,打印的菜单中需要加入个人学号姓名等信息。
代码如下

public class Node {
    public String data;
    public int id;
    public Node next;

    Node(){

    }

    Node(String data,int id)
    {
        this.data=data;
        this.id=id;
    }

}

public class MessageQue {
    private Node head;

    MessageQue() {
        head = new Node();
        head.next = null;
        head.id = 0;
    }

    public boolean IsEmpty() {
        if (head.next == null) {
            return true;
        } else {
            return false;
        }
    }

    public void Send(String data, int id) {
        Node node = new Node(data, id);
        node.next = head.next;
        head.next = node;
    }

    public boolean Read() {
        if (IsEmpty() == false) {
            System.out.println("MessageIs:" + head.next.data);
            return true;
        } else {
            System.out.println("NoMessage");
            return false;
        }
    }

    public boolean readAllMessage(){
        if (IsEmpty() == false) {
            Node node = new Node();
            node = head.next;
            while (node != null) {
                System.out.println("消息号:" + node.id + "消息为:" + node.data);
                node = node.next;
            }
            return true;
        } else {
            System.out.println("当前无消息");
            return false;
        }
    }

}

import java.util.Scanner;
public class Process {
    String No;
    MessageQue msg;
    public  void sendMsg(int No){
        Scanner sc = new Scanner(System.in);
        System.out.println("消息为:");
        msg.Send(sc.nextLine(),No);
    }
    Process(){

    }
    Process(String No,MessageQue msg){
        this.msg=msg;
        this.No=No;
    }

}
import java.util.Scanner;
public class Test {
    public static void main(String[] args){
        MessageQue MQA=new MessageQue();
        MessageQue MQB=new MessageQue();
        Process PA=new Process("A",MQA);
        Process PB = new Process("B",MQB);
        menu(PA,PB);
    }

    public static void menu(Process PA,Process PB){
        int idA=1;
        int idB=1;
        while(true){
            printMenu();
            Scanner sc=new Scanner(System.in);
            switch (sc.nextInt()){
                case 1:
                    PB.sendMsg(idB);
                    idB++;
                    break;
                case 2:
                    PA.sendMsg(idA);
                    idA++;
                    break;
                case 3:
                    PA.msg.readAllMessage();
                    break;
                case 4:
                    PB.msg.readAllMessage();
                    break;
                case 5:
                    PA.msg.Read();
                    break;
                case 6:
                    PB.msg.Read();
                    break;
                case 7:
                    System.out.println("A消息队列:");
                    PA.msg.readAllMessage();
                    System.out.println("B消息队列:");
                    PB.msg.readAllMessage();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("非法输入");

            }
        }
    }

    public static void printMenu(){
        System.out.println("按1 A发送给B消息");
        System.out.println("按2 B发送给A消息");
        System.out.println("按3 显示A的消息队列");
        System.out.println("按4 显示B的消息队列");
        System.out.println("按5 A接收一个消息");
        System.out.println("按6 B接收一个消息");
        System.out.println("按7 显示A,B的消息队列");
        System.out.println("    作者信息");
        System.out.println("按0退出");
    }

}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值