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之systemV共享内存方式

《Linux之systemV共享内存方式》:本文主要介绍Linux之systemV共享内存方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、工作原理二、系统调用接口1、申请共享内存(一)key的获取(二)共享内存的申请2、将共享内存段连接到进程地址空间3、将

快速修复一个Panic的Linux内核的技巧

《快速修复一个Panic的Linux内核的技巧》Linux系统中运行了不当的mkinitcpio操作导致内核文件不能正常工作,重启的时候,内核启动中止于Panic状态,该怎么解决这个问题呢?下面我们就... 感谢China编程(www.chinasem.cn)网友 鸢一雨音 的投稿写这篇文章是有原因的。为了配置完

Python如何精准判断某个进程是否在运行

《Python如何精准判断某个进程是否在运行》这篇文章主要为大家详细介绍了Python如何精准判断某个进程是否在运行,本文为大家整理了3种方法并进行了对比,有需要的小伙伴可以跟随小编一起学习一下... 目录一、为什么需要判断进程是否存在二、方法1:用psutil库(推荐)三、方法2:用os.system调用

Linux命令之firewalld的用法

《Linux命令之firewalld的用法》:本文主要介绍Linux命令之firewalld的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux命令之firewalld1、程序包2、启动firewalld3、配置文件4、firewalld规则定义的九大

Java程序进程起来了但是不打印日志的原因分析

《Java程序进程起来了但是不打印日志的原因分析》:本文主要介绍Java程序进程起来了但是不打印日志的原因分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java程序进程起来了但是不打印日志的原因1、日志配置问题2、日志文件权限问题3、日志文件路径问题4、程序

Linux之计划任务和调度命令at/cron详解

《Linux之计划任务和调度命令at/cron详解》:本文主要介绍Linux之计划任务和调度命令at/cron的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux计划任务和调度命令at/cron一、计划任务二、命令{at}介绍三、命令语法及功能 :at

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

Linux内核参数配置与验证详细指南

《Linux内核参数配置与验证详细指南》在Linux系统运维和性能优化中,内核参数(sysctl)的配置至关重要,本文主要来聊聊如何配置与验证这些Linux内核参数,希望对大家有一定的帮助... 目录1. 引言2. 内核参数的作用3. 如何设置内核参数3.1 临时设置(重启失效)3.2 永久设置(重启仍生效

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

SQL表间关联查询实例详解

《SQL表间关联查询实例详解》本文主要讲解SQL语句中常用的表间关联查询方式,包括:左连接(leftjoin)、右连接(rightjoin)、全连接(fulljoin)、内连接(innerjoin)、... 目录简介样例准备左外连接右外连接全外连接内连接交叉连接自然连接简介本文主要讲解SQL语句中常用的表