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

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>…