C

Pthread の排他処理のサンプル

自分用。 ソース (pthread_mutex.c) #include <stdio.h> #include <unistd.h> #include <pthread.h> #include <stdlib.h> #define MAX_THREADS 3 void start_threads(void); void thread_main(void *args); void synchronized_printf(const char* s); static pthread_mutex_t mut = PTHREAD_MUTEX_I</stdlib.h></pthread.h></unistd.h></stdio.h>…

C言語で、Gratuitous ARPパケットを送信するサンプル

自分用メモ。 #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <netdb.h> #include <string.h> #include <unistd.h> #include <ctype.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <sys/param.h> #include <sys/sysctl.h> #include <arpa/inet.h> #include </arpa/inet.h></sys/sysctl.h></sys/param.h></sys/ioctl.h></sys/socket.h></sys/types.h></ctype.h></unistd.h></string.h></netdb.h></stdlib.h></stdio.h></errno.h>

C言語でNICの情報を取得するサンプル

eth0 の IPアドレス、MACアドレス、ifIndexを取得するサンプル。自分用メモ。 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/ioctl.h> #include <net/if.h> #include <netinet/in.h> #include <arpa/inet.h> void get_ifinfo(char *devname, struct ifreq *ifreq, int flavor) { int iofd; if ((iofd = socket(A</arpa/inet.h></netinet/in.h></net/if.h></sys/ioctl.h></string.h></stdlib.h></stdio.h>…

3つのプログラミング言語での syslog の使い方メモ

C、Perl、Python でのsyslogのサンプル。 C #include <stdlib.h> #include <syslog.h> int main(int argc, char** argv){ openlog(argv[0], LOG_PID|LOG_PERROR, LOG_LOCAL0); syslog(LOG_INFO, "foo: %d", 123); closelog(); exit(EXIT_SUCCESS); } Perl #!/usr/bin/perl use st</syslog.h></stdlib.h>…

3つのプログラミング言語での getopt の使い方メモ

C、Perl、Python でのgetoptのサンプル。 とりあえず、help, version, user, password の4つのオプションを指定できるようにした。 使い方 % my_getopt --help % my_getopt -h % my_getopt --version % my_getopt -v % my_getopt --user=foo --password=abc …

C言語でprefork型のデーモンを書く(4): init スクリプト

prefork して、シャットダウン可能なデーモン本体ができたので、次はinit スクリプトを用意する。 適当なサンプルは、/etc/init.d/ 以下にたくさんあるのでそれを参考に書いてみた。 デーモン起動時に /var/run/my_prefork_daemon.pid が自動作成されるのを…

C言語でprefork型のデーモンを書く(3): デーモン化

prefork して、シグナルで綺麗に終了できるようになったので次はデーモン化する。デーモンをkill しやすいように プロセスIDをファイルに書いておくwrite_pid()関数と、デーモン化関数daemonize()を追加している。 my_prefork_daemon.c デーモン化の処理が入…

C言語でprefork型のデーモンを書く(2): 非デーモン prefork シグナルハンドラ付き

1つの親プロセスとたくさんの子プロセスという構成。親プロセスに SIGTERM を送ると、すべての子プロセスをきれいに終了させた後で終了するようにシグナルハンドラを追加したサンプル。 my_prefork_signal.c シグナルハンドラ付きのソース #include <stdio.h> #includ</stdio.h>…

C言語でprefork型のデーモンを書く(1): 非デーモン prefork サンプル

