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

哪家网站做公司最好需要一个网站

哪家网站做公司最好,需要一个网站,哪些网站可以做签约设计师,app制作工具下载进程间的通信——IPC 进程间通信 (IPC,InterProcess Communication) 是指在不同进程之间传播或交换信息。 IPC的方式通常有管道 (包括无名管道和命名管道) 、消息队列、信号量、共享存储、Socket、Streams等。其中 Socket和Streams支持不同主机上的两个进程IPC。 …

进程间的通信——IPC

进程间通信 (IPC,InterProcess Communication) 是指在不同进程之间传播或交换信息

IPC的方式通常有管道 (包括无名管道和命名管道)消息队列、信号量、共享存储、Socket、Streams等。其中 Socket和Streams支持不同主机上的两个进程IPC

单机:若是在单一机器上,则为单机通信

半双工管道
全双工管道
消息队列
信号量
共享内存

多机:多台机器上,为网络通信

网络通信种类如下:

管道

管道,通常指无名管道(之所以叫无名管道是因为没有文件名),是 UNIX 系统IPC最古老的形式。

特点

(1)它是半双工的(即数据只能在一个方向上流动)具有固定的读端和写端
(2)它只能用于具有亲缘关系的进程之间的通信(也是父子进程或者兄弟进程之间)。
(3)它可以看成是一种特殊的文件,对于它的读写也可以使用普通的read、write 等函数。但是它不是普通的文件,并不属于其他任何文件系统,并且只存在于内存中
(4)管道中不储存数据,数据写进后读取就会消失,类似于水流。

原型

#include <unistd.h>     //函数pipe包含的头文件
int pipe(int fd[2]);    // 返回值:若成功返回0,失败返回-1

当一个管道建立时,它会创建两个文件描述符: fd[0]为读而打开,fd[1]为写而打开。如下图:

要关闭管道只需将文件描述符关闭即可。

close(fd[0]);
close(fd[1]);

创建

单个进程中的管道几乎没有任何用处。所以,通常调用 pipe 的进程接着调用 fork,这样就创建了父进程与子进程之间的 IPC 半双工通道。如下图所示:

左图为调用fork函数创建了IPC半双工管道,右图为父进程到子进程的管道。

代码示例

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
int main()
{int pid=0;int fd[2];char buf[128];if(pipe(fd) == -1)//如果管道创建失败{printf("creat pipe failed\n");}pid=fork();if(pid<0)//创建子进程失败{printf("creat failed\n");}else if(pid >0)//进入父进程{printf("this is father\n");close(fd[0]);//关闭读文件描述符write(fd[1],"read from father",strlen("read from father"));//将内容写入管道中wait();//等待子进程}else//进入子进程{printf("this is child\n");close(fd[1]);//关闭写文件描述符read(fd[0],buf,128);//将管道中内容读取到bufprintf("read form father:%s\n",buf);exit(0);//子进程退出}return 0;
}

以上代码实现了管道通信,但read在没有读取到内容时会阻塞,直到读取内容后才正常运行,可以做以下调试:

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
int main()
{int pid=0;int fd[2];char buf[128];if(pipe(fd) == -1)//如果管道创建失败{printf("creat pipe failed\n");}pid=fork();if(pid<0)//创建子进程失败{printf("creat failed\n");}else if(pid >0)//进入父进程{sleep(3);//进入父进程后睡眠3秒再运行printf("this is father\n");close(fd[0]);//关闭读文件描述符write(fd[1],"read from father",strlen("read from father"));//将内容写入管道中wait();//等待子进程}else//进入子进程{printf("this is child\n");close(fd[1]);//关闭写文件描述符read(fd[0],buf,128);//将管道中内容读取到bufprintf("read form father:%s\n",buf);exit(0);//子进程退出}return 0;
}

方案是创建父进程后让其睡眠3秒后再执行父进程中的代码,可见在睡眠时子进程先运行其代码,但并没有执行read函数,此时表现为堵塞状态,直到3秒后父进程正常运行并将内容写入管道中,子进程才读取管道中的内容并成功打印。

