1)要求AB进程做通信
- A进程发送一句话,B进程接收打印
- 然后B进程发送给A进程一句话,A进程接收打印
- 重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程;
2)在第一题的基础上实现,AB进程能够随时收发数据(附加题)
A进程:
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
#include<errno.h>
#include<signal.h>
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
struct msgbuf
{
long mtype;
char mtext[128];
};
int msqid;
void* send(void* arg)
{
struct msgbuf snd;
while(1)
{
snd.mtype =1;
printf("A发送的消息为:");
fgets(snd.mtext,sizeof(snd.mtext),stdin);
snd.mtext[strlen(snd.mtext)-1]=0;
msgsnd(msqid,&snd,sizeof(snd.mtext),0);
if(strcasecmp(snd.mtext,"quit")==0)
exit(0);
}
}
void* receive(void* arg)
{
struct msgbuf rcv;
while(1)
{
msgrcv(msqid,&rcv,sizeof(rcv.mtext),2,0);
printf("\rA收到的消息为:%s\n",rcv.mtext);
printf("A发送到消息:");
fflush(stdout);
if(strcasecmp(rcv.mtext,"quit")==0)
exit(0);
}
}
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 tid1,tid2;
if(pthread_create(&tid1,NULL,send,NULL)!=0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid2,NULL,receive,NULL)!=0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
system("ipcs -q");
return 0;
}
B进程:
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
#include<errno.h>
#include<signal.h>
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
struct msgbuf
{
long mtype;
char mtext[128];
};
int msqid;
void* send(void* arg)
{
struct msgbuf snd;
while(1)
{
snd.mtype=2;
printf("B发送的消息为:");
fgets(snd.mtext,sizeof(snd.mtext),stdin);
snd.mtext[strlen(snd.mtext)-1]=0;
msgsnd(msqid,&snd,sizeof(snd.mtext),0);
if(strcasecmp(snd.mtext,"quit")==0)
exit(0);
}
}
void* receive(void* arg)
{
struct msgbuf rcv;
while(1)
{
msgrcv(msqid,&rcv,sizeof(rcv.mtext),1,0);
printf("\rB收到的消息为:%s\n",rcv.mtext);
printf("B发送到消息:");
fflush(stdout);
if(strcasecmp(rcv.mtext,"quit")==0)
exit(0);
}
}
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 tid1,tid2;
if(pthread_create(&tid1,NULL,send,NULL)!=0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid2,NULL,receive,NULL)!=0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
system("ipcs -q");
return 0;
}
- A进程写入一个整型,在该整型后,写入一个字符串
- B进程将共享内存中的整型以及字符串读取出来;
A进程:
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int main(int argc, const char *argv[])
{
key_t key=ftok("./",3);
if(key<0)
{
perror("ftok");
return -1;
}
printf("key=%d\n",key);
int shmid=shmget(key,64,IPC_CREAT|0664);
if(shmid<0)
{
perror("shmget");
return -1;
}
void *p=shmat(shmid,NULL,0);
if(p==(void*)-1)
{
perror("shmat");
return -1;
}
printf("p=%p\n",p);
int *q=(int*)p;
printf("请输入一个整型:");
scanf("%d",q);
getchar();
char *str=(char*)(q+1);
printf("请输入一个字符串:");
scanf("%s",str);
getchar();
return 0;
}
B进程:
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int main(int argc, const char *argv[])
{
key_t key=ftok("./",3);
if(key<0)
{
perror("ftok");
return -1;
}
printf("key=%d\n",key);
int shmid=shmget(key,64,IPC_CREAT|0664);
if(shmid<0)
{
perror("shmget");
return -1;
}
void *p=shmat(shmid,NULL,0);
if(p==(void*)-1)
{
perror("shmat");
return -1;
}
printf("p=%p\n",p);
int *q=(int*)p;
printf("%d\n",*q);
char *str=(char*)(q+1);
printf("%s\n",str);
return 0;
}