1.用消息队列实时对话
//A程序
#include <head.h>
struct mymsg {
long mtype; /* Message type. */
char mtext[1]; /* Message text. */
};
pthread_t pid,pid1;//全局
int id; //定义消息队列的id号
void* READ(void* arg){
//循环读
char buf[128] = "";
long int data = 1;
struct mymsg sg;
sg.mtype = data;//只读1区从A过来的
while(1){
if(msgrcv(id,&sg,sizeof(buf),data,0) < 0){//循环查找
break;
}
//show read
printf("Aread->>>:%ld|%s\n",sg.mtype,sg.mtext);
if(0 == strcmp(sg.mtext,"quit")){//读到quit退出
break;
}
}
pthread_cancel(pid1);
pthread_exit(NULL);
}
void* WRITE(void* arg){
//循环写
char buf[128] = "";
long int data = 2;
struct mymsg sg;
sg.mtype = data;//只写入1区给b读到
while(1){
memset(buf,0,sizeof(buf));
printf("A write>>>");
scanf("%s",buf);
strcpy(sg.mtext,buf);
if(msgsnd(id,&sg,sizeof(buf),0) < 0){//传入并且判断是否正确
perror("msgsnd");
return NULL;
}
if(0 == strcmp(sg.mtext,"quit")){//写道quit退出
break;
}
}
pthread_cancel(pid);
pthread_exit(NULL);
}
int main(int argc,const char * argv[])
{
if(pthread_create(&pid,NULL,READ,NULL) < 0){//开线程1
printf("__%d__\n",__LINE__);
return -1;
}
if(pthread_create(&pid1,NULL,WRITE,NULL) < 0){//开线程2
printf("__%d__\n",__LINE__);
return -1;
}
key_t key;
key = ftok("/home/ubuntu",1);//得密钥
if(key < 0){
perror("ftok");
printf("__%d__\n",__LINE__);
return -1;
}
//printf("key = %#x\n",key);
id = msgget(key,IPC_CREAT |0664);//得消息队列id号
if(id < 0){
perror("msgget");
printf("__%d__\n",__LINE__);
return -1;
}
pthread_exit(NULL);
pthread_exit(NULL);
msgctl(id,IPC_RMID,NULL);//关闭消息队列
return 0;
}
//B程序
#include <head.h>
struct mymsg {
long mtype; /* Message type. */
char mtext[1]; /* Message text. */
};
pthread_t pid,pid1;//全局
int id; //定义消息队列的id号
void* READ(void* arg){
//循环读
char buf[128] = "";
long int data = 2;
struct mymsg sg;
sg.mtype = data;//只读2区从B写过来的
while(1){
if(msgrcv(id,&sg,sizeof(buf),data,0) < 0){//循环查找
break;
}
//show read
printf("Bread->>>:%ld",sg.mtype);
printf("|%s\n",sg.mtext);
if(0 == strcmp(sg.mtext,"quit")){//读到quit退出
break;
}
}
pthread_cancel(pid);
pthread_exit(NULL);
}
void* WRITE(void* arg){
//循环写
char buf[128] = "";
long int data = 1;
struct mymsg sg;
sg.mtype = data;//只写入1区给b读到
while(1){
memset(buf,0,sizeof(buf));
printf("B write>>>");
scanf("%s",buf);
strcpy(sg.mtext,buf);
if(msgsnd(id,&sg,sizeof(buf),0) < 0){//传入并且判断是否正确
perror("msgsnd");
return NULL;
}
if(0 == strcmp(sg.mtext,"quit")){//写道quit退出
break;
}
}
pthread_cancel(pid1);
pthread_exit(NULL);
}
int main(int argc,const char * argv[])
{
if(pthread_create(&pid,NULL,WRITE,NULL) < 0){//开线程1
printf("__%d__\n",__LINE__);
return -1;
}
if(pthread_create(&pid1,NULL,READ,NULL) < 0){//开线程2
printf("__%d__\n",__LINE__);
return -1;
}
key_t key;
key = ftok("/home/ubuntu",1);//得密钥
if(key < 0){
perror("ftok");
printf("__%d__\n",__LINE__);
return -1;
}
//printf("key = %#x\n",key);
id = msgget(key,IPC_CREAT |0664);//得消息队列id号
if(id < 0){
perror("msgget");
printf("__%d__\n",__LINE__);
return -1;
}
pthread_exit(NULL);
pthread_exit(NULL);
return 0;
}
2.逆置
//逆置
#include <head.h>
int main(int argc,const char * argv[])
{
//打开共享内存
key_t key = ftok("./",1);
if(key < 0){
perror("ftok");
return -1;
}
int id = shmget(key,32,IPC_CREAT|0664);
if(id < 0){
perror("shmget");
return -1;
}
//申请内存空间
void* addr = shmat(id,NULL,0);
if((void *)-1 == addr ){
perror("shmat");
return -1;
}
char temp = 0;
while(1){
if(1 == *(char *)addr){
int i = 0,j = strlen(((char*)addr+1))-1;
while(i<j){
temp = *((char*)addr+1+i);
*((char*)addr+1+i) = *((char*)addr+1+j);
*((char*)addr+1+j) = temp;
i++;
j--;
}
*(char *)addr = 0;
}
}
return 0;
}
//show
#include <head.h>
int main(int argc,const char * argv[])
{
//打开共享内存
key_t key = ftok("./",1);
if(key < 0){
perror("ftok");
return -1;
}
int id = shmget(key,32,IPC_CREAT|0664);
if(id < 0){
perror("shmget");
return -1;
}
//申请内存空间
void* addr = shmat(id,NULL,0);
if((void *)-1 == addr ){
perror("shmat");
return -1;
}
//存进共享内存
char str[8] = "1234567";
*(char *)addr = 0;
strcpy((char*)addr+1,str);
char buf[10] = "";
while(1){
if(0 == *((char *)addr)){
printf("%s\n",(char*)addr+1);
*((char *)addr)= 1;
}
}
return 0;
}