作业
题目一
1.要求用消息队列实现AB进程对话
a.A进程先发送一句话给B进程,B进程接收后打印
b.B进程再回复一句话给A进程,A进程接收后打印
c.重复1.2步骤,当收到quit后,要结束AB进程
A进程
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<head.h>
struct msgbuf
{
long mtype;
char mtext[128];
};
int main(int argc, const char *argv[])
{
key_t key = ftok("./",10);
if(key < 0)
{
perror("ftok");
return -1;
}
int msqid = msgget(key,IPC_CREAT|0664);
if(msqid < 0)
{
perror("msgget");
return -1;
}
struct msgbuf sndbuf;
ssize_t res = 0;
while(1)
{
sndbuf.mtype = 1;
bzero(sndbuf.mtext,sizeof(sndbuf.mtext));
printf("请输入消息内容");
fgets(sndbuf.mtext,sizeof(sndbuf.mtext),stdin);
sndbuf.mtext[strlen(sndbuf.mtext)-1] = 0;
if(msgsnd(msqid,&sndbuf,sizeof(sndbuf.mtext),0) < 0)
{
perror("msgsnd");
return -1;
}
if(strcmp(sndbuf.mtext,"quit") == 0)
break;
printf("发送成功\n");
bzero(sndbuf.mtext,sizeof(sndbuf.mtext));
res = msgrcv(msqid,&sndbuf,sizeof(sndbuf.mtext),2,0);
if(res < 0)
{
perror("msgrcv");
return -1;
}
if(strcmp(sndbuf.mtext,"quit") == 0)
break;
printf("%s\n",sndbuf.mtext);
}
return 0;
}
B进程
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<head.h>
struct msgbuf
{
long mtype;
char mtext[128];
};
int main(int argc, const char *argv[])
{
key_t key = ftok("./",10);
if(key < 0)
{
perror("ftok");
return -1;
}
int msqid = msgget(key,IPC_CREAT|0664);
if(msqid < 0)
{
perror("msgget");
return -1;
}
struct msgbuf sndbuf;
ssize_t res = 0;
while(1)
{
bzero(sndbuf.mtext,sizeof(sndbuf.mtext));
res = msgrcv(msqid,&sndbuf,sizeof(sndbuf.mtext),1,0);
if(res < 0)
{
perror("msgrcv");
return -1;
}
if(strcmp(sndbuf.mtext,"quit") == 0)
break;
printf("%s\n",sndbuf.mtext);
sndbuf.mtype = 2;
bzero(sndbuf.mtext,sizeof(sndbuf.mtext));
printf("请输入消息内容");
fgets(sndbuf.mtext,sizeof(sndbuf.mtext),stdin);
sndbuf.mtext[strlen(sndbuf.mtext)-1] = 0;
if(msgsnd(msqid,&sndbuf,sizeof(sndbuf.mtext),0) < 0)
{
perror("msgsnd");
return -1;
}
if(strcmp(sndbuf.mtext,"quit") == 0)
break;
printf("发送成功\n");
}
return 0;
}
题目二
在上一题基础上,实现随时收发:用多进程 多线程
A进程
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<head.h>
int msqid = 0;
struct msgbuf
{
long mtype;
char mtext[128];
};
void* soo(void* arg)
{
struct msgbuf buf;
ssize_t res = 0;
while(1)
{
res = msgrcv(msqid,&buf,sizeof(buf.mtext),2,0);
if(res < 0)
{
perror("msgrcv");
return NULL;
}
if(strcmp(buf.mtext,"quit") == 0)
exit(1);
printf("%s\n",buf.mtext);
bzero(buf.mtext,sizeof(buf.mtext));
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
key_t key = ftok("./",2);
if(key < 0)
{
perror("ftok");
return -1;
}
msqid = msgget(key,IPC_CREAT|0664);
if(msqid < 0)
{
perror("msgget");
return -1;
}
pthread_t tid;
if(pthread_create(&tid,NULL,soo,NULL) != 0)
{
perror("pthread_create");
return -1;
}
struct msgbuf buf;
while(1)
{
bzero(buf.mtext,sizeof(buf.mtext));
buf.mtype = 1;
printf("A\t");
fgets(buf.mtext,sizeof(buf.mtext),stdin);
buf.mtext[strlen(buf.mtext)-1] = 0;
if(msgsnd(msqid,&buf,sizeof(buf.mtext),0) < 0)
{
perror("msgsnd");
return -1;
}
if(strcmp(buf.mtext,"quit") == 0)
exit(1);
}
pthread_join(tid,NULL);
return 0;
}
B进程
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<head.h>
int msqid = 0;
struct msgbuf
{
long mtype;
char mtext[128];
};
void* soo(void* arg)
{
struct msgbuf buf;
while(1)
{
bzero(buf.mtext,sizeof(buf.mtext));
buf.mtype = 2;
printf("B\t");
fgets(buf.mtext,sizeof(buf.mtext),stdin);
buf.mtext[strlen(buf.mtext)-1] = 0;
if(msgsnd(msqid,&buf,sizeof(buf.mtext),0) < 0)
{
perror("msgsnd");
return NULL;
}
if(strcmp(buf.mtext,"quit") == 0)
exit(1);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
key_t key = ftok("./",2);
if(key < 0)
{
perror("ftok");
return -1;
}
msqid = msgget(key,IPC_CREAT|0664);
if(msqid < 0)
{
perror("msgget");
return -1;
}
pthread_t tid;
if(pthread_create(&tid,NULL,soo,NULL) != 0)
{
perror("pthread_create");
return -1;
}
struct msgbuf buf;
ssize_t res = 0;
while(1)
{
bzero(buf.mtext,sizeof(buf.mtext));
res = msgrcv(msqid,&buf,sizeof(buf.mtext),1,0);
if(res < 0)
{
perror("msgrcv");
return -1;
}
if(strcmp(buf.mtext,"quit") == 0)
exit(1);
printf("%s\n",buf.mtext);
}
pthread_join(tid,NULL);
return 0;
}
题目三
- 要求在共享内存中存入字符串 “1234567”。A进程循环打印字符串,B进程循环倒置字符串,要求结果不允许出现乱序:
提示:共享内存中存储 flag + string.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<head.h>
int main(int argc, const char *argv[])
{
key_t key = ftok("./",3);
if(key < 0)
{
perror("ftok");
return -1;
}
int shmid = shmget(key,32,IPC_CREAT|0664);
if(shmid < 0)
{
perror("shmget");
return -1;
}
void* addr = shmat(shmid,NULL,0);
if(addr < 0)
{
perror("shmat");
return -1;
}
int *p1 = (int*)addr;
*p1 = 1;
char* p2 = (char*)addr + 4;
char s[] = "1234567";
strcpy(p2,s);
pid_t cpid = fork();
if(cpid >0)
{
while(1)
{
if(*p1 == 1)
{
printf("%s\n",(char*)addr+4);
*p1 = 0;
}
}
}
else if(cpid == 0)
{
while(1)
{
if(*p1 == 0)
{
for(int i = 0; i < strlen(s)/2; i++)
{
char c = *(p2+i) ; *(p2+i) = *(p2+strlen(s)-1-i) ; *(p2+strlen(s)-1-i) = c;
}
*p1 = 1;
}
}
}
else
{
perror("fork");
return -1;
}
return 0;
}
思维导图