Head First C ―頭とからだで覚えるCの基本
(オライリージャパン)
David Griffiths (著) Dawn Griffiths (著)
中田 秀基(監訳)(翻訳) 木下 哲也 (翻訳)
開発環境
- OS X Yosemite - Apple, Ubuntu (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang/LLVM (コンパイラ, Xcode - Apple)
Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の10章(プロセス間通信: お話は楽しい)、自分で考えてみよう(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.rss", NULL}; FILE *f = fopen("stories.txt", "w"); if (!f) error("stories.txtを開けません"); pid_t pid = fork(); if (pid == -1) error("プロセスをフォークできません"); if (!pid) { if (dup2(fileno(f), 1) == -1) error("標準出力をリダイレクトできません"); if (execle("/usr/bin/python", "/usr/bin/python", "rssgossip.py", phrase, NULL, vars) == -1) error("スクリプトを実行できません"); } return 0; }
Makefile
P=newshound2 CC=cc CFLAGS=-g -Wall # -O3 SRC=newshound2.c OBJ=newshound2.o error.o LDLIBS= $(P): $(OBJ) $(CC) $(CFLAGS) $(LDLIBS) $(OBJ) -o $@ newshound2.o: $(CC) $(CFLAGS) -c newshound2.c -o $@
入出力結果(Terminal)
$ make cc -g -Wall -c newshound2.c -o newshound2.o cc -g -Wall newshound2.o error.o -o newshound2 $ ./newshound2 U.S. $ cat stories.txt Sources: U.S reviews ISIS plan Assad in U.S. crosshairs Injured test pilot questioned U.S. sailors attacked in Turkey Russia 'advancing into Ukraine' Russian troops, tanks near border Louis Vuitton's Paris landmark 'Walking Dead' on the U.S. zombie trail Sex museums: A global guide Michelin's 'undisputed leader' in cuisine $
0 コメント:
コメントを投稿