📄 processescommunication.cpp
字号:
/*程序模拟进程间通信,调试并运行成功*/
/*Leon Kyd*/
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define true 1
#define false 0
#define null 0
#define len sizeof(struct qnode)
struct qnode {
int data;
struct qnode *next;
};
struct qnode *createq() {
int i;
int qsize=10;
struct qnode *p1,*p2,*head;
head=p2=(struct qnode *)malloc(len);
head->data=-1;
for(i=1;i<qsize;++i) {
p1=(struct qnode *)malloc(len);
p2->next=p1;
p1->data=-1;
p2=p1;
}p1->next=null;
return(head);
}
struct qnode *gettail(struct qnode *head) {
struct qnode *p;
p=head;
do {
p=p->next;
}while(p->next);
return(p);
}
int qfull(struct qnode *tail) {
struct qnode *t;
t=tail;
if(t->data!=-1) return(true);
else return(false);
}
int qempty(struct qnode *head) {
struct qnode *p;
p=head;
if(p->data==-1) return(true);
else return(false);
}
void into(struct qnode *head,int *mes,int num) {
struct qnode *p;
int *m;
int n;
p=head;m=mes;n=num;
do {
if(p->data==-1) {
p->data=*(m+n);
break;
}p=p->next;
}while(p);
}
void out(struct qnode *head) {
struct qnode *p;
p=head;
do {
p->data=p->next->data;
p=p->next;
}while(p->data!=-1);
}
void qstate(struct qnode *head) {
struct qnode *p;
p=head;
printf("The current state of the queue is:\n\t<");
do {
printf("%d**",p->data);
p=p->next;
}while(p->next);
printf("%d>\n",p->data);
}
void main() {
int *mes;
int i,rnd,msize,num_into=0,num_out=0;
struct qnode *head,*tail;
printf("Please enter the size of the message array.\n");
scanf("%d",&msize);
printf("The data in each message are : \n");
mes=(int *)malloc(msize*sizeof(int));
for(i=0;i<msize;++i) {
*(mes+i)=rand()/1500+1;
printf("%d ",*(mes+i));
}printf("\n");
head=createq();
qstate(head);
tail=gettail(head);
do {
rnd=(rand()/2000) % 2;
printf("\nrnd=%d ",rnd);
if(rnd) {
if(num_into>msize-1) {
printf(" 所有消息都已进入过队列!\n");
continue;
}
if(qfull(tail)) {
printf("The queue is full! Input blocked! Wait!\n");
continue;
}
else {
printf(" 消息 %d 入队\n",*(mes+num_into));
into(head,mes,num_into);
++num_into;
qstate(head);
}
}
else {
if(num_out>msize-1) {
printf(" 所有消息都已出过队列!");
}
if(qempty(head)) {
printf("The queue is empty! Output blocked! Wait!\n");
continue;
}
else {
printf(" 消息 %d 出队\n",*(mes+num_out));
out(head);
++num_out;
qstate(head);
}
}
}while(num_into<msize || num_out<msize);
printf("\n所有消息都已进出过队列!\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -