linux C学习笔记02--共享内存(进程同步)
system V下3中进程同步:共享内存(shared memory),信号量(semaphore)和消息队列(message queue)
调试了下午,终于调通啦! 运行./c.out 输出共享内存中的内容,运行 ./c.out arg1 对共享内存区进行修改,shell下输入ipcs -m 可以查看共享内存情况 ,-s 是信号量,-q 是消息队列
下面先贴上main的代码:
#include <signal.h> //head file of define signal
#include <pthread.h>
#include <static_lib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/mman.h> extern int create_shm(char **shmptr);
int main(int argc,char* argv[])
{
//IPC share memory
char *shmptr;
if(create_shm(&shmptr) == -)
{
printf("create_shm error \n");
return -;
}
if(argc > )
{
//do the second thing for share memory
while()
{
printf("input str to share memory:");
gets(shmptr);
}
}
else
{
memcpy(shmptr,"hello",);
while()
{
sleep();
printf("share memory is %s \n",shmptr);
}
}
return ;
}
下面是共享内存创建的代码:
/*************************************************************************
> File Name: share_memory.c
> Author: hailin.ma
> Mail: mhl2018@126.com
> Created Time: Wed 27 May 2015 11:19:26 PM CST
************************************************************************/ #include<stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <errno.h> #define SHARE_MEM_KEY 26 int create_shm(char **shmptr)
{
int shmid; //if((shmid = shmget(IPC_PRIVATE,200,IPC_CREAT|IPC_EXCL|0666)) == -1)
if((shmid = shmget(SHARE_MEM_KEY,,IPC_CREAT|IPC_EXCL|)) == -)
{
perror("shmget");
if(errno == EEXIST)
{
shmid = shmget(SHARE_MEM_KEY,,);
if(shmid == -)
{
perror("shmget2");
return -;
}
else
{
if((*shmptr = shmat(shmid,,)) == (void*)-)
{
perror("shmat2");
return -;
}
else
{
return shmid;
}
}
}
else
{
return -;
}
} if((*shmptr = shmat(shmid,,)) == (void*)-)
{
perror("shmat");
return -;
} return shmid;
}
运行效果:

05.29日增加了共享内存释放和删除:
int main(int argc,char* argv[])
{ //IPC share memory
char *shmptr;
int shmid;
if((shmid =create_shm(&shmptr)) == -)
{
printf("create_shm error \n");
return -;
}
if(argc > )
{
//do the second thing for share memory
while()
{
printf("input str to share memory:");
gets(shmptr);
if(shmptr[] == 'q') //quit
{
shmdt(shmptr); //disconnect to the share memory but will not dellect the memery
break;
} }
}
else
{
while()
{
sleep();
printf("share memory is: %s \n",shmptr);
if(shmptr[] == 'q')
{
shmdt(shmptr);
shmctl(shmid,IPC_RMID,NULL); //delete the share memery
break;
}
}
}
}
linux C学习笔记02--共享内存(进程同步)的更多相关文章
- Linux学习笔记27——共享内存
一 共享内存 共享内存是由IPC为进程创建的一个特殊的地址范围,它将出现在该进程的地址空间中.其他进程可以将同一段共享内存连接到它们自己的地址空间中,所有进程都可以访问共享内存中的地址.如果某个进程向 ...
- linux kernel学习笔记-5内存管理_转
void * kmalloc(size_t size, gfp_t gfp_mask); kmalloc()第一个参数是要分配的块的大小,第一个参数为分配标志,用于控制kmalloc()的行为. km ...
- Linux系统学习笔记:文件I/O
Linux支持C语言中的标准I/O函数,同时它还提供了一套SUS标准的I/O库函数.和标准I/O不同,UNIX的I/O函数是不带缓冲的,即每个读写都调用内核中的一个系统调用.本篇总结UNIX的I/O并 ...
- Linux内核学习笔记-1.简介和入门
原创文章,转载请注明:Linux内核学习笔记-1.简介和入门 By Lucio.Yang 部分内容来自:Linux Kernel Development(Third Edition),Robert L ...
- Linux内核学习笔记二——进程
Linux内核学习笔记二——进程 一 进程与线程 进程就是处于执行期的程序,包含了独立地址空间,多个执行线程等资源. 线程是进程中活动的对象,每个线程都拥有独立的程序计数器.进程栈和一组进程寄存器 ...
- 【cocos2d-x 3.x 学习笔记】对象内存管理
内存管理 内存管理一直是一个不易处理的问题.开发人员必须考虑分配回收的方式和时机,针对堆和栈做不同的优化处理,等等.内存管理的核心是动态分配的对象必须保证在使用完成后有效地释放内存,即管理对象的生命周 ...
- 尚硅谷韩顺平Linux教程学习笔记
目录 尚硅谷韩顺平Linux教程学习笔记 写在前面 虚拟机 Linux目录结构 远程登录Linux系统 vi和vim编辑器 关机.重启和用户登录注销 用户管理 实用指令 组管理和权限管理 定时任务调度 ...
- Linux内核学习笔记-2.进程管理
原创文章,转载请注明:Linux内核学习笔记-2.进程管理) By Lucio.Yang 部分内容来自:Linux Kernel Development(Third Edition),Robert L ...
- 20135316王剑桥Linux内核学习笔记
王剑桥Linux内核学习笔记 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 计算机是如何工作的 个人理 ...
随机推荐
- iTunesConnect进行App转移2-官方说明
Can I transfer an app to another developer's iTunes Connect account? Yes, you can transfer your app ...
- Oracle创建、删除表空间、用户
1.创建临时表空间 create temporary tablespace linshi tempfile 'e:\linshi.dbf' size 50m autoextend on next 50 ...
- 初识Winform , 还好没喜欢上控制台
虽然没听的太懂, 不过还是写点东西吧. 我呢, 就跟着这本书写了个学生管理系统 前面刚会了SQLserver, 所以这个学生管理系统需要连上数据库, 毕竟学了不用天诛地灭 既然需要连接数据库, 就要用 ...
- java io系列14之 DataInputStream(数据输入流)的认知、源码和示例
本章介绍DataInputStream.我们先对DataInputStream有个大致认识,然后再深入学习它的源码,最后通过示例加深对它的了解. 转载请注明出处:http://www.cnblogs. ...
- bk.
http://ol.tgbus.com/zt2013/gzsnew/ 巴士盘点 十大游戏工作室 http://bbs.3dmgame.com/forum.php?mod=viewthread& ...
- Cacti-安装和使用详解
Cacti是一套基于PHP,MySQL,SNMP及RRDTool开发的网络流量监测图形分析工具.Cacti是通过 snmp get来获取数据,使用 RRDtool绘画图形,而且你完全可以不需要了解RR ...
- android四大组件之ContentProvider(一)
ContentProvider学习笔记 1. ContentProvider基本概念 ContentProvider向我们提供了我们在应用程序之间共享数据的一种机制,虽然采用文件和SharedPref ...
- MySQL各逻辑模块工作配合
在了解了MySQL的各个模块之后(点击查看MySQL各个逻辑模块),我们再看看MySQL各个模块间是如何相互协同工作的.接下来,我们通过启动MySQL,客户端连接,请求query,得到返回结果,到最后 ...
- 一步步构建自己的AngularJS(1)——项目初始化
Angular1距离2009年发布已经好多年了,Angular2也已经出了Beta版,估计今年就能正式发布.大多数人对于Angular1.X的认识仅限于能够在项目中使用,对于其中的深层原理知道的并不多 ...
- 解决php的“It is not safe to rely on the system’s timezone settings”问题
PHP调试的时候出现了警告: It is not safe to rely on the system解决方法,其实就是时区设置不正确造成的,本文提供了3种方法来解决这个问题. 实 际上,从PHP 5 ...