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 ...
随机推荐
- 使用notepad++远程编辑Linux文档
上一篇中,我写了如何使用使用ftp服务器实现很方便的通信,这一篇我分享一个使用notepad++的一个NPPFTP插件远程编辑Linux中的文档的小技巧. 首先要确保你的Linux的ftp服务已经打开 ...
- Dojo的on函数(以前的dojo.connect)
同jQuery的on函数: require(["esri/map", "dojo/on"], function(Map, on) { // ... on(my ...
- 多线程编程之pthread线程深入理解
不同的平台和操作系统上 进程和线程的实现机制不完全一致 但是一般来说线程栈都是独立的 只要得到地址就可以相互访问 Pthread是 POSIX threads 的简称,是POSIX的线程 ...
- webgis技术在智慧城市综合治理(9+X)网格化社会管理平台(综治平台)的应用研究
综治中心9+X网格化社会管理平台 为落实中央关于加强创新社会治理的要求,适应国家治理体系和治理能力现代化要求,以基层党组织为核心,以整合资源.理顺关系.健全机制.发挥作用为目标,规范街道.社区综治中心 ...
- 了解swagger
https://blog.csdn.net/i6448038/article/details/77622977 随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染.先后端分离 ...
- strace用法
strace -- trace system calls and signals strace是Linux环境下的一款程序调试工具,用来监察一个应用程序所使用的系统调用及它所接收的系统信 ...
- POJ:1086-Parencodings
Parencodings Time Limit: 1000MS Memory Limit: 10000K Description Let S = s1 s2-s2n be a well-formed ...
- debian7不能apt安装emacs24
维护者在主页上 http://emacs.naquadah.org/ 提到: These packages are not maintained anymore I don't use these p ...
- Mysql登陆、退出、更改环境编码
登录: mysql -h[数据库地址] -u[username] -p[password] -P[端口] //大写P表示端口,小写p表示密码 例如:mysql -hlocalhost -uroot ...
- UVa 1452 递推 Jump
约瑟夫变形,先计算出3个数时,最后三个数字的编号. 然后以这三个数为起点,就可以递推出n个数对应的最后三个数字的编号. 递推公式都是一样的. #include <iostream> #in ...