Head First C ―頭とからだで覚えるCの基本
(オライリージャパン)
David Griffiths (著) Dawn Griffiths (著)
中田 秀基(監訳)(翻訳) 木下 哲也 (翻訳)
開発環境
- OS X Yosemite - Apple (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- LLVM/Clang (コンパイラ, Xcode - Apple)
Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の10章(プロセス間通信: お話は楽しい)、エクササイズ(p.447)を解いてみる。
その他参考書籍
- 21st Century C: C Tips from the New School
- プログラミング言語C 第2版 ANSI規格準拠(B.W. カーニハンD.M. リッチー(著)、石田 晴久(翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
- C実践プログラミング 第3版(Steve Oualline(著)、望月 康司 (監訳)(翻訳)、谷口 功(翻訳)、 オライリージャパン)
エクササイズ(p.447)
コード(BBEdit, Emacs)
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stopif.h>
#include <sys/wait.h>
int main(int argc, char **argv) {
char *vars[] = {"RSS_FEED=http://rss.cnn.com/rss/edition.rss", NULL};
int fd[2];
Stopif(pipe(fd) == -1, exit(1), "パイプを作成できません: %s", strerror(errno));
pid_t pid = fork();
Stopif(pid == -1, exit(1), "プロセスをフォークできません: %s",
strerror(errno));
if (!pid) {
close(fd[0]);
dup2(fd[1], 1);
Stopif(execle("/usr/bin/python", "/usr/bin/python", "rssgossip.py", "-u",
argv[1], NULL, vars) == -1,
exit(1), "スクリプトを実行できません: %s", strerror(errno));
}
dup2(fd[0], 0);
close(fd[1]);
char line[255];
while (fgets(line, 255, stdin))
if (line[0] == '\t') printf("%s", line + 1);
}
入出力結果(Terminal)
$ make news_opener && ./news_opener US clang ... http://edition.cnn.com/2015/02/25/us/new-york-terror-plot/index.html?eref=edition http://edition.cnn.com/2015/02/25/us/bikram-yoga-sex-assault-lawsuits/index.html?eref=edition http://edition.cnn.com/2015/02/25/world/amanpour-norway-ine-eriksen-soreide/index.html?eref=edition http://edition.cnn.com/video/data/2.0/video/world/2015/02/23/pkg-mclaughlin-russia-anti-american-sentiment-growing.cnn.html?eref=edition http://edition.cnn.com/video/data/2.0/video/world/2015/02/24/lkl-foster-octopus-crab-battle.cnn.html?eref=edition $
0 コメント:
コメントを投稿