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

相关文章

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java进程异常故障定位及排查过程

《Java进程异常故障定位及排查过程》:本文主要介绍Java进程异常故障定位及排查过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、故障发现与初步判断1. 监控系统告警2. 日志初步分析二、核心排查工具与步骤1. 进程状态检查2. CPU 飙升问题3. 内存

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

java向微信服务号发送消息的完整步骤实例

《java向微信服务号发送消息的完整步骤实例》:本文主要介绍java向微信服务号发送消息的相关资料,包括申请测试号获取appID/appsecret、关注公众号获取openID、配置消息模板及代码... 目录步骤1. 申请测试系统2. 公众号账号信息3. 关注测试号二维码4. 消息模板接口5. Java测试

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

在Linux终端中统计非二进制文件行数的实现方法

《在Linux终端中统计非二进制文件行数的实现方法》在Linux系统中,有时需要统计非二进制文件(如CSV、TXT文件)的行数,而不希望手动打开文件进行查看,例如,在处理大型日志文件、数据文件时,了解... 目录在linux终端中统计非二进制文件的行数技术背景实现步骤1. 使用wc命令2. 使用grep命令

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

linux重启命令有哪些? 7个实用的Linux系统重启命令汇总

《linux重启命令有哪些?7个实用的Linux系统重启命令汇总》Linux系统提供了多种重启命令,常用的包括shutdown-r、reboot、init6等,不同命令适用于不同场景,本文将详细... 在管理和维护 linux 服务器时,完成系统更新、故障排查或日常维护后,重启系统往往是必不可少的步骤。本文

基于Linux的ffmpeg python的关键帧抽取

《基于Linux的ffmpegpython的关键帧抽取》本文主要介绍了基于Linux的ffmpegpython的关键帧抽取,实现以按帧或时间间隔抽取关键帧,文中通过示例代码介绍的非常详细,对大家的学... 目录1.FFmpeg的环境配置1) 创建一个虚拟环境envjavascript2) ffmpeg-py