1.A进程写入一个整型,在该整型后,写入一个字符串
2.B进程将共享内存中的整型以及字符串读取出来
(注,调用的函数都未加判断是否执行成功)
A进程代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
int main(int argc, const char *argv[])
{
//创建键值
key_t key = ftok("./", 1);
//使用该键值创建共享内存空间
int shmid = shmget(key, 100, IPC_CREAT|0664);
//将共享内存空间映射到进程中,获取该空间首地址
void* p = shmat(shmid, NULL , 0);
//将指针强转成int*类型写入整型数据
int* q = (int*)p;
printf("请输入整型数据--->");
scanf("%d",q);
getchar();
//将整形后的第一个地址强转成char*类型写入字符串
char* s = (char*)(q+1);
char str[50]=""; //存手动输入的字符串
printf("请输入字符串--->");
fgets(str, sizeof(str), stdin);//终端获取字符串
str[strlen(str)-1] = '\0';
strcpy(s, str);
return 0;
}
B进程代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
int main(int argc, const char *argv[])
{
//拿到同一片共享内存
key_t key = ftok("./", 1);
int shmid = shmget(key, 100, IPC_CREAT|0664);
void* p = shmat(shmid, NULL , 0);
//将万能指针p强转成int类型读出数据
int* q = (int*)p;
printf("整数是:%d\n", *q);
//将整形后的第一个地址强转成char*类型读出字符串
char* s = (char*)(q+1);
printf("字符串是:%s\n",s);
return 0;
}
测试结果
要求AB进程做通信
A进程发送一句话,B进程接收打印
然后B进程发送给A进程一句话,A进程接收打印
重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程;
AB进程能够随时收发数据(附加题)
A进程代码(发送没有文字提示,直接输入即是发送)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void* send_msg(void* arg)
{
//只写打开管道文件1
int op1 = open("./myfifo1", O_WRONLY);
if(op1 < 0)
{
perror("open");
pthread_exit(NULL);
}
while(1)
{
//发送信息
char send[128];
bzero(send, 128);
fgets(send, 128, stdin);
send[strlen(send)-1] = '\0';
if(strcmp(send, "quit")==0)
{
write(op1, send, 128);//告诉B结束进程,然后自己结束进程
exit(1);
}
write(op1, send, 128);
}
close(op1);
}
void* accept_msg(void* arg)
{
//只读打开管道文件2
int op2 = open("./myfifo2", O_RDONLY);
if(op2 < 0)
{
perror("open");
pthread_exit(NULL);
}
while(1)
{
//接收信息
char accept[128];
int res = 0;
bzero(accept, 128);
res = read(op2, accept, 128);
if(strcmp(accept, "quit")==0)
{
exit(1); //结束进程
}
printf("A接收到的信息--->%s\n",accept);
}
close(op2);
}
int main(int argc, const char *argv[])
{
umask(0);//文件掩码
//创建用于发送信息的管道文件
if(mkfifo("./myfifo1", 0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
//创建用于接收信息的管道文件
if(mkfifo("./myfifo2", 0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
//创建一个线程发送数据
pthread_t tid1;
if(pthread_create(&tid1, NULL,send_msg, NULL)<0)
{
perror("pthread_create");
return -1;
}
//创建一个线程接收数据
pthread_t tid2;
if(pthread_create(&tid2, NULL,accept_msg, NULL)<0)
{
perror("pthread_create");
return -1;
}
printf("准备就绪\n");
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}
B进程代码(发送没有文字提示,直接输入即是发送)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void* accept_msg(void* arg)
{
//只读打开管道文件1
int op1 = open("./myfifo1", O_RDONLY);
if(op1 < 0)
{
perror("open");
pthread_exit(NULL);
}
while(1)
{
//接收信息
char accept[128];
int res = 0;
bzero(accept, 128);
res = read(op1, accept, 128);
if(strcmp(accept, "quit")==0)
{
exit(1);
}
printf("B接收到的信息--->%s\n",accept);
}
close(op1);
}
void* send_msg(void* arg)
{
//只写打开管道文件2
int op2 = open("./myfifo2", O_WRONLY);
if(op2 < 0)
{
perror("open");
pthread_exit(NULL);
}
while(1)
{
//发送信息
char send[128];
bzero(send, 128);
fgets(send, 128, stdin);
send[strlen(send)-1] = '\0';
if(strcmp(send, "quit")==0)
{
write(op2, send, 128);//告诉A结束进程,然后自己结束进程
exit(1);
}
write(op2, send, 128);
}
close(op2);
}
int main(int argc, const char *argv[])
{
umask(0);//文件掩码
//创建用于发送信息的管道文件
if(mkfifo("./myfifo1", 0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
//创建用于接收信息的管道文件
if(mkfifo("./myfifo2", 0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
//创建一个线程接收数据
pthread_t tid2;
if(pthread_create(&tid2, NULL,accept_msg, NULL)<0)
{
perror("pthread_create");
return -1;
}
//创建一个线程发送数据
pthread_t tid1;
if(pthread_create(&tid1, NULL,send_msg, NULL)<0)
{
perror("pthread_create");
return -1;
}
printf("准备就绪\n");
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}
测试结果