Linux下C语言进程通讯编程
代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
/*************基本的函数API********************
共享内存函数API
int shmget(key_t key, int size, int flag)
key:
共享内存的键值,多个进程可以通过它访问同一个共享内存。常用特殊值:IPC_PRIVATE,创建当前进程的私有共享内存
size:
指定创建共享内存的大小
flag:
操作权限
char * shmat(int shm_id, const void * addr, int flag)
shm_id:
要映射的共享内存标识符
addr:
指定在调用进程中映射共享内存的地址。通常取值为0,表示由系统自动分配地址。
flag:
设置共享内存的操作权限。若取值为0,表示可对共享内存进行读写操作。
int shmctl(int shm_id, int cmd, struct shmid_ds * buf)
shm_id:
共享内存标识符
cmd:
指定索要进行的操作:IPC_STAT IPC_SET IPC_RMID SHM_LOCK SHM_UNLOCK
buf:
结构体型指针
int shmdt(const void * addr)
**********************************************/
#define Test_pipe 0
#define Test_Shmget 0
#define Test_AT_w 0
#define Test_AT_r 0
#define Test_DT 1
int main(int argc, char *argv[])
{
#if Test_pipe
int x,fd[];
char buf[],s[];
pipe(fd);
x = fork();
if( == x)
{
sprintf(buf,"This is an pipe!");
write(fd[],buf,);
exit();
}
else
{
wait();
read(fd[],s,);
printf("read: %s\n",s);
}
#endif #if Test_Shmget
int shm_id;
shm_id = shmget(IPC_PRIVATE,,);
if(shm_id < )
{
perror("shmget id < 0");
exit();
}
printf("成功建立共享内存区域: %d\n",shm_id);
system("ipcs -m");
#endif #if Test_AT_w
int shm_id;
char *shm_buf;
shm_id = atoi(argv[]);
shm_buf = shmat(shm_id,,);
printf("写如数据到共享内存:\n");
sprintf(shm_buf,"对共享内存的读写操作!");
printf("%s\n",shm_buf);
#endif #if Test_AT_r
int shm_id;
char *shm_buf,str;
shm_id = atoi(argv[]);
shm_buf = shmat(shm_id,,);
printf("写如数据到共享内存:\n");
sprintf(str,shm_buf);
printf("%s\n",str);
system("ipcs -m");
#endif #if Test_DT
int shm_id;
char *shm_buf;
shm_id = atoi(argv[]);
shm_buf = shmat(shm_id,,);
shmdt(shm_buf);
shmctl(shm_id,IPC_RMID,NULL);
system("ipcs -m");
#endif
}
IPC通讯中共享内存参看代码:
Client端代码client.c(gcc client.c -lm -o client)
shmget函数得到的共享内存的大小有参数确定,参数的单位是Byte!
同样的shmget参数的0666代表的内容是,共享内存空间的可读可写性!
使用ftok函数获取的key_t key的作用在于生成一个文件节点引索,这样才能够连接两个进程让他们以这个引索值作为标识ID!
shmdt函数用来删除释放掉创建的共享内存空间!
shmat函数用来映射已经创建好的虚拟内存空间地址!返回值为两个进程进行操作的共享内存起始地址!
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <time.h>
#include <math.h> #define random(x) (rand()%x)
#define PI 3.1415926 #define Width 20
#define Height 20 int main(int argc,char *argv[])
{
key_t key = ;
int shmid = ;
setbuf(stdout,NULL);
printf("Begin to Create the shared-mem...\n");
key = ftok(".",); // Get the only IPC ID:inode'num + 1
if(key<)
{
perror("ftok");
return -;
}
printf("key=%d\n",key);
sleep();
printf("...\n");
printf("%d\n",IPC_CREAT);
shmid = shmget(key,,IPC_EXCL | IPC_CREAT | ); // Get shared mem:IPC_ID_key Mem_size
if(shmid<)
{
perror("shmget");
return -;
}
char * shared_mem_addr = shmat(shmid,NULL,); // mapping the mem_addr to the addr of the Process
printf("...\n"); sleep(); // waitting for setup the mem printf("The shared-mem create sucessfully!\n");
printf("Begin to insert the data into shared-mem!\n");
int i=;
while(i<Width*Height)
{
*(shared_mem_addr+i) = *sin((double)i*PI/180.0);// random(255)+255;
usleep();
printf(".");
i++;
}
printf("\nThe data insert finish!\n");
shmdt(shared_mem_addr);
return ;
}
Server端代码server.c(gcc server.c -o server)
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <time.h> #define Width 20
#define Height 20 int main(int argc,char *argv[])
{
key_t key = ;
int shmid = ;
key = ftok(".",); // Get the only IPC ID:inode'num + 1
if(key<)
{
perror("ftok");
return -;
}
shmid = shmget(key,,IPC_CREAT | ); // Get shared mem:IPC_ID_key Mem_size
if(shmid<)
{
perror("shmget");
return -;
}
char * shared_mem_addr = shmat(shmid,NULL,); // mapping the mem_addr to the addr of the Process sleep(); int i=;
while(i<Width*Height)
{
printf("The image data:%d\n",*(shared_mem_addr+i));
usleep();
i++;
}
shmdt(shared_mem_addr);
sleep();
shmctl(shmid,IPC_RMID,NULL);
return ;
}
首先运行Clent端,然后再运行Server端即可!
参看IPC通讯介绍:https://www.cnblogs.com/CheeseZH/p/5264465.html
Linux下C语言进程通讯编程的更多相关文章
- linux下C语言socket网络编程简例
原创文章,转载请注明转载字样和出处,谢谢! 这里给出在linux下的简单socket网络编程的实例,使用tcp协议进行通信,服务端进行监听,在收到client的连接后,发送数据给client:clie ...
- linux下c语言的多线程编程
我们在写linux的服务的时候,经常会用到linux的多线程技术以提高程序性能 多线程的一些小知识: 一个应用程序可以启动若干个线程. 线程(Lightweight Process,LWP),是程序执 ...
- linux下C语言多线程编程实例
用一个实例.来学习linux下C语言多线程编程实例. 代码目的:通过创建两个线程来实现对一个数的递加.代码: //包含的头文件 #include <pthread.h> #include ...
- Linux下C语言编程实现spwd函数
Linux下C语言编程实现spwd函数 介绍 spwd函数 功能:显示当前目录路径 实现:通过编译执行该代码,可在终端中输出当前路径 代码实现 代码链接 代码托管链接:spwd.c 所需结构体.函数. ...
- Linux基础与Linux下C语言编程基础
Linux基础 1 Linux命令 如果使用GUI,Linux和Windows没有什么区别.Linux学习应用的一个特点是通过命令行进行使用. 登录Linux后,我们就可以在#或$符后面去输入命令,有 ...
- LINUX下C语言编程基础
实验二 Linux下C语言编程基础 一.实验目的 1. 熟悉Linux系统下的开发环境 2. 熟悉vi的基本操作 3. 熟悉gcc编译器的基本原理 4. 熟练使用gcc编译器的常用选项 5 .熟练使用 ...
- LINUX下C语言编程调用函数、链接头文件以及库文件
LINUX下C语言编程经常需要链接其他函数,而其他函数一般都放在另外.c文件中,或者打包放在一个库文件里面,我需要在main函数中调用这些函数,主要有如下几种方法: 1.当需要调用函数的个数比较少时, ...
- Linux下C语言编程基础学习记录
VIM的基本使用 LINUX下C语言编程 用gcc命令编译运行C语言文件 预处理阶段:将*.c文件转化为*.i预处理过的C程序. 编译阶段:将*.i文件编译为汇编代码*.s文件. 汇编阶段:将*.s ...
- 【转】Linux基础与Linux下C语言编程基础
原文:https://www.cnblogs.com/huyufeng/p/4841232.html ------------------------------------------------- ...
随机推荐
- C# 防止content-type修改后上传恶意文件
以图片为例子.在上传图片的时候,使用Fiddler抓取 通过js判断文件类型是不安全的,所以通过后台来判断,代码如下: ) { HttpPostedFile file0 = Request.Files ...
- hive学习01词频统计
词频统计 #创建表,只有一列,列名line create table word_count ( line string) row format delimited fields terminated ...
- android招聘啦,美图秀秀欢迎你加入!
前言 最近朋友公司招聘,美图秀秀大家一定很熟悉吧,欢迎你的加入. 了解相关更多技术以外的,可参考<除了敲代码,你还有什么副业吗?>,再往下看,今天给需要换工作或者还未找到工作的童鞋们谋一个 ...
- 使用open live writer客户端写博客
注:Windows Live Writer 已经停止更新,建议安装 Open Live Writer,下载地址: http://openlivewriter.org/ 使用open live writ ...
- Confluence 6 用户目录图例 - 连接 Jira 和 Jira 连接 LDAP
上面的图: Confluence 连接到 JIRA 用户管理,JIRA 使用 LDAP 用户目录. https://www.cwiki.us/display/CONFLUENCEWIKI/Diagra ...
- swift项目初始化并添加忽略文件Swift.ignore
1 先去GitHub上去把最新的忽略文件下载下载 https://github.com/github/gitignore 2 然后找到Swift.gitignore 把里面的 pod 前面的# 删除 ...
- STL 容器区别:vector、list、deque、set、map的底层实现
https://blog.csdn.net/shawjan/article/details/45424405
- vivado 下安装modelsim
安装modelsim 下载链接:http://pan.baidu.com/s/1i4vHDbR 密码:dksy 1.运行modelsim-win64-10.4-se.exe,安装软件: 注意事项:安装 ...
- SpringCloud路由(网关)
springcloud网关接口就类似于转发 搭建路由网关项目(ZuulDemo) 1.创建pom.xml <project xmlns="http://maven.apache.org ...
- java----AOP框架理解
面向切面编程: 通过动态代理+加配置文件 目的解耦 给主逻辑添加一些修饰功能,但是不在主逻辑代码中进行修改,有点类似python中的装饰器,调用方法还是是通过接口的那个类来调用: import jav ...