2014-01-01から1年間の記事一覧

APUE - 16.3.1 Byte Ordering

include pathのアレコレ

c

インクルードパスについてのアレコレをメモ。 PATHを調べる gcc -v で確認ができる。 $ gcc -v hello.c (略) #include "..." search starts here: #include <...> search starts here: /usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/includ…

APUE - 15.9 Shared Memory

普通にforkしちゃうと、変数は共有することができない。 が、shmget(2), shmat(2)を使えば、メモリを共有することができる。 shmat(2) #include <sys/types.h> #include <sys/shm.h> void *shmat(int shmid, const void *shmaddr, int shmflg); int shmdt(const void *shmaddr); shma</sys/shm.h></sys/types.h>…

semaphore

c

POSIXのセマフォとSystemVのセマフォと2種類がある。 後者はAPI仕様上、複数個を扱うことになるのでやや使いづらい。 http://www.ibm.com/developerworks/jp/linux/library/l-semaphore/ APUEで解説されてるのはSystem Vのセマフォなので、そちらを。 最もシ…

nodebrew + node.js

js

ついカッとなって、node.jsをインストールしてみた。 homebrewでソースからビルドすると時間掛かるらしく、最近だとnodebrewがいいとの話なので、その方向で。 homebrewのnodeは消しておく $ brew uninstall node nodebrewのセットアップ $ curl -L git.io/n…

FIFO

c

FIFOは名前付きパイプとも呼ばれる。pipe(2)によってつくられるパイプは"無名"で、親子関係など限られたプロセス間通信にしか使えないが、FIFOならばそのような制約はない。さらに、FIFOはファイルと同じように扱える。 カンタンな例 mkfifo(1)を使う。 shel…

pipeとdup

c

dup(2), dup2(2)

c

Advanced Programing in the UNIX Environmentを再開した。 本日はdupとdup2を。 詳解UNIXプログラミング作者: W.リチャードスティーヴンス,W.Richard Stevens,大木敦雄出版社/メーカー: ピアソンエデュケーション発売日: 2000/12メディア: 単行本購入: 8人 …

アルゴリズムとデータ構造

c

MIT Open Coursewareが終わったので、次は積ん読になっていたこれ。 プログラミングの宝箱 アルゴリズムとデータ構造 第2版作者: 紀平拓男,春日伸弥出版社/メーカー: ソフトバンククリエイティブ発売日: 2011/03/30メディア: 単行本購入: 15人 クリック: 255…

pipe(2)

c

PIPE(2) #include <unistd.h> int pipe(int pipefd[2]); pipe(2) はパイプを生成する。 パイプは、プロセス間通信に使用できる単方向のデータチャネルである。 配列 pipefd は、パイプの両端を参照する二つのファイルディスクリプタを 返すのに使用される。 pipefd[0] </unistd.h>…

malloc(2), calloc(2), memset(2)

c

MAN void *malloc(size_t size); void *calloc(size_t nmemb, size_t size); malloc() 関数は size バイトを割り当て、 割り当てられたメモリに対する ポインタを返す。メモリの内容は初期化されない。 size が 0 の場合、 malloc() は NULL または free() …

select(2), poll(2)

c

引き続き、MIT Open Courseware。 select(2) int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); int pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespe…

socket(2), bind(2), listen(2), accept(2)

c

本日は、MIT Open CoursewareのLecture14で、socket(2), bind(2), listen(2), accept(2)など。 アレコレ参考にしながら、最もシンプルなサーバとクライアントを書いてみた。 サーバ クライアント socket(2) #include <sys/socket.h> int socket(int domain, int type, int </sys/socket.h>…

thread / mutex

c

引き続き MIT Open CoursewareのPractical Programming in C に取り組み中。本日はLecture13のrace conditionについて。 #include <stdlib.h> #include <stdio.h> #include <pthread.h> unsigned int cnt = 0; void *count(void *arg) { int i; for ( i = 0; i < 100000; i++ ) { cnt++; } </pthread.h></stdio.h></stdlib.h>…

スレッドで共有されるリソース

Operating System Conceptsを引っ張りだして、再整理。 出典: Operating System Concepts - Fig4.1 メモリのコードセクションとデータセクション が共有される。 また、ファイルディスクリプタとシグナル が共有される。

mmap(2), msync(2)

c

mmapは「ファイルやデバイスをメモリにマップ」するシステムコール。 msyncは「ファイルをマップしたメモリと同期させる」システムコール。 #include <stdlib.h> #include <fcntl.h> #include <stdio.h> #include <sys/mman.h> int main(int argc, char* argv[]) { int fd; char *cp; fd = open("/tmp</sys/mman.h></stdio.h></fcntl.h></stdlib.h>…

charとint

c

#include <stdlib.h> #include <stdio.h> int main(int argc, char* argv[]) { FILE *fp; char c[5] = { '1', '2', '3', '4', '5' }; if ( (fp = fopen("/tmp/foo", "w")) == NULL ) { exit(EXIT_FAILURE); } fwrite(c, sizeof(char), 5, fp); fclose(fp); exit(EXIT_SUCCESS); </stdio.h></stdlib.h>…

ダイナミックリンクと共有オブジェクトファイル(.so)

c

実行までの流れ プログラムをコンパイルしてオブジェクトファイルを生成 オブジェクトファイルをリンカで結びつけて実行ファイルを生成 共有オブジェクトファイルは、実行時に動的にリンクされる。 やってみる 足し算をするだけのライブラリをつくってみる。…

ttyとプロセス管理

ttyやら端末切断時のプロセス管理について曖昧だったので、調べてみた。 ttyとは... ttyとは、標準入出力となっている端末デバイス(制御端末、controlling terminal)の名前を表示するUnix系のコマンドである。元来ttyとはteletypewriter(テレタイプライター…

segmentation fault

c

segmentation fault(セグメンテーション違反)は、許可されていないメモリ領域へのアクセス あるいは 操作によって発生するエラーである。 例えば、NULLポインタの指すアドレスに値を書き込もうとした場合に発生する。 #include <stdlib.h> #include <stdio.h> int main(int ar</stdio.h></stdlib.h>…