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 (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の12章(スレッド)、ビールマグネット(p.509)を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
- C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、 谷口 功 (翻訳)、 オライリージャパン)
ビールマグネット(p.509)
コード(BBEdit, Emacs)
sample509.c
#include <stdio.h>
#include <pthread.h>
#include "drink_lots.h"
#include "error.h"
int main(int argc, char *argv[])
{
pthread_t threads[20];
int t;
printf("壁にはビールが%i本\n%i本のビール\n", beers, beers);
for (t = 0; t < 20; ++t)
pthread_create(&threads[t], NULL, drink_lots, NULL);
void *result;
for (t = 0; t < 20; ++t)
pthread_join(threads[t], &result);
/* 各スレッドがbeersに同時にアクセスできるで、0本になるとはかぎらない。 */
printf("現在、壁にはビールが%i本あります\n", beers);
return (0);
}
Makefile
all: sample509 sample509: sample509.c error.o drink_lots.o cc -g -o sample509 sample509.c error.o drink_lots.o drink_lots.o: drink_lots.c cc -c -o drink_lots.o drink_lots.c error.o: error.c cc -c -o error.o error.c clean: rm -rf sample509
入出力結果(Terminal)
$ make cc -c -o drink_lots.o drink_lots.c cc -g -o sample509 sample509.c error.o drink_lots.o $ ./sample509 壁にはビールが2000000本 2000000本のビール 現在、壁にはビールが1389616本あります $ ./sample509 壁にはビールが2000000本 2000000本のビール 現在、壁にはビールが1296461本あります $ ./sample509 壁にはビールが2000000本 2000000本のビール 現在、壁にはビールが1357809本あります $ ./sample509 壁にはビールが2000000本 2000000本のビール 現在、壁にはビールが1419295本あります $ ./sample509 壁にはビールが2000000本 2000000本のビール 現在、壁にはビールが1294705本あります $
0 コメント:
コメントを投稿