1、要求用消息队列实现AB进程对话:
1)A进程发送一句话,B进程接收后打印;
2)B进程接着再发送一句话,A进程接收打印;
3)重复上述步骤,当A进程或者B进程接收到quit后退出AB进程。
实现AB进程能够随时收发。
A.c:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
pthread_t tid,pid;
#include <sys/ipc.h>
#include <sys/msg.h>
struct msgbuf
{
long mtype;//消息类型,必须 > 0
char mtext[128];//消息内容
};
int msqid;
void* callBack(void* arg)
{
ssize_t res=0;
struct msgbuf buf;
while(1)
{
memset(&buf,0,sizeof(buf));
res = msgrcv(msqid,&buf,sizeof(buf.mtext),30,0|020000);
if(res < 0)
{
perror("msgrcv");
break;
}
printf("接受到消息: text = %s !!\n",buf.mtext);
if(strncasecmp(buf.mtext,"quit",4) == 0)
{
break;
}
}
if(msgctl(msqid,IPC_RMID,NULL) < 0)
{
perror("msgctl");
pthread_exit(NULL);
}
printf("消息队列已删除!!\n");
pthread_cancel(pid);
pthread_exit(NULL);
}
void* callback(void* arg)
{
struct msgbuf buf;
while(1)
{
memset(&buf,0,sizeof(buf));
fgets(buf.mtext,sizeof(buf.mtext),stdin);
buf.mtype = 30;
buf.mtext[strlen(buf.mtext) -1] = '\0';
if(msgsnd(msqid,&buf,sizeof(buf.mtext),0) < 0)
{
perror("msgsnd");
break;
}
printf("消息发送成功!!\n");
if(strncasecmp(buf.mtext,"quit",4) == 0)
{
break;
}
}
pthread_cancel(tid);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//key值
key_t key = ftok("/home/ubuntu/",1);
if(key < 0)
{
perror("ftok");
return -1;
}
printf("key = %#x\n",key);
//创建消息队列,获取消息队列的id号
msqid = msgget(key,IPC_CREAT|0664);
if(msqid < 0)
{
perror("msgget");
return -1;
}
printf("msqid = %d\n",msqid);
if(pthread_create(&tid,NULL,callBack,NULL) != 0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&pid,NULL,callback,NULL) != 0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid,NULL);
pthread_join(pid,NULL);
return 0;
}
B.c:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
pthread_t tid,pid;
#include <sys/ipc.h>
#include <sys/msg.h>
struct msgbuf
{
long mtype;//消息类型,必须 > 0
char mtext[128];//消息内容
};
int msqid;
void* callBack(void* arg)
{
ssize_t res=0;
struct msgbuf buf;
while(1)
{
memset(&buf,0,sizeof(buf));
res = msgrcv(msqid,&buf,sizeof(buf.mtext),20,0|020000);
if(res < 0)
{
perror("msgrcv");
break;
}
printf("接受到消息: text = %s !!\n",buf.mtext);
if(strncasecmp(buf.mtext,"quit",4) == 0)
{
break;
}
}
if(msgctl(msqid,IPC_RMID,NULL) < 0)
{
perror("msgctl");
pthread_exit(NULL);
}
printf("消息队列已删除!!\n");
pthread_cancel(pid);
pthread_exit(NULL);
}
void* callback(void* arg)
{
struct msgbuf wbuf;
while(1)
{
memset(&wbuf,0,sizeof(wbuf));
fgets(wbuf.mtext,sizeof(wbuf.mtext),stdin);
wbuf.mtype = 20;
wbuf.mtext[strlen(wbuf.mtext) -1] = '\0';
if(msgsnd(msqid,&wbuf,sizeof(wbuf.mtext),0) < 0)
{
perror("msgsnd");
break;
}
printf("消息发送成功!!\n");
if(strncasecmp(wbuf.mtext,"quit",4) == 0)
{
break;
}
}
pthread_cancel(tid);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//key值
key_t key = ftok("/home/ubuntu/",1);
if(key < 0)
{
perror("ftok");
return -1;
}
printf("key = %#x\n",key);
//创建消息队列,获取消息队列的id号
msqid = msgget(key,IPC_CREAT|0664);
if(msqid < 0)
{
perror("msgget");
return -1;
}
printf("msqid = %d\n",msqid);
if(pthread_create(&tid,NULL,callBack,NULL) != 0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&pid,NULL,callback,NULL) != 0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid,NULL);
pthread_join(pid,NULL);
return 0;
}
2、创建两个进程A、B,以及一个共享内存,共享内存中存储char str[] = "123456",在不考虑进程退 出的情况下,要求如下:
1. A进程 循环 打印str字符串。
2. B进程 循环 倒置str字符串,不使用辅助数组。注意是循环倒置,要把字符串倒过来,倒回 去。
3. 要求A进程打印出来的结果是有序的,例如:"123456" 或者 "654321",不允许出 现"623451",,,等无序情况
提示:将flag + str一起写到共享内存中,当flag=0,打印 当flag=1 ,倒置
A.c:
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/types.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("fotk");
return -1;
}
printf("key = %d\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);
if((void*)-1 == shmaddr)
{
perror("shmat");
return -1;
}
printf("shmaddr = %p\n",shmaddr);
while(1)
{
if(1 == *(int*)shmaddr)
{
printf("%s\n",(char*)shmaddr+4);
*(int*)shmaddr = 0;
}
}
//断开映射
shmdt(shmaddr);
printf("断开映射!!\n");
system("ipcs -m");
return 0;
}
B.c:
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/types.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("fotk");
return -1;
}
printf("key = %d\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);
if((void*)-1 == shmaddr)
{
perror("shmat");
return -1;
}
printf("shmaddr = %p\n",shmaddr);
//存储一个int类型的整型数,
int* pa = (int*)shmaddr;
*pa = 1;
//在 int 后面存储一个字符串
char str[]="123456";
int len = strlen(str);
char* pb = (char*)pa+4;
strcpy(pb,str);
while(1)
{
if(0 == *(int*)shmaddr)
{
*pa=1;
for(int i = 0;i < len/2; i++ )
{
char temp = str[i];
str[i] = str[len-1-i];
str[len-i-1]=temp;
}
pb=(char*)pa+4;
strcpy(pb,str);
}
}
//断开映射
shmdt(shmaddr);
printf("断开映射!!\n");
system("ipcs -m");
return 0;
}