linux 进程间通信--systemV 共享内存 实例

2024-02-12 04:18

本文主要是介绍linux 进程间通信--systemV 共享内存 实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1:共享内存代码

#include <sys/shm.h>
#include <sys/types.h>  /* Type definitions used by many programs */
#include <stdio.h>      /* Standard I/O functions */
#include <stdlib.h>     /* Prototypes of commonly used library functions,                         plus EXIT_SUCCESS and EXIT_FAILURE constants */
#include <unistd.h>     /* Prototypes for many system calls */
#include <errno.h>      /* Declares errno and defines error constants */
#include <string.h>     /* Commonly used string-handling functions */
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/stat.h>
#include <stddef.h>                     /* For definition of offsetof() */
#include <stdarg.h>                     /* For definition of offsetof() */
#include <limits.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>  /* Type definitions used by many programs */
#include <unistd.h>     /* Prototypes for many system calls */
#include <signal.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <time.h>
#include <syslog.h>
void usageErr(const char *format, ...)
{va_list argList;fflush(stdout);           /* Flush any pending stdout */fprintf(stderr, "Usage: ");va_start(argList, format);vfprintf(stderr, format, argList);va_end(argList);fflush(stderr);           /* In case stderr is not line-buffered */exit(EXIT_FAILURE);
}static void printShmDS(const struct shmid_ds *ds)
{printf("Size:                      %ld\n", (long) ds->shm_segsz);printf("# of attached processes:   %ld\n", (long) ds->shm_nattch);printf("Mode:                      %lo",(unsigned long) ds->shm_perm.mode);
#ifdef SHM_DESTprintf("%s", (ds->shm_perm.mode & SHM_DEST) ? " [DEST]" : "");
#endif
#ifdef SHM_LOCKEDprintf("%s", (ds->shm_perm.mode & SHM_LOCKED) ? " [LOCKED]" : "");
#endifprintf("\n");printf("Last shmat():              %s", ctime(&ds->shm_atime));printf("Last shmdt():              %s", ctime(&ds->shm_dtime));printf("Last change:               %s", ctime(&ds->shm_ctime));printf("Creator PID:               %ld\n", (long) ds->shm_cpid);printf("PID of last attach/detach: %ld\n", (long) ds->shm_lpid);
}
void showmemDs(int shmId)
{struct shmid_ds ds;memset(&ds,0,sizeof(struct shmid_ds));if (shmctl(shmId, IPC_STAT, &ds) == -1){perror("shmctl");}else{printShmDS(&ds);}
}
int shareMemChildAndParent(int argc, char *argv[])
{int childpid;int id;int i;int buf[10];char *ptr;int totalbytes = 0;if((childpid = fork ())==-1){perror("fork");exit(EXIT_FAILURE);}if(childpid==0){if ((id = shmget((key_t)12345,50*sizeof(char), IPC_CREAT)) == -1){perror("Failed to create shared memory segment");exit(EXIT_FAILURE);}showmemDs(id);if ((ptr = (char *)shmat(id, NULL, 0)) == NULL){if (shmctl(id, IPC_RMID, NULL) == -1)perror("Failed to  remove memory segment");exit(EXIT_FAILURE);}for(i=0;argv[1][i]!='\0';i++){*ptr=argv[1][i];ptr++;}printf("this is child,write argv[1] to shm.\nyou input charater count is %d\n",i);sleep(5);exit(EXIT_SUCCESS);}else{wait(NULL);if ((id = shmget((key_t)12345, 50*sizeof(char), IPC_CREAT)) == -1){perror("Failed to create shared memory segment");exit(EXIT_FAILURE);}showmemDs(id);if ((ptr = (char *)shmat(id, NULL, 0)) == NULL){perror("shmat");if (shmctl(id, IPC_RMID, NULL) == -1)perror("Failed to  remove memory segment");exit(EXIT_FAILURE);}printf("this is parent,input charater is %s\n",ptr);if (shmctl(id, IPC_RMID, NULL) == -1){perror("Failed to  remove memory segment");exit(EXIT_FAILURE);}sleep(3);exit(EXIT_SUCCESS);}
}int main(int argc, char *argv[])
{shareMemChildAndParent(argc,argv);exit(EXIT_SUCCESS);
}

