目录
重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程
2.在第一题的基础上实现,AB进程能够随时收发数据(附加题)
1.要求AB进程做通信(使用队列)
-
A进程发送一句话,B进程接收打印
-
然后B进程发送给A进程一句话,A进程接收打印
-
重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程
2.在第一题的基础上实现,AB进程能够随时收发数据(附加题)
A进程
#include <stdio.h>
#include <sys/types.h>
#include <pthread.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
//消息包结构体
struct msgbuf
{
long mtype; //消息类型,必须大于0
char mtext[128]; //消息内容,该成员类型可变
};
off_t len;
struct msgbuf rcv;
ssize_t res =0;
struct msgbuf snd;
int msqid;
int msqid2;
void* callBack1(void* arg)
{
while(1)
{
//发送消息包
printf("请输入消息类型>>>");
scanf("%ld", &snd.mtype);
getchar(); //吸收垃圾字符
printf("请输入>>>");
fgets(snd.mtext, sizeof(snd.mtext), stdin);
snd.mtext[strlen(snd.mtext)-1] = 0;
if(strcasecmp(snd.mtext, "quit") == 0)
{
exit(0);
}
//阻塞方式发送,消息队列满了,阻塞
if(msgsnd(msqid, &snd, sizeof(snd.mtext), 0) < 0)
{
perror("msgsnd");
return NULL;
}
printf("A消息包发送成功\n");
}
}
void* callBack2(void* arg)
{
while(1)
{
res=msgrcv(msqid2,&rcv,sizeof(rcv.mtext),0,0);
if(strcasecmp(snd.mtext, "quit") == 0)
{
exit(0);
}
if(res<0)
{
perror("msgrcv");
return NULL;
}
printf("B res=%ld [%ld : %s]\n",res,rcv.mtype,rcv.mtext);
}
}
int main(int argc, const char *argv[])
{
//计算key值
key_t key = ftok("./",1);
if(key < 0)
{
perror("ftok");
return -1;
}
printf("key = %#x\n", key);
//计算key值
key_t key2 = ftok("./",2);
if(key2 < 0)
{
perror("ftok");
return -1;
}
printf("key2 = %#x\n", key2);
//创建一个消息队列
msqid = msgget(key, IPC_CREAT|0664);
if(msqid < 0)
{
perror("msgget");
return -1;
}
printf("msqid = %d\n", msqid);
//创建一个消息队列
msqid2 = msgget(key2, IPC_CREAT|0664);
if(msqid2 < 0)
{
perror("msgget");
return -1;
}
printf("msqid2 = %d\n", msqid2);
//创建一个线程
pthread_t tid1;
if(pthread_create(&tid1, NULL, callBack1,NULL) != 0)
{
perror("pthread_create");
return -1;
}
//创建一个线程,
pthread_t tid2;
if(pthread_create(&tid2, NULL, callBack2, NULL) != 0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2,NULL);
return 0;
}
B进程
#include <stdio.h>
#include <sys/types.h>
#include <pthread.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
//消息包结构体
struct msgbuf
{
long mtype; //消息类型,必须大于0
char mtext[128]; //消息内容,该成员类型可变
};
off_t len;
struct msgbuf rcv;
ssize_t res =0;
struct msgbuf snd;
int msqid;
int msqid2;
void* callBack1(void* arg)
{
while(1)
{
res=msgrcv(msqid,&rcv,sizeof(rcv.mtext),0,0);
if(strcasecmp(snd.mtext, "quit") == 0)
{
exit(0);
}
if(res<0)
{
perror("msgrcv");
return NULL;
}
printf("A res=%ld [%ld : %s]\n",res,rcv.mtype,rcv.mtext);
}
}
void* callBack2(void* arg)
{
while(1)
{
//发送消息包
printf("请输入消息类型>>>");
scanf("%ld", &snd.mtype);
getchar(); //吸收垃圾字符
printf("请输入>>>");
fgets(snd.mtext, sizeof(snd.mtext), stdin);
snd.mtext[strlen(snd.mtext)-1] = 0;
if(strcasecmp(snd.mtext, "quit") == 0)
{
exit(0);
}
//阻塞方式发送,消息队列满了,阻塞
if(msgsnd(msqid2, &snd, sizeof(snd.mtext), 0) < 0)
{
perror("msgsnd");
return NULL;
}
printf("B消息包发送成功\n");
}
}
int main(int argc, const char *argv[])
{
//计算key值
key_t key = ftok("./",1);
if(key < 0)
{
perror("ftok");
return -1;
}
printf("key = %#x\n", key);
//计算key值
key_t key2 = ftok("./",2);
if(key2 < 0)
{
perror("ftok");
return -1;
}
printf("key2 = %#x\n", key2);
//创建一个消息队列
msqid = msgget(key, IPC_CREAT|0664);
if(msqid < 0)
{
perror("msgget");
return -1;
}
printf("msqid = %d\n", msqid);
//创建一个消息队列
msqid2 = msgget(key2, IPC_CREAT|0664);
if(msqid2 < 0)
{
perror("msgget");
return -1;
}
printf("msqid2 = %d\n", msqid2);
//创建一个线程
pthread_t tid1;
if(pthread_create(&tid1, NULL, callBack1,NULL) != 0)
{
perror("pthread_create");
return -1;
}
//创建一个线程
pthread_t tid2;
if(pthread_create(&tid2, NULL, callBack2, NULL) != 0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2,NULL);
return 0;
}
3.
-
A进程写入一个整型,在该整型后,写入一个字符串
-
B进程将共享内存中的整型以及字符串读取出来;
写入共享内存的进程
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
//创建key值
key_t key = ftok("./", 'a');
if(key < 0)
{
perror("ftok");
return -1;
}
printf("key = %#x\n", key);
//创建共享内存
int shmid = shmget(key, 32, IPC_CREAT|0664);
if(shmid < 0)
{
perror("shmget");
return -1;
}
printf("shmid = %d\n", shmid);
//共享内存的映射
void* shmaddr = shmat(shmid, NULL, 0);
int* pint = (int*)shmaddr;
*pint=1;
char* pstr = (char*)(pint+1);
strcpy(pstr, "hello world");
system("ipcs -m");
return 0;
}
输出内容的进程
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
//创建key值
key_t key = ftok("./", 'a');
if(key < 0)
{
perror("ftok");
return -1;
}
printf("key = %#x\n", key);
//创建共享内存
int shmid = shmget(key, 32, IPC_CREAT|0664);
if(shmid < 0)
{
perror("shmget");
return -1;
}
printf("shmid = %d\n", shmid);
//共享内存的映射
void* shmaddr = shmat(shmid,NULL ,0);
int* pint=(int*)shmaddr;
char* pstr = (char*)(pint+1);
// strcpy(pstr, "hello world");
printf("%d ",*pint);
printf("%s\n",pstr);
return 0;
}