代码:

 #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语言进程通讯编程的更多相关文章

  1. linux下C语言socket网络编程简例

    原创文章,转载请注明转载字样和出处,谢谢! 这里给出在linux下的简单socket网络编程的实例,使用tcp协议进行通信,服务端进行监听,在收到client的连接后,发送数据给client:clie ...

  2. linux下c语言的多线程编程

    我们在写linux的服务的时候,经常会用到linux的多线程技术以提高程序性能 多线程的一些小知识: 一个应用程序可以启动若干个线程. 线程(Lightweight Process,LWP),是程序执 ...

  3. linux下C语言多线程编程实例

    用一个实例.来学习linux下C语言多线程编程实例. 代码目的:通过创建两个线程来实现对一个数的递加.代码: //包含的头文件 #include <pthread.h> #include ...

  4. Linux下C语言编程实现spwd函数

    Linux下C语言编程实现spwd函数 介绍 spwd函数 功能:显示当前目录路径 实现:通过编译执行该代码,可在终端中输出当前路径 代码实现 代码链接 代码托管链接:spwd.c 所需结构体.函数. ...

  5. Linux基础与Linux下C语言编程基础

    Linux基础 1 Linux命令 如果使用GUI,Linux和Windows没有什么区别.Linux学习应用的一个特点是通过命令行进行使用. 登录Linux后,我们就可以在#或$符后面去输入命令,有 ...

  6. LINUX下C语言编程基础

    实验二 Linux下C语言编程基础 一.实验目的 1. 熟悉Linux系统下的开发环境 2. 熟悉vi的基本操作 3. 熟悉gcc编译器的基本原理 4. 熟练使用gcc编译器的常用选项 5 .熟练使用 ...

  7. LINUX下C语言编程调用函数、链接头文件以及库文件

    LINUX下C语言编程经常需要链接其他函数,而其他函数一般都放在另外.c文件中,或者打包放在一个库文件里面,我需要在main函数中调用这些函数,主要有如下几种方法: 1.当需要调用函数的个数比较少时, ...

  8. Linux下C语言编程基础学习记录

    VIM的基本使用  LINUX下C语言编程 用gcc命令编译运行C语言文件 预处理阶段:将*.c文件转化为*.i预处理过的C程序. 编译阶段:将*.i文件编译为汇编代码*.s文件. 汇编阶段:将*.s ...

  9. 【转】Linux基础与Linux下C语言编程基础

    原文:https://www.cnblogs.com/huyufeng/p/4841232.html ------------------------------------------------- ...

随机推荐

  1. redhat7.3安装yum源

    #检查rehat自带的yum源[root@localhost ~]# rpm -qa | grep yum -.el7.noarch -.el7.noarch -.el7.noarch -.el7.n ...

  2. hexo d 部署博客时出错

    问题描述: // 第一次遇到的问题 Error: packet_write_wait: Connection to 192.30.253.113 port 22: Broken pipe packet ...

  3. Spring如何使用JdbcTemplate调用存储过程的三种情况

    注:原文 <Spring如何使用JdbcTemplate调用存储过程的三种情况 > Spring的SimpleJdbcTemplate将存储过程的调用进行了良好的封装,下面列出使用Jdbc ...

  4. Confluence 6 XML 备份失败的问题解决

    XML 站点备份仅仅被用于整合到一个新的数据库.设置一个测试服务器 或者 创建一个可用的备份策略 相对 XML 备份来说是更合适的策略. 相关页面: Enabling detailed SQL log ...

  5. 第十七单元 Samba服务

    Samba的功能 Samba的安装 Samba服务的启动.停止.重启 Samba服务的配置 Samba服务的主配置文件 samba服务器配置实例 Samba客户端设置 windows客户端 Linux ...

  6. springboot多环境(dev、test、prod)配置

    propertiest配置格式在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如: ...

  7. Fisher–Yates shuffle 算法

    费希尔 - 耶茨洗牌 维基百科,自由的百科全书     所述费-耶茨洗牌是一种算法,用于产生随机排列的有限的序列 -in平原而言,算法打乱的序列.该算法有效地将所有元素放在帽子里; 它通过随机从帽子中 ...

  8. Python初探list

    今天要说一个新概念--list,中文可以翻译成列表,是用来处理一组有序项目的数据结构.想象一下你的购物清单.待办工作.手机通讯录等等,它们都可以看作是一个列表.说它是新概念也不算确切,因为我们之前已经 ...

  9. servlet在地址栏填写参数

    单个参数:以"?"开头+参数名+"="符号+参数值 例如 https://i.cnblogs.com/EditPosts.aspx?opt=1 多个参数:以&q ...

  10. 关于金蝶k3 wise供应生门户登陆界面屏蔽业务账套多余功能模块设置方法

    关于金蝶k3 wise供应生门户登陆界面屏蔽业务账套多余功能模块设置方法 1. 找到以下路径 ...\Kingdee\K3ERP\KDHR\SITEFILE\WEBUI\ 找到“Login.aspx” ...