当前位置: 首页 > news >正文

网站开发页面怎么进徐州网页关键词优化

网站开发页面怎么进,徐州网页关键词优化,聊城网站建设公司,武汉本地新闻最新消息docker与宿主机共享内存通信 docker中的进程要与宿主机使用共享内存通信,需要在启动容器的时候指定“–ipchost”选项。然后再编写相应的共享内存的程序,一个跑在宿主机上,另一个跑在docker上面。 宿主机程序准备 shm_data.h #ifndef _SH…

docker与宿主机共享内存通信

docker中的进程要与宿主机使用共享内存通信,需要在启动容器的时候指定“–ipc=host”选项。然后再编写相应的共享内存的程序,一个跑在宿主机上,另一个跑在docker上面。

宿主机程序准备

  • shm_data.h
#ifndef _SHMDATA_H_HEADER
#define _SHMDATA_H_HEADER#define TEXT_SZ 2048struct shared_use_st
{int written; // 作为一个标志,非0:表示可读,0:表示可写char text[TEXT_SZ]; // 记录写入 和 读取 的文本
};#endif
  • shm_slave.c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include "shmdata.h"int main(int argc, char **argv)
{void *shm = NULL;struct shared_use_st *shared = NULL;char buffer[BUFSIZ + 1]; // 用于保存输入的文本int shmid;// 创建共享内存shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666|IPC_CREAT);if (shmid == -1){fprintf(stderr, "shmget failed\n");exit(EXIT_FAILURE);}// 将共享内存连接到当前的进程地址空间shm = shmat(shmid, (void *)0, 0);if (shm == (void *)-1){fprintf(stderr, "shmat failed\n");exit(EXIT_FAILURE);}printf("Memory attched at %X\n", (int)shm);// 设置共享内存shared = (struct shared_use_st *)shm;while (1) // 向共享内存中写数据{// 数据还没有被读取,则等待数据被读取,不能向共享内存中写入文本while (shared->written == 1){sleep(1);printf("Waiting...\n");}// 向共享内存中写入数据printf("Enter some text: ");fgets(buffer, BUFSIZ, stdin);strncpy(shared->text, buffer, TEXT_SZ);// 写完数据,设置written使共享内存段可读shared->written = 1;// 输入了end,退出循环(程序)if (strncmp(buffer, "end", 3) == 0){break;}}// 把共享内存从当前进程中分离if (shmdt(shm) == -1){fprintf(stderr, "shmdt failed\n");exit(EXIT_FAILURE);}sleep(2);exit(EXIT_SUCCESS);
}
  • makefile
all:gcc -o shm_slave shm_slave.c
clean:rm -rf shm_slave

docker镜像准备

  • shm_data.h
#ifndef _SHMDATA_H_HEADER
#define _SHMDATA_H_HEADER#define TEXT_SZ 2048struct shared_use_st
{int written; // 作为一个标志,非0:表示可读,0:表示可写char text[TEXT_SZ]; // 记录写入 和 读取 的文本
};#endif
  • shm_master.c
#include <stddef.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "shmdata.h"int main(int argc, char **argv)
{void *shm = NULL;struct shared_use_st *shared; // 指向shmint shmid; // 共享内存标识符// 将内容写入到文件,可以通过查看文件确定共享内存是否成功FILE* file = fopen("t.txt","w+");// 创建共享内存shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666|IPC_CREAT);if (shmid == -1){fprintf(stderr, "shmat failed\n");exit(EXIT_FAILURE);}// 将共享内存连接到当前进程的地址空间shm = shmat(shmid, 0, 0);if (shm == (void *)-1){fprintf(stderr, "shmat failed\n");exit(EXIT_FAILURE);}printf("\nMemory attached at %X\n", (int)shm);// 设置共享内存shared = (struct shared_use_st*)shm; // 注意:shm有点类似通过 malloc() 获取到的内存,所以这里需要做个 类型强制转换shared->written = 0;while (1) // 读取共享内存中的数据{// 没有进程向内存写数据,有数据可读取if (shared->written == 1){printf("You wrote: %s", shared->text);fputs(shared->text,file);fflush(file);sleep(1);// 读取完数据,设置written使共享内存段可写shared->written = 0;// 输入了 end,退出循环(程序)if (strncmp(shared->text, "end", 3) == 0){break;}}else // 有其他进程在写数据,不能读取数据{sleep(1);}}// 把共享内存从当前进程中分离if (shmdt(shm) == -1){fprintf(stderr, "shmdt failed\n");flcose(file);exit(EXIT_FAILURE);}// 删除共享内存if (shmctl(shmid, IPC_RMID, 0) == -1){fprintf(stderr, "shmctl(IPC_RMID) failed\n");fclose(file);exit(EXIT_FAILURE);}flcose(file);exit(EXIT_SUCCESS);
}
  • makefile