Cで書かれた prefork デーモン(daemon)のちょうど良いサンプルが見つからなかったので自分で書く。 ちょうど良いお手本がないので PerlのソースをCに移植した。 プラットフォームはCentOS5.2。 my_prefork.c ただの prefork のサンプル(デーモン化はしてい…

Cでハッシュテーブルを使うときのメモ

C

Cでハッシュテーブルを使いたくなったので、調べてみたら APR(Apache Portability Runtime) のハッシュテーブルのパフォーマンスが良いらしい。名前的に移植性も良さそうな気がする。 簡単な使い方を兼ねたサンプルをメモがわりに残す。プラットフォームはCe…

何もしないOutputfilterのサンプル

http://asp.mi.hama-med.ac.jp/web/ のソースをほとんどそのままコピー。これを元に改造していく。いつものように、 # apxs -g -n myoutputfilter mod_myoutputfilter.conf LoadModule myoutputfilter_module modules/mod_myoutputfilter.so SetOutputFilter…

GDBのまとめ

かなり自分用 起動・終了など コマンド 備考 起動 gdb プログラムファイル coreファイル付きで 起動 gdb プログラムファイル core 実行中のプロセスをデバッグする gdb プログラムファイル プロセスID 実行中のプロセスをデバッグする attach プロセスID 実…

POSTされたデータをパースするApacheモジュール

CentOS5.1 上の Apache2.2のサンプル。libapreq2 を使用。 libapreq2 のインストールには、あらかじめEPELレポジトリの追加が必要。 # rpm -ivh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-1.noarch.rpm # yum install libapreq2-de…

内部リダイレクトするApacheモジュール

CentOS5.1 上の Apache2.2のサンプル。 #include "httpd.h" #include "http_config.h" #include "http_protocol.h" #include "ap_config.h" static int myinternalredirect_handler(request_rec *r) { ap_internal_redirect("/hello", r); return OK; } stat…

外部リダイレクトするApacheモジュール

CentOS5.1 上の Apache2.2のサンプル。 #include "httpd.h" #include "http_config.h" #include "http_protocol.h" #include "apr_tables.h" #include "ap_config.h" /* The sample content handler */ static int myredirect_handler(request_rec *r) { apr…

QUERY_STRING をパースするApacheモジュール

CentOS5.1 上の Apache2.2のサンプル。 mod_mygetparse の作成 # apxs -g -n mygetparse # cd mygetparse # make # make install/etc/httpd/conf.d/mod_mygetparse.conf を以下のように作成 LoadModule mygetparse_module modules/mod_mygetparse.so <Location /mygetparse> SetHan</location>…

Apacheモジュール 1.3から2.2への移植: mod_reqinfo

Apacheモジュール プログラミングガイド の中のサンプルプログラムをApache1.3からApache2.2に書き換えたのでそのときのメモ。 #include "httpd.h" #include "http_config.h" #include "http_protocol.h" #include "http_core.h" #include "ap_config.h" /* …

CentOS5.1 64ビット版の整数のビット長

テストコード #include <stdio.h> int main(){ printf("int is %d bit.\n", sizeof(int)*8 ); printf("long is %d bit.\n", sizeof(long)*8 ); printf("long long is %d bit.\n", sizeof(long long)*8 ); }結果 int is 32 bit. long is 64 bit. long long is 64 bit.l</stdio.h>…

Semaphore (セマフォ) のサンプル

#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED) /* union semun is defined by including <sys/sem.h> */ #else /* according to X/OPEN we have to define it ourselves…</sys/sem.h></sys/sem.h></sys/ipc.h></sys/types.h></stdio.h></stdlib.h></unistd.h>

fcntl を使ったファイルロック

fcntl を使ったファイルロックであれば、NFS上のファイルでもロックできる(らしい。) 以下はテストコード。 #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #include <sys/stat.h> int main(int args, char** argv ){ int file_desc; struct flock region; file_desc = open</sys/stat.h></fcntl.h></stdio.h></stdlib.h></unistd.h>…

mixiの「C/C++の課題丸投げ」コミュニティから「最大公約数の関数」の問題

C

最大公約数に関する次の関係を使って、2つの整数aとbを入力して、それらの最大公約数を求める整数型再帰関数GCD(a,b)を再帰関数を用いて作ってください。 GCD(a,b)=a (a=b) =GCD(a-b,b) (a>b) =GCD(a,b-a) (a<b) LinuxでC言語です。できれば今週中にお願いします。回答例 #include <stdio.h> int gcd(int a, int b){ if(a</b)>…

コンパイルした実行ファイルやオブジェクトファイルに関数が含まれているかどうかをチェックする方法

C

ただし -g オプション付きでコンパイルしていないとだめかも。 /usr/bin/objdump --all-headers ファイル | grep 関数名など

struct などのオブジェクト型でできたvectorのソート方法

C

std::sort( v.begin(), v.end() ) ではなく、std::sort( v.begin(), v.end(), func ) を使う。 #include <stdio.h> #include <vector> #include <iostream> using namespace std; struct DOMAINS { char szDomain[512]; }; bool func(const DOMAINS& x, const DOMAINS& y){ return ( str</iostream></vector></stdio.h>…

C++ のクラス変数の使い方のメモ

C

#include <stdio.h> using namespace std; class Test { public: static int a; //このへんはJavaと同じ。 }; int Test::a; //ここで定義しないといけないところがJava出身だと分かり難い。 int main(){ Test::a = 100; //Javaと同じでクラス変数はインスタンス化が不</stdio.h>…

C言語の練習

Cはもうほとんど忘れてしまっているので、復習をかねて昔読んだアルゴリズムの本の問題をやってみようと思った。 bubble sort #include <stdio.h> void swap( int *a, int *b); void bubbleSort( int *a, int length ); void bubbleSort( int *a, int length ) { for(</stdio.h>…

MD5を生成するサンプル

openssl付属のライブラリを使ったサンプル。そのままだとバイナリのMD5が取れるので16進に変換するサンプル。 //注意 コンパイル時に -lssl を付けること。 #include <openssl/md5.h> void to_hex( unsigned char *md, int length, unsigned char *md_hex ) { static char t</openssl/md5.h>…

逆アセンブル

某会社では私が来るまでSubversion、CVSなどのソースコード管理をしていなかった。ファイルサーバー上のフォルダなどに日付を付けて管理という旧石器時代のような管理方法だった。現在は私がSubversionサーバーをたて、各自のマシンにローカルなテスト環境、…

日付、時間の計算

C

すぐに忘れるのでメモ。 1900年1月1日から任意の日付の秒数の取得 例: 2007/9/26の場合 struct tm tm_begin; tm_begin.tm_year = 2007 - 1900; tm_begin.tm_mon = 9 - 1; tm_begin.tm_mday = 26; time_t t20070926 = mktime( &tm_begin ); 1900年1月1日から…

文字列コピー

C

前任者が、 memcpy( strHtmlFileName, pstrTemp, MAX_PATH );このようなことをしていたので時々アプリケーションが落ちていたのを発見。 以下のように修正した。 strncpy( strHtmlFileName, pstrTemp, sizeof(strHtmlFileName) - 1 );

JSONのライブラリのメモ

C

今使っているCで書かれたWEBアプリのフレームワークには、セッション変数を扱う機能が無いので追加することにした。その際にセッション変数を保持するファイル形式としてJSONが楽そうなのでJSONを使うことにした。その時のメモ。 よく使う関数プロトタイプ一…