Head First C ―頭とからだで覚えるCの基本
(オライリージャパン)
David Griffiths (著) Dawn Griffiths (著)
中田 秀基(監訳)(翻訳) 木下 哲也 (翻訳)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang (コンパイラ)
Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の9章(プロセス間通信)、自分で考えてみよう(p.435)を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
- C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、 谷口 功 (翻訳)、 オライリージャパン)
自分で考えてみよう(p.435)
コード(BBEdit, Emacs)
newshound2.c
#include <stdio.h>
#include <unistd.h>
#include "error.h"
int main(int argc, char *argv[])
{
char *phrase = argv[1];
char *vars[] = {"RSS_FEED=http://rss.cnn.com/rss/edition.rssl", NULL};
FILE *out_file = fopen("stories.txt", "w");
if (!out_file)
error("stories.txtを開けません");
pid_t pid = fork();
if (pid == -1) {
error("プロセスをフォークできません");
}
if (!pid) {
if (dup2(fileno(out_file), 1) == -1)
error("標準出力をリダイレクトできません");
if (execle("/usr/bin/python", "/usr/bin/python", "rssgossip.py", phrase,
NULL, vars) == -1)
error("スクリプトを実行できません");
}
return (0);
}
Makefile
all: newshound2 newshound2: newshound2.c error.o cc -g -o newshound2 newshound2.c error.o error.o: error.c error.h cc -c -o error.o error.c clean: rm -rf newshound2
入出力結果(Terminal)
$ make cc -c -o error.o error.c cc -g -o newshound2 newshound2.c error.o $ ./newshound2 japan $ cat stories.txt $ ./newshound2 u.s $ cat stories.txt Study: U.S. 'wary of futuristic tech' Russia's red line Pro-Russians thwart army Corfu's amazing starry sky Pope Francis continues to break tradition Did U.S. miss a huge al Qaeda meet? U.S.-Russia relations are icy ... Rise of the mega-museums How Louis Vuitton fortune was built $
0 コメント:
コメントを投稿