APUE 学习笔记(十) 高级I/O
1. Unix IPC(InterProcess Communication)
2. 管道
#include <stdio.h> FILE* popen(const char* cmdstring, const char* type);
int pclose(FILE* fp);
函数popen先执行fork,然后调用exec以执行cmdstring,并且返回一个标准I/O文件指针


#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <sys/wait.h>
#include <fcntl.h> /* pointer to array allocated at run-time */
static pid_t* childpid = NULL; /* from our open_max() */
static int maxfd; FILE* my_popen(const char* cmdstring, const char* type)
{
int pfd[];
pid_t pid; /* only allow type = "r" or "w" */
if ((type[] != 'r' && type[] != 'w') || type[] != ) {
errno = EINVAL;
return NULL;
}
childpid = (pid_t*)calloc(maxfd, sizeof(pid_t));
if (childpid == NULL) {
return NULL;
}
} if (pipe(pfd) < ) {
return NULL;
} if ((pid = fork()) < ) {
return NULL;
} else if (pid == ) { /* child */
if (*type == 'r') {
close(pfd[]);
if (pfd[] != STDOUT_FILENO) {
dup2(pfd[], STDOUT_FILENO);
close(pfd[]);
}
} else {
close(pfd[]);
if (pfd[] != STDIN_FILENO) {
dup2(pfd[], STDIN_FILENO);
close(pfd[]);
}
} /* close all fds in childpid[] */
for (int i = ; i < maxfd; ++i) {
if (childpid[i] > ) {
close(i);
}
} } /* parent continue */
FILE* fp;
if (*type == 'r') {
close(pfd[]);
if ((fp = fdopen(pfd[], type)) == NULL) {
return NULL;
}
} else {
close(pfd[]);
if ((fp = fdopen(pfd[], type)) == NULL) {
return NULL;
close(pfd[]);
if ((fp = fdopen(pfd[], type)) == NULL) {
return NULL;
}
} childpid[fileno(fp)] = pid;
return fp;
}
3. 消息队列
4. 信号量
5. 共享存储器
#include <sys/shm.h>
/* 获得共享存储标识符 */
int shmget(key_t key, size_t size, int flag); /* 对共享存储区执行多种操作 */
int shmctl(int shmid, int cmd, struct shmid_ds* buf); /* 进程将共享存储区连接到它的地址空间中 */
void* shmat(int shmid, const void* addr, int flag);
如果addr为0,则此段连接到内核选择的第一个可用地址上。一般将addr指定为0,以便由内核选择地址
打印各种不同类型的数据所存放的位置:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h> #define ARRAY_SIZE 40000
#define MALLOC_SIZE 100000
#define SHM_SIZE 100000
#define SHM_MODE 0600 /* user read/write */ char array[ARRAY_SIZE]; /* uninitialized data = bss */ int main(int argc, char* argv[])
{
int shmid;
char* ptr = NULL;
char* shmptr = NULL; fprintf(stdout, "array[] from %p to %p\n", array, array + ARRAY_SIZE);
fprintf(stdout, "stack around %p\n", &shmid);
ptr = (char*)malloc(MALLOC_SIZE);
if (ptr == NULL) {
fprintf(stderr, "malloc error\n");
} fprintf(stdout, "malloc from %p to %p\n", ptr, ptr + MALLOC_SIZE); shmid = shmget(IPC_PRIVATE, SHM_SIZE, SHM_MODE);
if (shmid < ) {
fprintf(stderr, "shmget error\n");
} shmptr = shmat(shmid, , );
if (shmptr == (void*)-) {
fprintf(stderr, "shmat error\n");
} fprintf(stdout, "shared memory from %p to %p\n", shmptr, shmptr + SHM_SIZE);
if (shmctl(shmid, IPC_RMID, ) < ) {
fprintf(stderr, "shmctl error\n");
}
free(ptr);
return ;
}
在基于Intel的Linux系统上运行此程序,其输出如下:


