ipc

Running bash using posix instead of fork/execv

北城余情 提交于 2019-12-10 11:09:24
问题 I have a CLI, one of the commands is entering into Linux bash shell. This is the code which does it using fork & execv: if ((pid = fork()) < 0) { syslog_debug(LOG_ERR, "Could not fork"); } if (pid == 0) { /* In child, open the child's side of the tty. */ int i; for(i = 0; i <= maxfd; i++) { close(i); } /* make new process group */ setsid(); if ((fd[0] = open(tty_name, O_RDWR /*| O_NOCTTY*/)) < 0) { syslog_debug(LOG_ERR, "Could not open tty"); exit(1); } fd[1] = dup(0); fd[2] = dup(0); /* exec

Two children of same parent are not communicating using pipe if parent do not call wait()

。_饼干妹妹 提交于 2019-12-10 11:05:51
问题 Please see the code below: #include<stdio.h> main(){ int pid, fds[2], pid1; char buf[200]; pipe(fds); pid = fork(); if(pid==0) { close(fds[0]); scanf("%s", &buf); write(fds[1], buf, sizeof(buf)+1); } else { pid1 = fork(); if(pid1==0) { close(fds[1]); read(fds[0], buf, sizeof(buf)+1); printf("%s\n", buf); } else { Line1: wait(); } } } If I do not comment out Line1, it is working fine. Please see below: hduser@pc4:~/codes/c/os$ ./a.out hello //*Entry from keyboard* hello //Output hduser@pc4:~

how to attach to an existing shared memory segment

依然范特西╮ 提交于 2019-12-10 10:07:28
问题 I am having trouble with shared memory. I have one process that creates and writes to a shared memory segment just fine. But I cannot get a second process to attach that same existing segment. My second process can create a new shared segment if I use IPC_CREATE flag but I need to attach to the existing shared segment that was created by the 1st process. This is my code in the 2nd process: int nSharedMemoryID = 10; key_t tKey = ftok("/dev/null", nSharedMemoryID); if (tKey == -1) { std::cerr <

Named pipes vs. UDP for IPC on Windows

橙三吉。 提交于 2019-12-10 07:51:08
问题 Why Named Pipes are preferable for IPC (Inter Process Comunication) on local Windows machine over UDP? Or UDP sometimes might be somewhere better? 回答1: UDP packets even on localhost can be lost. Also, as UDP is datagram-based and has no guaranteed delivery, it's hard to transfer larger data blocks. Finally, UDP on localhost is sometimes blocked by browsers. In general, UDP is usually not even considered for single-computer IPC. On Windows I recommend memory-mapped files + synchronization

C# bi-directional IPC over stdin and stdout

巧了我就是萌 提交于 2019-12-10 04:13:41
问题 How can I connect two C# processes so they can communicate with each other over stdin and stdout? Like this: Process A --> stdout A --> stdin B ---> Process B Process A <-- stdin A <-- stdout B <--- Process B 回答1: using System; using System.Diagnostics; class Program { static void Main(string[] args) { string name; if (args.Length > 0 && args[0] == "slave") { name = "slave"; } else { name = "master"; var info = new ProcessStartInfo(); info.FileName = "BidirConsole.exe"; info.Arguments =

Named pipes server read timeout

拜拜、爱过 提交于 2019-12-10 04:01:31
问题 When using C# NamedPipeServerStream, in case a client doesn't send any message-end-pattern (like \r\n when server reads with ReadLine()) NamedPipeServerStream Read methods will wait forever and no Abort() or Interupt() methods will work on that thread. Since: 1) Stream.ReadTimeout not supported for NamedPipeServerStream 2) Abort() or Interupt() doesn't work on thread 3) NamedPipeServerStream.Disconnect() nether work It is unclear, how to setup timeout on NamedPipeServerStream read operations?

消息队列函数(msgget、msgctl、msgsnd、msgrcv)小记学习

霸气de小男生 提交于 2019-12-10 02:00:05
一、什么是消息队列 消息队列提供了一种从一个进程向另一个进程发送一个数据块的方法。 每个数据块都被认为含有一个类型,接收进程可以独立地接收含有不同类型的数据结构。我们可以通过发送消息来避免命名管道的同步和阻塞问题。但是消息队列与命名管道一样,每个数据块都有一个最大长度的限制。 Linux用宏MSGMAX和MSGMNB来限制一条消息的最大长度和一个队列的最大长度。 二、在Linux中使用消息队列 Linux提供了一系列消息队列的函数接口来让我们方便地使用它来实现进程间的通信。消息队列函数由msgget、msgctl、msgsnd、msgrcv四个函数组成。下面的表格列出了这四个函数的函数原型及其具体说明。 1. msgget函数原型 msgget(得到消息队列标识符或创建一个消息队列对象) 所需头文件 #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> 函数说明 得到消息队列标识符或创建一个消息队列对象并返回消息队列标识符 函数原型 int msgget(key_t key, int msgflg) 函数传入值 key 0(IPC_PRIVATE):会建立新的消息队列 大于0的32位整数:视参数msgflg来确定操作。通常要求此值来源于ftok返回的IPC键值 msgflg 0:取消息队列标识符

PHP进程及进程间通信

人走茶凉 提交于 2019-12-09 22:31:53
一、引言 进程是一个具有独立功能的程序关于某个数据集合的一次运行活动。换句话说就是,在系统调度多个cpu的时候,一个程序的基本单元。进程对于大多数的语言都不是一个陌生的概念,作为"世界上最好的语言PHP"当然也例外。 二、环境 php中的进程是以扩展的形式来完成。通过这些扩展,我们能够很轻松的完成进程的一系列动作。 pcntl扩展:主要的进程扩展,完成进程创建于等待操作。 posix扩展:完成posix兼容机通用api,如获取进程id,杀死进程等。 sysvmsg扩展:实现system v方式的进程间通信之消息队列。 sysvsem扩展:实现system v方式的信号量。 sysvshm扩展:实现system v方式的共享内存。 sockets扩展:实现socket通信。 这些扩展只能在linux/mac中使用,window下是不支持。最后建议php版本为5.5+。 相关代码: 进程相关代码 三、简单的例子 一个简单的PHP多进程例子,该例子中,一个子进程,一个父进程。子进程输出5次,退出程序。 $parentPid = posix_getpid(); echo "parent progress pid:{$parentPid}\n"; $childList = array(); $pid = pcntl_fork(); if ( $pid == -1) { // 创建失败

第15章 进程间通行 15.6 XSI IPC 15.7 消息队列

北战南征 提交于 2019-12-09 20:35:47
15.6 XSI IPC (1)3种称作XSI IPC的IPC是: 1)消息队列 2)信号量 3)共享存储器 (2)标识符和键 1)标识符:是一个非负整数,用于引用IPC结构。是IPC对象的内部名。 2)键:IPC对象的外部名。可使多个合作进程能够在同一IPC对象上汇聚。 (3)IPC_PRIVATE键: 用于创建一个新的IPC结构。不能指定此键来引用一个现有的IPC结构。 (4)ftok函数: 由一个路径名和项目ID产生一个键。 (5)ipc_perm结构体 规定了ipc结构的权限和所有者。 (6)结构限制: XSI IPC结构都有内置限制,可通过重新配置内核来改变。 1)sysctl命令:观察、修改内核配置参数。 2)ipcs -l:显示ipc相关限制。 (7)IPC结构和管道、FIFO的区别: IPC结构在系统范围内起作用,且没有引用计数。 (8)IPC结构在文件系统中没有名字;IPC不使用文件描述符。 15.7 消息队列 (1)新的应用程序中不要使用消息队列。它们有缺点。(15.6.4) (2)客户进程和服务器进程之间的双向数据流,可以使用消息队列或全双工管道。 15.8 信号量 (1)多个进程间共享一个资源,可以使用信号量、记录锁和互斥量中的一种来协调。 (2)共享存储中的互斥量速度最快,但作者依然喜欢使用记录锁的两个原因: 1)<459> 2) <459> 15.9

Android 的 Binder 机制概念介绍

房东的猫 提交于 2019-12-09 19:40:34
结合了以下两篇文章的介绍,对 Android 的 Binder 机制概念开始有了一定的理解。分享给大家。 -------------------------------------分割线--------------------------------- 摘要 Binder是android 中一个很重要且很复杂的概念,它在系统的整体运作中发挥着极其重要的作用,不过本文并不打算从深层次分析Binder机制,有两点原因:1是目前网上已经 有2篇很好的文章了,2是对Binder机制进行深入底层乃至驱动的分析这一过程相当困难且相当耗时,因此并不适合重复造轮子。本文的角度是对 Android的Binder机制从整体和概念上进行分析,能够让大家很快明白到底什么是Binder,Binder是干什么的,Binder和应用开发 的关系是什么,总之,这篇文章还是很值得去看一看的。 什么是Binder 1. 直观来说,Binder是Android中的一个类,它继承了IBinder接口 2. 从IPC角度来说,Binder是Android中的一种跨进程通信方式,Binder还可以理解为一种虚拟的物理设备,它的设备驱动是/dev/binder,该通信方式在linux中没有 3. 从Android Framework角度来说,Binder是ServiceManager连接各种Manager