1.使用消息列表完成AB程序间通信
2.使用共享内存在A写入整形与字符串在B读出
习题1
分配两个消息列表在AB程序中分别在进行读写,可分进程完成也可分线程完成。
本题中选用线程完成该功能
程序A
#include <stdio.h>
#include <pthread.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <stdlib.h>
#include <sys/msg.h>
#include <string.h>
key_t key1,key2;
struct msgbuf
{
long mtype;
char mtext[20];
}snd,rev;
int msqid1,msqid2;
void *fa(void*arg)
{
while(1)
{
bzero(snd.mtext,sizeof(snd.mtext));
printf("size:");
scanf("%ld",&snd.mtype);
getchar();
printf("输入>>");
fgets(snd.mtext,sizeof(snd.mtext),stdin);
snd.mtext[strlen(snd.mtext)-1]=0;
if (msgsnd(msqid1,&snd,sizeof(snd.mtext),0)<0)
{
perror("msgsnd");
return NULL;
}
if (strcmp(snd.mtext,"quit")==0)
{
exit(0);
}
printf("消息包发送成功\n");
}
pthread_exit(NULL);
}
void *shou (void*arg)
{
while(1)
{
if (msgrcv(msqid2,&rev,sizeof(rev.mtext),0,0)<0)
{
perror("msgrcv");
return NULL;
}
if (strcmp((rev.mtext),"quit")==0)
{
break;
}
printf(" \n%ld %s\n",rev.mtype ,rev.mtext);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
key_t key1= ftok("./",1);
if (key1<0)
{
perror("ftok");
return -1;
}
printf("key1 = %#x \n",key1);
msqid1 = msgget(key1,IPC_CREAT| 0664);
if (msqid1<0)
{
return -1;
}
key_t key2= ftok("./",2);
if (key2<0)
{
perror("ftok");
return -1;
}
printf("key2 = %#x \n",key2);
msqid2 = msgget(key2,IPC_CREAT| 0664);
if (msqid2<0)
{
return -1;
}
pthread_t pid1,pid2;
if (pthread_create(&pid1,NULL,fa,NULL)<0)
{
return -1;
}
if (pthread_create(&pid2,NULL,shou,NULL)<0)
{
return -1;
}
pthread_join(pid1,NULL);
pthread_join(pid2,NULL);
system("ipcs -q");
return 0;
}
程序B
#include <stdio.h>
#include <pthread.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <stdlib.h>
#include <sys/msg.h>
#include <string.h>
key_t key1,key2;
struct msgbuf
{
long mtype;
char mtext[20];
}snd,rev;
int msqid2,msqid1;
void *fa(void*arg)
{
while(1)
{
bzero(snd.mtext,sizeof(snd.mtext));
printf("size:");
scanf("%ld",&snd.mtype);
getchar();
printf("输入>>");
fgets(snd.mtext,sizeof(snd.mtext),stdin);
snd.mtext[strlen(snd.mtext)-1]=0;
if (msgsnd(msqid2,&snd,sizeof(snd.mtext),0)<0)
{
perror("msgsnd");
return NULL;
}
if (strcmp(snd.mtext,"quit")==0)
{
exit(0);
}
printf("消息包发送成功\n");
}
pthread_exit(NULL);
}
void *shou (void*arg)
{
while(1)
{
if (msgrcv(msqid1,&rev,sizeof(rev.mtext),0,0)<0)
{
perror("msgrcv");
return NULL;
}
if (strcmp(rev.mtext,"quit")==0);
{
break;
}
printf(" \n%ld %s\n",rev.mtype ,rev.mtext);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
key_t key1= ftok("./",1);
if (key1<0)
{
perror("ftok");
return -1;
}
printf("key1 = %#x \n",key1);
msqid1 = msgget(key1,IPC_CREAT| 0664);
if (msqid1<0)
{
return -1;
}
key_t key2= ftok("./",2);
if (key2<0)
{
perror("ftok");
return -1;
}
printf("key2 = %#x \n",key2);
msqid2 = msgget(key2,IPC_CREAT| 0664);
if (msqid2<0)
{
return -1;
}
pthread_t pid1,pid2;
if (pthread_create(&pid1,NULL,fa,NULL)<0)
{
return -1;
}
if (pthread_create(&pid2,NULL,shou,NULL)<0)
{
return -1;
}
pthread_join(pid1,NULL);
pthread_join(pid2,NULL);
system("ipcs -q");
return 0;
}
结果测试
当写入quit时完成退出。
习题2
通过强制转化先将shmat返回的指针转化为整形,输入内容后在对指针进行加法运算将指针进行位移之后再通过strcmp输入字符串内容
程序A
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <stdio.h>
#include <stdio.h>
int main(int argc, const char *argv[])
{
char c[5]="www";
int a;
a=20;
key_t key = ftok("./",1);
int sh = shmget(key,50,IPC_CREAT|0644);
int*p=(int*)shmat(sh,NULL,0);
*p=a;
char*pc= (char*)(p+1);
strcpy(pc,c);
return 0;
}
程序B
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <stdio.h>
#include <stdio.h>
int main(int argc, const char *argv[])
{
key_t key = ftok("./",1);
int sh = shmget(key,50,IPC_CREAT|0644);
int*p=(int*)shmat(sh,NULL,0);
printf("整形:%d ",*p);
printf("字符串%s \n",(char*)(p+1));
return 0;
}
结果测试
再两个程序都编译运行后可见完成题目要求