APUE 学习笔记(十) 高级I/O的更多相关文章
- APUE 学习笔记(九) 高级I/O
1. 非阻塞I/O 低速系统调用时可能会使进程永远阻塞的一类系统调用,包括以下调用: (1)某些文件类型你(网络socket套接字.终端设备.管道)暂无可使用数据,则读操作可能会使调用者永远阻塞 (2 ...
- python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例
python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...
- Hadoop学习笔记(7) ——高级编程
Hadoop学习笔记(7) ——高级编程 从前面的学习中,我们了解到了MapReduce整个过程需要经过以下几个步骤: 1.输入(input):将输入数据分成一个个split,并将split进一步拆成 ...
- Learning ROS for Robotics Programming Second Edition学习笔记(十) indigo Gazebo rviz slam navigation
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 moveit是书的最后一章,由于对机械臂完全不知,看不懂 ...
- python3.4学习笔记(十八) pycharm 安装使用、注册码、显示行号和字体大小等常用设置
python3.4学习笔记(十八) pycharm 安装使用.注册码.显示行号和字体大小等常用设置Download JetBrains Python IDE :: PyCharmhttp://www. ...
- python3.4学习笔记(十九) 同一台机器同时安装 python2.7 和 python3.4的解决方法
python3.4学习笔记(十九) 同一台机器同时安装 python2.7 和 python3.4的解决方法 同一台机器同时安装 python2.7 和 python3.4不会冲突.安装在不同目录,然 ...
- python3.4学习笔记(十六) windows下面安装easy_install和pip教程
python3.4学习笔记(十六) windows下面安装easy_install和pip教程 easy_install和pip都是用来下载安装Python一个公共资源库PyPI的相关资源包的 首先安 ...
- python3.4学习笔记(十五) 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
python3.4学习笔记(十五) 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) python print 不换行(在后面加上,end=''),prin ...
- python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL
python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL实战例子:使用pyspider匹配输出带.html结尾的URL:@config(a ...
随机推荐
- Java迭代器问题 有100个人围成一个圈从1开始报数,报到14的这个人就要退出,然后其他人重新开始,从1报数,到14退出问:最后剩下的是100人中的第几个人 用listIterator迭代元素,并对集合进行删除操作
package com.swift; import java.util.ArrayList; import java.util.List; import java.util.ListIterator; ...
- 哈希表(Hash Table)/散列表(Key-Value)
目录 1. 哈希表的基本思想 2. 哈希表的相关基本概念 1.概念: 2.哈希表和哈希函数的标准定义: 1)冲突: 2)安全避免冲突的条件: 3)冲突不可能完全避免 4)影响冲突的因素 3. 哈希表的 ...
- NOIP模拟赛 数列
Problem 2 数列(seq.cpp/c/pas) [题目描述] a[1]=a[2]=a[3]=1 a[x]=a[x-3]+a[x-1] (x>3) 求a数列的第n项对1000000007 ...
- python2和python3中filter函数
在python2和python3中filter是不同的,其中在python2中filter返回的是一个list,可以直接使用 >>> a = [1,2,3,4,5,6,7] > ...
- k8s调度的预选策略及优选函数
scheduler调度过程: Predicate(预选)-->Priority(优选)-->Select(选定)调度方式: 1.节点亲和性调度(NodeAffinity)使用n ...
- 1、python-初探
语言包括编译型语言和解释型语言编译型:全部翻译,再执行:c.c++解释型:边执行边翻译:python.php.java.c#.perl.ruby.javascript 一.系统位数32位系统内存的最大 ...
- Developing for nRF52810(转载)
Table of Contents Introduction Hardware emulation of nRF52810 Limitations Software emulation of nRF5 ...
- PAT Basic 1078
1078 字符串压缩与解压 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示.例如 ccccc 就用 5c 来表示.如果字符 ...
- Linux网络文件系统NFS详解
什么是文件系统,NFS文件系统又是什么? 简单的说,文件系统就是通过软件对磁盘上的数据进行组织和管理的一种机制,对其的一种封装或透视. 你女朋友拍了美美的暧昧照片,放一个文件夹里发送给了A服务器,当你 ...
- debian 7 stable 不能编译android源码
rebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.8-linaro/bin/arm-linux-androideabi-gcc: /lib/x86_ ...