1.使用消息队列完成两个进程之间相互通信
#include<myhead.h>
struct msgbuf
{
long mtype;
char mtext[1024];
};
struct heizi
{
long mtype;
char mtext[1024];
};
#define SIZE (sizeof(struct msgbuf)-sizeof(long))
#define SIZE1 (sizeof(struct heizi)-sizeof(long))
int main(int argc, const char *argv[])
{
pid_t pid=fork();
//子进程
if(pid ==0)
{
//创建key
key_t key;
if((key=ftok("/",'t'))==-1)
{
perror("ggg");
return -1;
}
//创建消息队列
int ikun=0;
if((ikun=msgget(key,IPC_CREAT|0664))==-1)
{
perror("ggg");
return -1;
}
printf("%d\n",ikun);
struct msgbuf buf;
while(1)
{
printf("要向2发送的数据:\n");
scanf("%s",buf.mtext);
getchar();
//向消息队列中传递
msgsnd(ikun,&buf,SIZE,0);
printf("发送成功\n");
if(strcmp(buf.mtext,"quit")==0)
{
break;
}
}
//关闭消息队列
if(msgctl(ikun,IPC_RMID,NULL) ==-1)
{
perror("ggg21");
return -1;
}
exit(EXIT_SUCCESS);
}
//父进程
else if(pid>0)
{
key_t key1;
if((key1=ftok("/",'y'))==-1)
{
perror("ggg");
return -1;
}
//创建消息队列
int kun=0;
if((kun=msgget(key1,IPC_CREAT|0664))==-1)
{
perror("ggg22");
return -1;
}
printf("%d\n",kun);
struct heizi buf1;
while(1)
{
msgrcv(kun,&buf1,SIZE1,0,0);
printf("收到2发来消息为: %s\n",buf1.mtext);
if(strcmp(buf1.mtext,"quit")==0)
{
break;
}
}
if(msgctl(kun,IPC_RMID,NULL) ==-1)
{
perror("ggg");
return -1;
}
}
else
{
perror("ggg");
return -1;
}
wait(NULL);
return 0;
}
#include<myhead.h>
struct msgbuf
{
long mtype;
char mtext[1024];
};
struct heizi
{
long mtype;
char mtext[1024];
};
#define SIZE (sizeof(struct msgbuf)-sizeof(long))
#define SIZE1 (sizeof(struct heizi)-sizeof(long))
int main(int argc, const char *argv[])
{
pid_t pid=fork();
if(pid ==0)
{
key_t key;
if((key=ftok("/",'t'))==-1)
{
perror("ggg");
return -1;
}
int ikun=0;
if((ikun=msgget(key,IPC_CREAT|0664))==-1)
{
perror("ggg");
return -1;
}
printf("%d\n",ikun);
struct msgbuf buf;
while(1)
{
msgrcv(ikun,&buf,SIZE,0,0);
printf("收到1传来消息为: %s\n",buf.mtext);
if(strcmp(buf.mtext,"quit")==0)
{
break;
}
}
if(msgctl(ikun,IPC_RMID,NULL) ==-1)
{
perror("ggg11");
return -1;
}
exit(EXIT_SUCCESS);
}
if(pid>0)
{
key_t key1;
if((key1=ftok("/",'y'))==-1)
{
perror("ggg");
return -1;
}
int kun=0;
if((kun=msgget(key1,IPC_CREAT|0664))==-1)
{
perror("ggg");
return -1;
}
printf("%d\n",kun);
struct heizi buf1;
while(1)
{
printf("要向1发送的数据:\n");
scanf("%s",buf1.mtext);
getchar();
msgsnd(kun,&buf1,SIZE1,0);
printf("发送成功\n");
if(strcmp(buf1.mtext,"quit")==0)
{
break;
}
}
if(msgctl(kun,IPC_RMID,NULL) ==-1)
{
perror("ggg12");
return -1;
}
}
else
{
perror("ggg");
return -1;
}
wait(NULL);
return 0;
}
2、将信号通信相关代码重新实现一遍
#include<myhead.h>
void ikun(int gg)
{
if(gg ==SIGUSR1)
{
printf("我真是爱坤\n");
raise(SIGKILL);
}
}
int main(int argc, const char *argv[])
{
pid_t pid=fork();
if(pid>0)
{
if(signal(SIGUSR1,ikun) ==SIG_ERR)
{
perror("ggg");
return -1;
}
printf("我是真爱坤\n");
while(1)
{
sleep(1);
printf("你是小黑子\n");
}
}
else if(pid ==0)
{
printf("食不食油饼,香精煎鱼\n");
sleep(3);
printf("同归于尽吧\n");
kill(getppid(),SIGUSR1);
exit(EXIT_SUCCESS);
}
else
{
perror("ggg");
return -1;
}
wait(NULL);
return 0;
}
3、将共享内存相关代码重新实现一遍
发送
#include<myhead.h>
#define PAGE_SIZE 4096
int main(int argc, const char *argv[])
{
//1、创建key值
key_t key = -1;
if((key = ftok("/", 't')) == -1)
{
perror("ggggg");
return -1;
}
printf("key = %#x\n", key);
//2、创建共享内存
int shmid = 0;
if((shmid = shmget(key, PAGE_SIZE, IPC_CREAT|0664)) == -1)
{
perror("ggggg");
return -1;
}
printf("shmid = %d\n", shmid);
//3、映射空间
char *addr = (char *)shmat(shmid, NULL, 0);
if(addr == (void *)-1)
{
perror("ggggg");
return -1;
}
printf("addr = %p\n", addr);
//4、进行操作
while(1)
{
fgets(addr, PAGE_SIZE, stdin);
addr[strlen(addr) - 1] = '\0';
if(strcmp(addr, "quit") == 0)
{
break;
}
}
//5、取消映射
if(shmdt(addr) ==-1)
{
perror("ggg");
return -1;
}
//6、删除映射
if(shmctl(shmid, IPC_RMID, NULL) == -1)
{
perror("ggggg");
return -1;
}
while(1);
return 0;
}
接受
#include<myhead.h>
#define PAGE_SIZE 4096
int main(int argc, const char *argv[])
{
//1、创建key值
key_t key = -1;
if((key = ftok("/", 't')) == -1)
{
perror("gggg");
return -1;
}
printf("key = %#x\n", key);
//2、将物理内存创建出共享内存段
int shmid = 0;
if((shmid = shmget(key, PAGE_SIZE, IPC_CREAT|0664)) == -1)
{
perror("gggg");
return -1;
}
printf("shmid = %d\n", shmid);
//3、将内存段地址映射
char *addr = (char *)shmat(shmid, NULL, 0);
if(addr == (void *)-1)
{
perror("gggg");
return -1;
}
printf("addr = %p\n", addr);
//4、操作共享内存
//char buf[128] = "";
while(1)
{
printf("共享内存中的数据为:%s\n", addr);
sleep(1);
if(strcmp(addr, "quit") == 0)
{
break;
}
}
//5、取消映射
if(shmdt(addr) == -1)
{
perror("ggg");
return -1;
}
//6、删除共享内存
if(shmctl(shmid, IPC_RMID, NULL) == -1)
{
perror("ggggg");
return -1;
}
return 0;
}