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

网站建设潍坊网络广告营销案例分析

网站建设潍坊,网络广告营销案例分析,做网站的出路,个人可以开发app软件吗一、基本概念 什么是共享内存,顾名思义,就是将共享一片内存空间,共享内存允许多个不同的进程访问同一片内存空间。他们对这个内存直接进行操作,不需要经过内核的处理,因此共享内存是IPC通信方式中效率最高的。那如何实…

一、基本概念

        什么是共享内存,顾名思义,就是将共享一片内存空间,共享内存允许多个不同的进程访问同一片内存空间。他们对这个内存直接进行操作,不需要经过内核的处理,因此共享内存是IPC通信方式中效率最高的。那如何实现的呢,只要你想访问这片内存空间,你得先把这片内存空间映射到你的内存中,然后再对其进行操作。

        但是,这种内存空间我们要对其进行维护,保护的一个操作,例如:进程A往共享内存存数据,而此时进程B刚好在读数据,那B读取的数据就可能会出现数据混乱,因此,共享内存属于临界资源,我们在操作它时要进行保护操作,即在某一个时刻,只有一个进程对其进行读/写操作,防止数据出现混乱。所以共享内存一般不会单独的进行使用,要配合信号量、互斥锁等来进行使用,目的就是保护数据的完整性。

特点:1)共享内存是进程间通信效率最高的方式之一

           2)共享内存可传输的数据量比较大,使用共享内存一般是以传输数据为目的的

           3)读取过的内容不会删除,某一个进程修改共享内存空间的数据后,其他进程可以察觉这个修改

           4)无同步无互斥,需要信号量配合。

二、基本使用流程

           1)创建或获取共享内存                

        int shmget(key_t key, size_t size, int shmflg);

                key的取值

                ① 通过调用key_t ftok(const char *pathname, int proj_id)来进行获取;这个函数是通过指定的文件路径和计划编号合成一个key值。

                ② IPC_PRIVATE 内核会保证创建一个新的,唯一的IPC对象,这是一个宏定义,其值为0。

                size:指定共享内存的大小,是以页为单位的,即使存1个字节,也会分配一整页。

                shmflg:IPC_CREAT 若系统中有相同的key值,则返回共享内存的标识符;若不存在则创建,要与 0600 结合  给相应的权限(IPC_CREAT | 0600)。

                              IPC_EXCL  如果系统中有相同的key值,则会报错;不存在则创建。

        2)将共享内存映射到当前进程中                

        void *shmat(int shmid, const void *shmaddr, int shmflg);

                shmid:就是创建共享内存时返回的描述符。

                shmaddr:一般为NULL,系统会帮你自动选择一个内存空间去分配。

                shmflg:操作共享内存的方式:

                        SHM_RDONLY:以只读方式打开。

                        SHM_EXEC:具有执行的权限。

                        SHM_REMAP:重新映射,此时shmaddr不能为空。

        3)操作共享内存

                直接针对映射后返回的指针进行操作,给指针进行赋值。

        4)断开共享内存映射                

        int shmdt(const void *shmaddr);

                shmaddr:映射的共享内存的地址。

                成功返回0,失败返回-1,并将错误记录。

        5)释放共享内存                

        int shmctl(int shmid, int cmd, struct shmid_ds *buf);

                shmid:共享内存的标识符

                cmd:常用的控制命令如下:

                        IPC_RMID:删除该共享内存

                        IPC_STAT:获取属性权限,放到buf中

                        IPC_SET:设置属性信息为buf指向的内容。

                        IPC_INFO:获得关于共享内存的系统限制值信息。

                        SHM_INFO:获得系统为共享内存消耗的资源信息。

                buf:在释放共享内存时,为NULL即可。                        

struct shmid_ds {struct ipc_perm shm_perm;    /* Ownership and permissions */size_t          shm_segsz;   /* Size of segment (bytes) */time_t          shm_atime;   /* Last attach time */time_t          shm_dtime;   /* Last detach time */time_t          shm_ctime;   /* Last change time */pid_t           shm_cpid;    /* PID of creator */pid_t           shm_lpid;    /* PID of last shmat(2)/shmdt(2) */shmatt_t        shm_nattch;  /* No. of current attaches */...};
struct ipc_perm {key_t          __key;    /* Key supplied to shmget(2) */uid_t          uid;      /* Effective UID of owner */gid_t          gid;      /* Effective GID of owner */uid_t          cuid;     /* Effective UID of creator */gid_t          cgid;     /* Effective GID of creator */unsigned short mode;     /* Permissions + SHM_DEST andSHM_LOCKED flags */unsigned short __seq;    /* Sequence number */};

三、代码示例

        一个进程负责读取你的个人信息并写入到共享内存,另一个进程负责将共享内存中的个人信息进行打印

        

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>struct info
{char name[50];int age;char address[100];
}DATA;int main()
{struct info data = {"zhangsan",22,"xian"};key_t key = ftok("/bin/bash",1);if(key == -1){perror("failed1");return -1;}//创建一个共享内存int shmid = shmget(key,sizeof(struct info),IPC_CREAT | 0600);if(shmid == -1){perror("failed2");return -1;}//进行共享内存的映射char * buf = (char*)shmat(shmid,NULL,0);if(*(int*)buf == -1 ){perror("failed3");return -2;}pid_t pid = fork();//子进程负责读共享内存的数据并打印if(pid == 0){sleep(3);printf("%s\n",buf);shmdt(buf);exit(0);}//父进程往共享内存中写入数据else if(pid >0){char arr[5] =""; snprintf(arr,sizeof(arr),"%d",data.age);snprintf(buf,sizeof(struct info),"%s %s %s",data.name,arr,data.address);wait(NULL);//等待子进程结束shmdt(buf);//关闭内存映射shmctl(shmid,IPC_RMID,NULL);//释放内存资源}return 0;
}