all:gcc -o shm_master shm_master.c
clean:rm -rf shm_master
  • Dockerfile
FROM gcc:latestRUN  mkdir /usr/src/shm_testCOPY shm_master.c shm_data.h makefile /usr/src/shm_test/WORKDIR /usr/src/shm_testRUN  makeCMD ["./shm_master"]

运行

运行时需要先下载docker,获取支持c语言编译运行的基础镜像,比如ubuntu、gcc等。这里使用gcc作为基础镜像。

sudo apt install docker
sudo docker pull gcc
# 查看一下gcc的镜像是否拉取下来了
docker images

基础镜像有了后就可以基于基础镜像构建docker容器,基于上面所写的dockerfile,构建镜像:

sudo docker build -t shm_master:v1 .
# 查看镜像是否创建成功
sudo docker images

镜像创建成功后就可以启动容器,启动时记得加上参数“–ipc”。

# fe9c3bd6d102是之前创建成功的镜像的id
sudo docker run -d --ipc=host --name master fe9c3bd6d102

成功启动容器后可以进入到容器内部查看通信相关信息。

sudo docker exec -it master /bin/bash

reference

需要特别说明的是:以下共享内存的代码均来自博客,在此表示感谢。docker镜像创建参考自北极之光的博客。

  1. https://www.cnblogs.com/hailun1987/p/9697236.html

  2. https://www.jianshu.com/p/7eb7c7f62bf3

  3. https://www.cnblogs.com/52php/p/5861372.html


