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 ...
随机推荐
- linux必会命令-查询-tail
先说一个tail使用的例子: tail -n 20 filename 说明:显示filename最后20行. Linux下tail命令的使用方法.linux tail命令用途是依照要求将指定的文件的最 ...
- Visual Studio Professional 2015 简体中文专业版 序列号
Visual Studio Professional 2015 简体中文专业版 专业版激活密钥:HMGNV-WCYXV-X7G9W-YCX63-B98R2 Visual Studio Enterpri ...
- POP简单动画简单使用 (入门级别)
动画可以让APP“更友好”的与用户交互,苹果提供很多的好看的动画供开发者使用,不过简单的平移.旋转.缩放.......使用起来很简单,但是想要进行一些比较复杂的动画效果,使用起来就比较难以实现,俗话说 ...
- sql 经典加强巩固练习题
由于本人需要加强巩固一下数据库知识,就搜罗了一些题目来练习,感觉不错,故分享一下资源难度层度依次上升这50道里面自认为应该没有太多错误,而且尽可能使用了最简单或是最直接的查询,有多种不相上下解法的题目 ...
- Struts2和SpringMVC简单配置以及区别总结
Struts2: struts 2 是一个基于MVC(mode-view-con)设计模式的Web应用框架,是由Struts1和WebWork两个经典框架发展而来的. 工作流程: 1客户端浏览器发出H ...
- october安装过程
下载代码 composer create-project october/october myoctober 准备好数据库, create database october; 配置环境于安装 php ...
- VNC远程登录端使用经验之一
1.vnc/xmanager都是经常用的远程登录软件.vnc有个缺点就是他的进程不会自动退出比如如果开了PID1再去开PID2...PIDn.那么前面的PIDn-1个进程就会一直运行如果不手动kill ...
- Python入门基本语法
Python入门 以下主要讲述Python的一些基础语法,包含行的缩进在python中的重要意义,python中常见的保留字和引号的使用,如何实现单行注释和多行注释. print("he ...
- hdu 6333
Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...
- Python虚拟机之异常控制流(四)
Python虚拟机中的异常控制流 先前,我们分别介绍了Python虚拟机之if控制流(一).Python虚拟机之for循环控制流(二)和Python虚拟机之while循环控制结构(三).这一章,我们来 ...