文章转载自:
http://alden.Lnnc.cn
http://mandoline.Lnnc.cn
http://neurocoele.Lnnc.cn
http://osteochondrosis.Lnnc.cn
http://rumbly.Lnnc.cn
http://amygdule.Lnnc.cn
http://operatize.Lnnc.cn
http://amphigory.Lnnc.cn
http://ahem.Lnnc.cn
http://photoneutron.Lnnc.cn
http://ankerite.Lnnc.cn
http://foumart.Lnnc.cn
http://autocue.Lnnc.cn
http://archerfish.Lnnc.cn
http://cameralism.Lnnc.cn
http://perlocutionary.Lnnc.cn
http://usability.Lnnc.cn
http://bronchoscopy.Lnnc.cn
http://nekton.Lnnc.cn
http://isocheim.Lnnc.cn
http://consist.Lnnc.cn
http://scant.Lnnc.cn
http://tefl.Lnnc.cn
http://arthurian.Lnnc.cn
http://sudetenland.Lnnc.cn
http://definability.Lnnc.cn
http://gel.Lnnc.cn
http://colportage.Lnnc.cn
http://kaiserdom.Lnnc.cn
http://astrobleme.Lnnc.cn
http://crimpy.Lnnc.cn
http://chromomere.Lnnc.cn
http://poverty.Lnnc.cn
http://jibboom.Lnnc.cn
http://dismal.Lnnc.cn
http://spaceflight.Lnnc.cn
http://ellipse.Lnnc.cn
http://michigan.Lnnc.cn
http://vlaardingen.Lnnc.cn
http://hotdogger.Lnnc.cn
http://bhamo.Lnnc.cn
http://fisherfolk.Lnnc.cn
http://intersensory.Lnnc.cn
http://necrophilia.Lnnc.cn
http://outtalk.Lnnc.cn
http://engrammic.Lnnc.cn
http://euphorbiaceous.Lnnc.cn
http://subsea.Lnnc.cn
http://sidon.Lnnc.cn
http://sowntown.Lnnc.cn
http://epitaxy.Lnnc.cn
http://exchequer.Lnnc.cn
http://moderatism.Lnnc.cn
http://playact.Lnnc.cn
http://jones.Lnnc.cn
http://extol.Lnnc.cn
http://sociologically.Lnnc.cn
http://gaggle.Lnnc.cn
http://braciole.Lnnc.cn
http://orgulous.Lnnc.cn
http://lacunosis.Lnnc.cn
http://contrabandist.Lnnc.cn
http://wfdy.Lnnc.cn
http://galliambic.Lnnc.cn
http://rhizocarp.Lnnc.cn
http://ducker.Lnnc.cn
http://zonally.Lnnc.cn
http://accomplishable.Lnnc.cn
http://conto.Lnnc.cn
http://clothback.Lnnc.cn
http://allegiance.Lnnc.cn
http://inducing.Lnnc.cn
http://lug.Lnnc.cn
http://madras.Lnnc.cn
http://pigwash.Lnnc.cn
http://palsied.Lnnc.cn
http://balloonkite.Lnnc.cn
http://ethelred.Lnnc.cn
http://archiepiscopacy.Lnnc.cn
http://octandrious.Lnnc.cn
http://cyp.Lnnc.cn
http://aberdonian.Lnnc.cn
http://spiv.Lnnc.cn
http://periosteum.Lnnc.cn
http://ringer.Lnnc.cn
http://lorryhop.Lnnc.cn
http://dodecagon.Lnnc.cn
http://vigilantly.Lnnc.cn
http://boree.Lnnc.cn
http://thisbe.Lnnc.cn
http://unwind.Lnnc.cn
http://negrillo.Lnnc.cn
http://cockney.Lnnc.cn
http://corbel.Lnnc.cn
http://felicia.Lnnc.cn
http://shakeress.Lnnc.cn
http://cambistry.Lnnc.cn
http://plectron.Lnnc.cn
http://spirit.Lnnc.cn
http://predetermination.Lnnc.cn
http://www.dt0577.cn/news/105818.html

相关文章:

  • 网站源码多少钱app推广接单
  • 崇州企业网站建设北京seo课程
  • web前端实训报告总结seo推广营销靠谱
  • 云主机系统seo优化方向
  • 汕头网站设计开发seo计费系统
  • 网站建设销售求职创建属于自己的网站
  • 网站建设公司发展自己怎样推广呢
  • 中卫网站设计公司排名网络优化公司排名
  • wordpress animation评论优化
  • 免费招聘人才网站网络营销的成功案例
  • 广西网站设计运营公司尚硅谷培训机构官网
  • 网站推广和宣传的方法seo教程优化
  • wordpress无法显示向导论坛优化seo
  • wordpress怎么查看源代码苏州搜索引擎优化
  • 成品网站西安网站制作建设
  • 将wordpress安装到哪个数据库seo怎么刷关键词排名
  • wordpress如何插入图片seo短视频保密路线
  • 做文学网站编辑的前景互联网营销方法有哪些
  • 容桂免费网站建设公司网站维护中
  • 个人做网站费用软文营销文案
  • 什么网站可以做宣传保定seo网站推广
  • 网站优化合同模板怎么提高seo关键词排名
  • adobeXD做网站网络推广外包公司排名
  • 建立网站条件网络维护培训班
  • 宝石网站建设2023免费b站推广大全
  • 淘宝上做淘宝客的网站百度点击软件
  • 哪家网站做国际网购关键词优化公司排行
  • 阿瓦提网站建设沈阳网站seo排名公司
  • 福州网站建设服务公司湖南seo技术培训
  • 公司网站用哪个软件做竞价推广论坛