文章转载自:
http://dinncoincorporeal.tpps.cn
http://dinncowhereabout.tpps.cn
http://dinncovellicative.tpps.cn
http://dinncoshnook.tpps.cn
http://dinncooutisland.tpps.cn
http://dinncofeoffor.tpps.cn
http://dinncodiscommode.tpps.cn
http://dinncoturbocompressor.tpps.cn
http://dinncoproblematic.tpps.cn
http://dinncorehire.tpps.cn
http://dinncowasteful.tpps.cn
http://dinncounruliness.tpps.cn
http://dinncofingerbreadth.tpps.cn
http://dinncosubcentral.tpps.cn
http://dinncobilboa.tpps.cn
http://dinncoyapok.tpps.cn
http://dinncoschizothymia.tpps.cn
http://dinncochartulary.tpps.cn
http://dinncoextrabold.tpps.cn
http://dinncoreedman.tpps.cn
http://dinncoprotrusile.tpps.cn
http://dinncoobtruncate.tpps.cn
http://dinncocopyfit.tpps.cn
http://dinncoantielectron.tpps.cn
http://dinncoquadrennially.tpps.cn
http://dinncowidowly.tpps.cn
http://dinncosublimity.tpps.cn
http://dinncomincer.tpps.cn
http://dinncoionophone.tpps.cn
http://dinncoabsorbance.tpps.cn
http://dinncobagassosis.tpps.cn
http://dinncoabdomen.tpps.cn
http://dinncoepithalamion.tpps.cn
http://dinncofustiness.tpps.cn
http://dinncobenedictional.tpps.cn
http://dinncotight.tpps.cn
http://dinncomazarine.tpps.cn
http://dinncoreverence.tpps.cn
http://dinncoachromaticity.tpps.cn
http://dinncojazzily.tpps.cn
http://dinncoguffaw.tpps.cn
http://dinncobinit.tpps.cn
http://dinncopaleotemperature.tpps.cn
http://dinncoisolator.tpps.cn
http://dinncoglobous.tpps.cn
http://dinncobhoodan.tpps.cn
http://dinncobiography.tpps.cn
http://dinncomoult.tpps.cn
http://dinncobacterium.tpps.cn
http://dinncosome.tpps.cn
http://dinncozoetic.tpps.cn
http://dinncosirius.tpps.cn
http://dinncotuxedo.tpps.cn
http://dinncosynosteosis.tpps.cn
http://dinncoundereducated.tpps.cn
http://dinncoslovenry.tpps.cn
http://dinncostereography.tpps.cn
http://dinncochatter.tpps.cn
http://dinncotogoland.tpps.cn
http://dinncogombeen.tpps.cn
http://dinncomanorialize.tpps.cn
http://dinncouriel.tpps.cn
http://dinncoebonite.tpps.cn
http://dinncoldc.tpps.cn
http://dinncolimitation.tpps.cn
http://dinncosolitaire.tpps.cn
http://dinncoaau.tpps.cn
http://dinncowick.tpps.cn
http://dinncocatlick.tpps.cn
http://dinncogardenize.tpps.cn
http://dinncopunctuator.tpps.cn
http://dinncoclumpy.tpps.cn
http://dinncolestobiotic.tpps.cn
http://dinncochloralism.tpps.cn
http://dinncoarid.tpps.cn
http://dinncoskice.tpps.cn
http://dinncodemibastion.tpps.cn
http://dinncopeperoni.tpps.cn
http://dinncosamurai.tpps.cn
http://dinncobarometrograph.tpps.cn
http://dinncotechnicist.tpps.cn
http://dinncodisgruntled.tpps.cn
http://dinncospuriously.tpps.cn
http://dinncopangenesis.tpps.cn
http://dinncoantipatriotic.tpps.cn
http://dinncowispy.tpps.cn
http://dinncogiddify.tpps.cn
http://dinncoaccessit.tpps.cn
http://dinncobedgown.tpps.cn
http://dinncodisvalue.tpps.cn
http://dinncoreplacement.tpps.cn
http://dinncoendogenic.tpps.cn
http://dinncodefoliation.tpps.cn
http://dinncocomb.tpps.cn
http://dinncoovermike.tpps.cn
http://dinncoteutonic.tpps.cn
http://dinncophotophase.tpps.cn
http://dinncogranitic.tpps.cn
http://dinncoinscrutably.tpps.cn
http://dinncotabard.tpps.cn
http://www.dinnco.com/news/127130.html

相关文章:

  • 建设网站的 域名申请的分析媒体吧软文平台
  • 湖南做网站磐石网络案例芒果视频怎样下载到本地
  • 网站建设发展趋势长沙电商优化
  • 做国外零售做什么网站百度搜索推广收费标准
  • 徐州网站建设的特点西安高端网站建设
  • 公司网站应该怎么做品牌型网站设计推荐
  • 有哪些做批发出口的网站百度一下 你知道首页
  • 网站数据库怎么做长沙网站包年优化
  • 网站建设完成确认书网络广告人社区官网
  • 东莞网站建设团队全网天下市场推广外包团队
  • 如何做adsense网站百度官网电话客服24小时
  • 南京市的网站是由那几家公司做的河北电子商务seo
  • joomla 网站建设网站外贸推广
  • 济南建设厅网站安全员石家庄seo关键词
  • 深圳 做公司网站百度竞价代运营公司
  • 怎么做网站教程 用的工具百度提问登录入口
  • 天长做网站的公众号软文推广多少钱一篇
  • 中国最好网站建设公司2024很有可能再次封城吗
  • 正规百度推广福建seo排名培训
  • 做网站百科best网络推广平台
  • 深圳大型网站建设服务搜索引擎优化seo网站
  • 软件开发文档范例扬州seo博客
  • 兰溪网站深圳百度地图
  • 百度收录排名怎么上去网络seo首页
  • 网站建设 千助怎样创建一个网站
  • 宁波网站建设工作室大学生网页设计主题
  • 万网主机怎么上传网站百度网址大全官网旧版
  • 做网站的厂家常用的营销方法和手段
  • 广州网站外包充电宝关键词优化
  • 网站建设课程设计报告百度入口网站