1、AB进程能够随时收发数据
A进程:
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<unistd.h>
#include<sys/msg.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
struct msgbuf
{
long mtype;
char mtext[30];
};
pthread_t fp1;
pthread_t fp2;
void *func1(void*arg)
{
struct msgbuf pp;
while(1)
{
printf("请输入消息类型>>>>>>");
fflush(stdout);
scanf("%ld",&pp.mtype);
while(getchar()!='\n');
printf("请输入信息>>>>>>");
fgets(pp.mtext,sizeof(pp.mtext),stdin);
pp.mtext[strlen(pp.mtext)-1] = '\0';
if(msgsnd(*(int*)arg,&pp,sizeof(pp.mtext),0)<0)
{
perror("msgsnd");
exit(0);
}
if(strcmp(pp.mtext,"quit")==0)
{
exit(0);
}
}
}
void *func2(void*arg)
{
struct msgbuf pp;
ssize_t res = 0;
while(1)
{
res = msgrcv(*(int*)arg,&pp,sizeof(pp.mtext),0,0);
if(res<0)
{
perror("msgsnd");
return NULL;
}
printf("\n");
printf("B消息类型 %ld\n",pp.mtype);
printf("B:%s\n",pp.mtext);
if(strcmp(pp.mtext,"quit")==0)
{
exit(0);
}
sleep(1);
}
}
int main(int argc,const char *argv[])
{
key_t key1 = ftok("./",1);
if(key1<0)
{
perror("ftok'");
return -1;
}
int msqid1 = msgget(key1,IPC_CREAT|0664);
if(msqid1<0)
{
perror("msgget");
return -1;
}
key_t key2 = ftok("./",2);
if(key2<0)
{
perror("ftok'");
return -1;
}
int msqid2 = msgget(key2,IPC_CREAT|0664);
if(msqid2<0)
{
perror("msgget");
return -1;
}
// system("ipcs -q");
pthread_create(&fp1,NULL,func1,(void*)&msqid1);
pthread_create(&fp2,NULL,func2,(void*)&msqid2);
pthread_join(fp1,NULL);
pthread_join(fp2,NULL);
return 0;
}
B进程:
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<unistd.h>
#include<sys/msg.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
struct msgbuf
{
long mtype;
char mtext[30];
};
pthread_t fp1;
pthread_t fp2;
void *func1(void*arg)
{
struct msgbuf pp;
while(1)
{
printf("请输入消息类型>>>>>>");
scanf("%ld",&pp.mtype);
while(getchar()!='\n');
printf("请输入信息>>>>>>");
fgets(pp.mtext,sizeof(pp.mtext),stdin);
pp.mtext[strlen(pp.mtext)-1] = '\0';
if(msgsnd(*(int*)arg,&pp,sizeof(pp.mtext),0)<0)
{
perror("msgsnd1");
exit(0);
}
if(strcmp(pp.mtext,"quit")==0)
{
exit(0);
}
}
}
void *func2(void*arg)
{
struct msgbuf pp;
ssize_t res = 0;
while(1)
{
sleep(1);
res = msgrcv(*(int*)arg,&pp,sizeof(pp.mtext),0,0);
if(res<0)
{
perror("msgsnd2");
return NULL;
}
printf("\n");
printf("A消息类型 %ld\n",pp.mtype);
printf("A:%s\n",pp.mtext);
if(strcmp(pp.mtext,"quit")==0)
{
exit(0);
}
}
}
int main(int argc,const char *argv[])
{
key_t key1 = ftok("./",2);
if(key1<0)
{
perror("ftok'");
return -1;
}
int msqid1 = msgget(key1,IPC_CREAT|0664);
if(msqid1<0)
{
perror("msgget");
return -1;
}
key_t key2 = ftok("./",1);
if(key2<0)
{
perror("ftok'");
return -1;
}
int msqid2 = msgget(key2,IPC_CREAT|0664);
if(msqid2<0)
{
perror("msgget");
return -1;
}
// system("ipcs -q");
pthread_create(&fp1,NULL,func1,(void*)&msqid1);
pthread_create(&fp2,NULL,func2,(void*)&msqid2);
pthread_join(fp1,NULL);
pthread_join(fp2,NULL);
return 0;
}
2、A进程写入一个整型,在该整型后,写入一个字符串
B进程将共享内存中的整型以及字符串读取出来
A进程:
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/unistd.h>
#include<sys/msg.h>
#include<sys/shm.h>
#include<string.h>
int main(int argc, const char *argv[])
{
key_t key = ftok("./",100);
if(key < 0)
{
perror("ftok");
return -1;
}
size_t size = 500;
int pp = shmget(key,size,IPC_CREAT|0664);
typedef struct
{
int num;
char str[20];
}h;
h*a = shmat(pp,NULL,0);
printf("请输入要写入的整形:");
scanf("%d",&(a->num));
printf("请输入字符串:");
char arr[20];
scanf("%s",arr);
strcpy(a->str,arr);
return 0;
}
演示结果:
B进程:
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/unistd.h>
#include<sys/msg.h>
#include<sys/shm.h>
#include<string.h>
int main(int argc, const char *argv[])
{
key_t key = ftok("./",100);
if(key < 0)
{
perror("ftok");
return -1;
}
size_t size = 500;
int pp = shmget(key,size,IPC_CREAT|0664);
typedef struct{
int num;
char str[20];
}h;
h *p =shmat(pp,NULL,0);
printf("%d %s ",p->num,p->str);
return 0;
}
演示结果: