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--共享内存(进程同步)的更多相关文章

  1. Linux学习笔记27——共享内存

    一 共享内存 共享内存是由IPC为进程创建的一个特殊的地址范围,它将出现在该进程的地址空间中.其他进程可以将同一段共享内存连接到它们自己的地址空间中,所有进程都可以访问共享内存中的地址.如果某个进程向 ...

  2. linux kernel学习笔记-5内存管理_转

    void * kmalloc(size_t size, gfp_t gfp_mask); kmalloc()第一个参数是要分配的块的大小,第一个参数为分配标志,用于控制kmalloc()的行为. km ...

  3. Linux系统学习笔记:文件I/O

    Linux支持C语言中的标准I/O函数,同时它还提供了一套SUS标准的I/O库函数.和标准I/O不同,UNIX的I/O函数是不带缓冲的,即每个读写都调用内核中的一个系统调用.本篇总结UNIX的I/O并 ...

  4. Linux内核学习笔记-1.简介和入门

    原创文章,转载请注明:Linux内核学习笔记-1.简介和入门 By Lucio.Yang 部分内容来自:Linux Kernel Development(Third Edition),Robert L ...

  5. Linux内核学习笔记二——进程

    Linux内核学习笔记二——进程   一 进程与线程 进程就是处于执行期的程序,包含了独立地址空间,多个执行线程等资源. 线程是进程中活动的对象,每个线程都拥有独立的程序计数器.进程栈和一组进程寄存器 ...

  6. 【cocos2d-x 3.x 学习笔记】对象内存管理

    内存管理 内存管理一直是一个不易处理的问题.开发人员必须考虑分配回收的方式和时机,针对堆和栈做不同的优化处理,等等.内存管理的核心是动态分配的对象必须保证在使用完成后有效地释放内存,即管理对象的生命周 ...

  7. 尚硅谷韩顺平Linux教程学习笔记

    目录 尚硅谷韩顺平Linux教程学习笔记 写在前面 虚拟机 Linux目录结构 远程登录Linux系统 vi和vim编辑器 关机.重启和用户登录注销 用户管理 实用指令 组管理和权限管理 定时任务调度 ...

  8. Linux内核学习笔记-2.进程管理

    原创文章,转载请注明:Linux内核学习笔记-2.进程管理) By Lucio.Yang 部分内容来自:Linux Kernel Development(Third Edition),Robert L ...

  9. 20135316王剑桥Linux内核学习笔记

    王剑桥Linux内核学习笔记 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 计算机是如何工作的 个人理 ...

随机推荐

  1. Spring使用——切面编程AOP使用

    在我们的spring xml配置中,加上<aop:config>之后,Eclipse报错,提示The prefix "aop" for element "ao ...

  2. hdu 4336 Card Collector

    dp+状态压缩 #include<cstdio> using namespace std; ]; <<]; int main() { int n; while(scanf(&q ...

  3. sphinx.conf 详解

    sphinx的配置文件是在配置的时候最容易出错的了: 我们先要明白几个概念: source:数据源,数据是从什么地方来的. index:索引,当有数据源之后,从数据源处构建索引.索引实际上就是相当于一 ...

  4. 计算DNA中每种核苷酸的数目

    问题描述:计算DNA中每种核苷酸的数目 输入文件内容: 代码: 输出结果:

  5. Model1

    jsp+javabean的开发模式 此处JavaBean也可是封装的业务逻辑 流程: 浏览器端访问jsp,jsp交给Javabean处理,javabean处理后台数据,交还给Jsp

  6. bootstrap-4

    html文档中,列表结构主要有三种:有序列表.无序列表和定义列表:<ul><li>.<ol><li>.<dl><dt><d ...

  7. [JSOI2008][BZOJ1012] 最大数(动态开点线段树)

    题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制:L不超过当前数列的长度. 2. 插入操作 ...

  8. 2016HUAS_ACM暑假集训2G - Who's in the Middle

    这个题真的没什么好说的.一个排序就能解决的问题.唯一感到不爽的是这道题不是0msAC的,希望各位大神能够给我点指导. 头文件#include<algorithm>,注意一下排序函数的用法就 ...

  9. 怎么 才能显示Eclipse中Console的全部内容

    可以如下设置 preference->run/debug->console 设置limit console output 为false,方便调试时,查看全部console. 这个真是太有用 ...

  10. windows ftp 连接serv_U 管理员

    连接工具名称:8uftp 小技巧:活动模式连接