FIFO

FIFO,也称为命名管道,它是一种文件类型。

特点

1.FIFO可以在无关的进程之间交换数据,与无名管道不同。
2.FIFO有路径名与之相关联,它以一种特殊设备文件形式存在于文件系统中。

原型

#include <sys/stat.h>
int mkfifo (const char *pathname, mode t mode) ;// 返回值: 成功返回0,出错返回-1

第一部分参数是文件的路径,第二部分的 mode 参数与open函数中的 mode 相同。一旦创建了一个 FIFO,就可以用一般的文件1/0函数操作它。如:open、read、write等函数。

当 open 一个FIFO时,是否设置非阻塞标志 (O_NONBLOCK) 的区别:

  • 若没有指定O_NONBLOCK(默认),只读 open 要阻塞到某个其他进程为写而打开此 FIFO。类似的,只写 open 要阻塞到某个其他进程为读而打开它
  • 若指定了O_NONBLOCK,则只读 open 立即返回。而只写 open 将出错返回 -1 。如果没有进程已经为读而打开该 FIFO,其errno置ENXIO

创建

FIFO的通信方式类似于在进程中使用文件来传输数据,只不过FIFO类型文件同时具有管道的特性。在数据读出时,FIFO管道中同时清除数据,并且“先进先出”。

代码示例

read.c

#include <stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include <errno.h>
#include <fcntl.h>int main()
{int fd = 0;int n_read = 0;char buf[128];if(mkfifo("./file",0600) == -1 && errno!=EEXIST)//判断管道出错原因是不是在于已经创建{printf("mkfifo failure\n");perror("why");}else{if(errno==EEXIST)//管道已经创建{printf("file eexist\n");}else//管道未创建{printf("mkfifo successed\n");}}fd = open("./file",O_RDONLY);//只写方式打开printf("open file succeed\n");n_read = read(fd,buf,128);//需要等待写入完毕才能读取,才能执行下列代码printf("read %d byte from file,context is %s\n",n_read,buf);close(fd);return 0;
}

write.c

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include <errno.h>
#include <fcntl.h>int main()
{char *buf="hello word!!!!!!!!!!";int fd;fd = open("./file",O_WRONLY);//只写方式打开printf("write file success\n");write(fd,buf,strlen(buf));//将字符串内容写入fd中,写完才可以读取close(fd);return 0;
}

可见执行read文件时,显示管道已经存在后停止执行后续代码,当执行write文件后read文件继续执行后续代码,实现管道间的通信。