2:执行结果


这篇关于linux 进程间通信--systemV 共享内存 实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/701648

相关文章

Linux内核定时器使用及说明

《Linux内核定时器使用及说明》文章详细介绍了Linux内核定时器的特性、核心数据结构、时间相关转换函数以及操作API,通过示例展示了如何编写和使用定时器,包括按键消抖的应用... 目录1.linux内核定时器特征2.Linux内核定时器核心数据结构3.Linux内核时间相关转换函数4.Linux内核定时

Linux镜像文件制作方式

《Linux镜像文件制作方式》本文介绍了Linux镜像文件制作的过程,包括确定磁盘空间布局、制作空白镜像文件、分区与格式化、复制引导分区和其他分区... 目录1.确定磁盘空间布局2.制作空白镜像文件3.分区与格式化1) 分区2) 格式化4.复制引导分区5.复制其它分区1) 挂载2) 复制bootfs分区3)

Go异常处理、泛型和文件操作实例代码

《Go异常处理、泛型和文件操作实例代码》Go语言的异常处理机制与传统的面向对象语言(如Java、C#)所使用的try-catch结构有所不同,它采用了自己独特的设计理念和方法,:本文主要介绍Go异... 目录一:异常处理常见的异常处理向上抛中断程序恢复程序二:泛型泛型函数泛型结构体泛型切片泛型 map三:文

Linux服务器数据盘移除并重新挂载的全过程

《Linux服务器数据盘移除并重新挂载的全过程》:本文主要介绍在Linux服务器上移除并重新挂载数据盘的整个过程,分为三大步:卸载文件系统、分离磁盘和重新挂载,每一步都有详细的步骤和注意事项,确保... 目录引言第一步:卸载文件系统第二步:分离磁盘第三步:重新挂载引言在 linux 服务器上移除并重新挂p

Linux下屏幕亮度的调节方式

《Linux下屏幕亮度的调节方式》文章介绍了Linux下屏幕亮度调节的几种方法,包括图形界面、手动调节(使用ACPI内核模块)和外接显示屏调节,以及自动调节软件(CaliseRedshift和Reds... 目录1 概述2 手动调节http://www.chinasem.cn2.1 手动屏幕调节2.2 外接显

Linux(centos7)虚拟机没有IP问题及解决方案

《Linux(centos7)虚拟机没有IP问题及解决方案》文章介绍了在CentOS7中配置虚拟机网络并使用Xshell连接虚拟机的步骤,首先,检查并配置网卡ens33的ONBOOT属性为yes,然后... 目录输入查看ZFhrxIP命令:ip addr查看,没有虚拟机IP修改ens33配置文件重启网络Xh

springboot+mybatis一对多查询+懒加载实例

《springboot+mybatis一对多查询+懒加载实例》文章介绍了如何在SpringBoot和MyBatis中实现一对多查询的懒加载,通过配置MyBatis的`fetchType`属性,可以全局... 目录springboot+myBATis一对多查询+懒加载parent相关代码child 相关代码懒

C++中的解释器模式实例详解

《C++中的解释器模式实例详解》这篇文章总结了C++标准库中的算法分类,还介绍了sort和stable_sort的区别,以及remove和erase的结合使用,结合实例代码给大家介绍的非常详细,感兴趣... 目录1、非修改序列算法1.1 find 和 find_if1.2 count 和 count_if1

linux实现对.jar文件的配置文件进行修改

《linux实现对.jar文件的配置文件进行修改》文章讲述了如何使用Linux系统修改.jar文件的配置文件,包括进入文件夹、编辑文件、保存并退出编辑器,以及重新启动项目... 目录linux对.jar文件的配置文件进行修改第一步第二步 第三步第四步总结linux对.jar文件的配置文件进行修改第一步进

MySQL中如何求平均值常见实例(AVG函数详解)

《MySQL中如何求平均值常见实例(AVG函数详解)》MySQLavg()是一个聚合函数,用于返回各种记录中表达式的平均值,:本文主要介绍MySQL中用AVG函数如何求平均值的相关资料,文中通过代... 目录前言一、基本语法二、示例讲解1. 计算全表平均分2. 计算某门课程的平均分(例如:Math)三、结合