编写两个进程a和b,利用共享内存技术,a向共享内存写字符串,b将从共享内存中读到的字符串在屏幕上打印出来。
2016-07-09 12:55
435 查看
创建一个共享内存
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main(int arg, char *args[])
{
int shmid = shmget(IPC_PRIVATE, 1024, 0666);
if (shmid < 0)
{
printf("error\n");
} else
{
printf("succes\n");
}
shmat(shmid, NULL, )
return EXIT_SUCCESS;
}然后使用这个共享内存
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/shm.h>
int main(int arg, char *args[])
{
char *shmbuf;
int shmid = 0;
if (arg > 2)
{
shmid = atoi(args[1]);
shmbuf = shmat(shmid, 0, 0);
if (atoi(args[2]) == 1)
{
scanf("%s", shmbuf);
}
if (atoi(args[2]) == 2)
{
printf("%s\n", shmbuf);
}
shmdt(shmbuf);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main(int arg, char *args[])
{
int shmid = shmget(IPC_PRIVATE, 1024, 0666);
if (shmid < 0)
{
printf("error\n");
} else
{
printf("succes\n");
}
shmat(shmid, NULL, )
return EXIT_SUCCESS;
}然后使用这个共享内存
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/shm.h>
int main(int arg, char *args[])
{
char *shmbuf;
int shmid = 0;
if (arg > 2)
{
shmid = atoi(args[1]);
shmbuf = shmat(shmid, 0, 0);
if (atoi(args[2]) == 1)
{
scanf("%s", shmbuf);
}
if (atoi(args[2]) == 2)
{
printf("%s\n", shmbuf);
}
shmdt(shmbuf);
}
return 0;
}
相关文章推荐
- Linux的SOCKET编程详解
- 2.一个EJB的小Demo
- Vim里调试错误小结(一):程序中有游离的 ‘\200’
- 编写两个不同的可执行程序,一个打开文件,一个读文件
- VS2015下Log4Cplus编译及其使用
- 对于javascript四舍五入保留多少位小数的问题
- Alamofire请求网络
- iOS-最全的App上架教程
- Subtree
- SVN服务器搭建和使用
- Linux编写一个孤儿进程,这个孤儿进程可以同时创建100个僵死进程。
- Python cx_Oracle & Oracle Client for Mac OS
- bzoj1821(克鲁斯卡尔)
- Rich feature hierarchies for accurate object detection and semantic segmentation 笔记
- 安卓手册 第三章(开发常识)
- 初学者对于I/O流的小节-下
- CMake 运行错误 could not find CMAKE_ROOT
- Cocoa框架
- 四种方案解决ScrollView嵌套ListView问题
- 十年微软(Microsoft)MVP