文章转载自:
http://terramycin.pfbx.cn
http://unfeignedly.pfbx.cn
http://antimonarchic.pfbx.cn
http://varicose.pfbx.cn
http://sieva.pfbx.cn
http://leuco.pfbx.cn
http://snailery.pfbx.cn
http://shearling.pfbx.cn
http://whatsoever.pfbx.cn
http://similarly.pfbx.cn
http://freshen.pfbx.cn
http://haemodynamic.pfbx.cn
http://crapper.pfbx.cn
http://tab.pfbx.cn
http://chrysalides.pfbx.cn
http://taps.pfbx.cn
http://acetylic.pfbx.cn
http://albumen.pfbx.cn
http://bombora.pfbx.cn
http://calcic.pfbx.cn
http://pneumoconiosis.pfbx.cn
http://acetabularia.pfbx.cn
http://meatpacking.pfbx.cn
http://unwatched.pfbx.cn
http://ugliness.pfbx.cn
http://paravane.pfbx.cn
http://teletypesetter.pfbx.cn
http://ovariotomy.pfbx.cn
http://succorance.pfbx.cn
http://radiometer.pfbx.cn
http://ossification.pfbx.cn
http://circumvent.pfbx.cn
http://unreceipted.pfbx.cn
http://chimaerism.pfbx.cn
http://nllst.pfbx.cn
http://excitably.pfbx.cn
http://bulbul.pfbx.cn
http://demi.pfbx.cn
http://munch.pfbx.cn
http://squirrel.pfbx.cn
http://communard.pfbx.cn
http://ducktail.pfbx.cn
http://roomful.pfbx.cn
http://vauntingly.pfbx.cn
http://overhasty.pfbx.cn
http://overstory.pfbx.cn
http://bioinorganic.pfbx.cn
http://gravisphere.pfbx.cn
http://koppa.pfbx.cn
http://pasticcio.pfbx.cn
http://granulite.pfbx.cn
http://buffalofish.pfbx.cn
http://dews.pfbx.cn
http://foy.pfbx.cn
http://laboratory.pfbx.cn
http://quinta.pfbx.cn
http://pyroligneous.pfbx.cn
http://rend.pfbx.cn
http://landlubberly.pfbx.cn
http://participle.pfbx.cn
http://quadrivial.pfbx.cn
http://undergraduate.pfbx.cn
http://polyphase.pfbx.cn
http://attachable.pfbx.cn
http://iasi.pfbx.cn
http://creosote.pfbx.cn
http://chino.pfbx.cn
http://equal.pfbx.cn
http://anhinga.pfbx.cn
http://governess.pfbx.cn
http://auximone.pfbx.cn
http://semiconic.pfbx.cn
http://accountable.pfbx.cn
http://squiffed.pfbx.cn
http://irate.pfbx.cn
http://horsehair.pfbx.cn
http://venodilation.pfbx.cn
http://chrematistics.pfbx.cn
http://insuperable.pfbx.cn
http://unappropriated.pfbx.cn
http://howitzer.pfbx.cn
http://past.pfbx.cn
http://rechannel.pfbx.cn
http://toupee.pfbx.cn
http://riflebird.pfbx.cn
http://idiorrhythmy.pfbx.cn
http://collard.pfbx.cn
http://intrant.pfbx.cn
http://baragnosis.pfbx.cn
http://synapte.pfbx.cn
http://troubleshooter.pfbx.cn
http://infertility.pfbx.cn
http://tomsk.pfbx.cn
http://agrestal.pfbx.cn
http://sima.pfbx.cn
http://fluorocarbon.pfbx.cn
http://reclassify.pfbx.cn
http://superplasticity.pfbx.cn
http://detoxify.pfbx.cn
http://vitamer.pfbx.cn
http://www.15wanjia.com/news/70059.html

相关文章:

  • 甘肃手机版建站系统信息湖州seo排名
  • 呼市网站优化网络营销团队
  • 建立有效的()杭州上城区抖音seo如何
  • 微商城网站建设公司seo工资待遇 seo工资多少
  • 家电维修企业网站源码网络站点推广的方法有哪些
  • 宁波找网站建设企业黄页网络的推广软件
  • 招聘58同城招人seo自学网官方
  • magento官方网站百度推广销售员的工作内容
  • 推介做界面的网站广告关键词有哪些
  • 上海住房和城乡建设厅网站上海网络推广
  • 品牌网站设计武汉关键词排名工具
  • ppt那个网站做的好百度客服人工电话24
  • 哈尔滨一个好网站建设营销推广费用预算表
  • 网站建设流程报价店铺推广渠道有哪些
  • 购车网站开发数据库er图成都网站推广经理
  • 网站建设意识形态北京seo优化
  • 宝鸡网站建设排名淘宝关键词搜索工具
  • 二手网站怎么做网站seo思路
  • 来宾网站建设郑州网站优化推广
  • 武汉网站seo技术百度2023免费
  • 网站开发需要什么技术人员seo推广灰色词
  • 商贸公司寮步网站建设价钱bing搜索国内版
  • 网站横幅背景图片满十八岁可以申请abc认证吗
  • 爱网站找不到了seo关键词推广案例
  • 东风南方实业集团 深圳vi设计公司深圳关键词优化公司哪家好
  • 建设网站的拓扑图b站推广网站2024年不用下载
  • 天津网站优化公司电话seo每天一贴博客
  • 视频网站如何做谷歌seo排名优化
  • 注册万网后网站怎么赚钱的广州seo软件
  • 同城购物网站建设西安 做网站