实现AB进程对话。
a. A进程发送一句话,B进程接收到打印。然后B进程发送一句话, A进程接收后打印
b.复上述步骤。直到AB接收或者发送完quit后,结束AB进程
#include "head.h"
struct msgbuf
{
long mtype;
char mtext[128];
};
int main(int argc, const char *argv[])
{
key_t key=ftok("/home/ubuntu/",1);
if(key<0)
{
perror("ftok");
return -1;
}
printf("key=%#x\n",key);
int did=msgget(key,IPC_CREAT | 0664);
if(did<0)
{
perror("msgget");
return -1;
}
struct msgbuf snd;
struct msgbuf rcv;
while(1)
{
size_t res;
printf("消息类型>>>");
scanf("%ld",&snd.mtype);
getchar();
printf("消息内容>>>");
fgets(snd.mtext,sizeof(snd.mtext),stdin);
snd.mtext[strlen(snd.mtext)-1]='\0';
res = msgsnd(did,&snd,sizeof(snd.mtext),0);
if(strcmp(snd.mtext,"quit")==0)
break;
if(strcmp(snd.mtext,"")==0 )
break;
if(res<0)
{
perror("msgsnd");
return -1;
}
printf("发送成功\n");
res = msgrcv(did,&rcv,sizeof(rcv.mtext),2,0);
if(strcmp(rcv.mtext,"quit")==0 )
break;
if(strcmp(rcv.mtext,"")==0 )
break;
if(res<0)
{
perror("msgrcv");
return -1;
}
printf("userB:%s\n",rcv.mtext);
}
if(msgctl(did,IPC_RMID,NULL)<0)
{
perror("msgctl");
return -1;
}
return 0;
}
#include "head.h"
struct msgbuf
{
long mtype;
char mtext[128];
};
int main(int argc, const char *argv[])
{
key_t key=ftok("/home/ubuntu/",1);
if(key<0)
{
perror("ftok");
return -1;
}
printf("key=%#x\n",key);
int did=msgget(key,IPC_CREAT | 0664);
if(did<0)
{
perror("msgget");
return -1;
}
struct msgbuf rcv;
struct msgbuf snd;
while(1)
{
size_t res;
res = msgrcv(did,&rcv,sizeof(rcv.mtext),1,0);
if(strcmp(rcv.mtext,"quit")==0)
break;
if(strcmp(rcv.mtext,"")==0 )
break;
if(res<0)
{
perror("msgrcv");
return -1;
}
printf("userA:%s\n",rcv.mtext);
printf("消息类型>>>");
scanf("%ld",&snd.mtype);
getchar();
printf("消息内容>>>");
fgets(snd.mtext,sizeof(snd.mtext),stdin);
snd.mtext[strlen(snd.mtext)-1]='\0';
res = msgsnd(did,&snd,sizeof(snd.mtext),0);
if(strcmp(snd.mtext,"quit")==0)
break;
if(strcmp(snd.mtext,"")==0 )
break;
if(res<0)
{
perror("msgsnd");
return -1;
}
printf("发送成功\n");
}
if(msgctl(did,IPC_RMID,NULL)<0)
{
perror("msgctl");
return -1;
}
return 0;
}
运行结果
要求在共享内存中存入字符串“1234567"。A进程循环打印字符串,B进程循环倒置字符串,要求结果不允许出现乱序:提示:共享内存+信号灯集
#include "head.h"
#include "sys/sem.h"
int main(int argc, const char *argv[])
{
key_t key=ftok("/home/ubuntu/",1);
if(key<0)
{
perror("fork");
return -1;
}
int did=semget(key,2,IPC_CREAT | 0664);
if(did<0)
{
perror("semget");
return -1;
}
int shid=shmget(key,128,IPC_CREAT|0664);
if(shid<0)
{
perror("shmget");
return -1;
}
printf("shid=%d\n",shid);
void* add = shmat(shid,NULL,0);
if(add == (void*)-1)
{
perror("shmat");
return -1;
}
while(1)
{
char* i=(char*)add;
char* j;
j=i+(strlen((char*)add)-1);
struct sembuf sops;
sops.sem_num=0;
sops.sem_op=-1;
sops.sem_flg=0;
if(semop(did,&sops,1)<0)
{
return -1;
}
while(i
#include "head.h"
#include "sys/sem.h"
int main(int argc, const char *argv[])
{
key_t key=ftok("/home/ubuntu/",1);
if(key<0)
{
perror("fork");
return -1;
}
int did=semget(key,2,IPC_CREAT | 0664);
if(did<0)
{
perror("semget");
return -1;
}
int shid=shmget(key,128,IPC_CREAT|0664);
if(shid<0)
{
perror("shmget");
return -1;
}
printf("shid=%d\n",shid);
void* add = shmat(shid,NULL,0);
if(add == (void*)-1)
{
perror("shmat");
return -1;
}
printf("add=%p\n",add);
char* prr=(char*)add;
strcpy(prr,"1234567");
while(1)
{
struct sembuf sops;
sops.sem_num=0;
sops.sem_op=+1;
sops.sem_flg=0;
if(semop(did,&sops,1)<0)
{
return -1;
}
printf("add=%s\n",(char*)add);
sops.sem_num=1;
sops.sem_op=-1;
sops.sem_flg=0;
if(semop(did,&sops,1)<0)
{
return -1;
}
}
return 0;
}
运行结果