Luv Connection / Tei Towa

自分のitunes掘ってたらみつけた。

特にMAWのremixが好きだけど全部よいですな。

Unix/Linuxプログラミング理論と実践 7章 (インターバルタイマ)

interval timerを使うと定期的にシグナルを送れる。

ProductName Unix/Linuxプログラミング理論と実践
Bruce Molay
アスキー・メディアワークス / ¥ 6,090 ()
在庫あり。

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <signal.h>

int main()
{
  void countdown(int);

  signal(SIGALRM, countdown);
  if ( set_ticker(50) == -1)
    perror("set_ticker");
  else
    while( 1 )
      pause();
  return 0;
}

void countdown(int signum)
{
  static int num = 10;
  printf("%d ..", num--);
  fflush(stdout);
  if ( num < 0 ){
    printf("DONE!\n");
    exit(0);
  }
}

int set_ticker( int n_msecs )
{
  struct itimerval new_timeset;
  long n_sec, n_usecs;

  n_sec = n_msecs / 1000;
  n_usecs = ( n_msecs % 1000 ) * 1000L;

  new_timeset.it_interval.tv_sec = n_sec;
  new_timeset.it_interval.tv_usec = n_usecs;

  new_timeset.it_value.tv_sec = n_sec;
  new_timeset.it_value.tv_usec = n_usecs;

  return setitimer(ITIMER_REAL, &new_timeset, NULL);
}

sleepは秒単位だけどusleep使えばmicrosecondで一時停止できる

Unix/Linuxプログラミング理論と実践 6章 (端末制御)

端末に関して、カノニカルモードとかノンブロッキングとか。あとシグナル処理。

ProductName Unix/Linuxプログラミング理論と実践
Bruce Molay
アスキー・メディアワークス / ¥ 6,090 ()
在庫あり。

端末を非カノニカル(cr)モードにするには

  • ICANONビットをオフにする
  • 制御文字バッファのVMIN要素に1をセット

ノンブロッキングモードにするには

  • O_NDELAYビットをONにする

Unix/Linuxプログラミング理論と実践 5章 (stty)

端末ドライバのプログラミング。

writeなんてコマンドがあるのか。

ProductName Unix/Linuxプログラミング理論と実践
Bruce Molay
アスキー・メディアワークス / ¥ 6,090 ()
在庫あり。

  • flagset |= MASK (bitをセット)
  • flagset &= ~MASK (bitをクリア)

ioctlシステムコールにはほとんど触れてない。

「幸福の方程式」を読んだ

昨日は食中りのような、げんなりするような鈍痛でやる気があまり出なかったので本でも読んだ。

フローの幸福からストックの幸福へ。

ProductName 幸福の方程式 (ディスカヴァー携書)
山田 昌弘,電通チームハピネス
ディスカヴァー・トゥエンティワン / ¥ 1,050 ()
在庫あり。

幸福のペンタゴンモデル

  • 時間密度
  • 手応え実感
  • 自尊心
  • 承認
  • 裁量の自由

半農半Xという生き方は読んだ。あと、ブータンのGNHの話は伊藤洋一のビジネストレンドの第206回「GNHという考え方」が面白かった。

今日の畑(091003)

メモ

  • ほうれん草と小松菜の種を撒いた。
  • 少し、草取りをしないといけない
  • ジャガイモがちょっと弱っていた
  • オクラがでかくなってた。

Unix/Linuxプログラミング理論と実践 4章 (pwdを書く)

pwdコマンドはinodeを親ディレクトリに辿っていく。ルートディレクトリは. と..が同じinodeを指す。

macbookでls -ia。

$ ls -ia /
      2 ./                                               31452 Users/
      2 ../                                              22730 Volumes/

ので.と..のinodeをチェックしながら再帰的に辿るという実装。

void printpathto( ino_t this_inode )
{
  ino_t my_inode;
  char  its_name[BUFSIZ];

  if ( get_inode( ".." ) != this_inode )
    {
      chdir( ".." );

      inum_to_name(this_inode, its_name, BUFSIZ);

      my_inode = get_inode( "." );
      printpathto( my_inode );
      printf("/%s",its_name );
    }
}

ProductName Unix/Linuxプログラミング理論と実践
Bruce Molay
アスキー・メディアワークス / ¥ 6,090 ()
在庫あり。

ダブル間接ブロックとトリプル間接ブロック

Unix/Linuxプログラミング理論と実践 3章 (lsを書く)

lsを書く。

ProductName Unix/Linuxプログラミング理論と実践
Bruce Molay
アスキー・メディアワークス / ¥ 6,090 ()
在庫あり。

ファイルタイプとパーミッションを調べるのにマスキングを使う。

sys/stat.hのファイルタイプのとこ

/* File type */
#define S_IFMT          0170000         /* [XSI] type of file mask */
#define S_IFIFO         0010000         /* [XSI] named pipe (fifo) */
#define S_IFCHR         0020000         /* [XSI] character special */
#define S_IFDIR         0040000         /* [XSI] directory */
#define S_IFBLK         0060000         /* [XSI] block special */
#define S_IFREG         0100000         /* [XSI] regular */
#define S_IFLNK         0120000         /* [XSI] symbolic link */
#define S_IFSOCK        0140000         /* [XSI] socket */
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
#define S_IFWHT         0160000         /* whiteout */
#define S_IFXATTR       0200000         /* extended attribute */
#endif

man ls

b     Block special file.
c     Character special file.
d     Directory.
l     Symbolic link.
s     Socket link.
p     FIFO.
-     Regular file.

Unix/Linuxプログラミング理論と実践 2章(cpを書く)

システムコールを使ってcpコマンドを書く。

ProductName Unix/Linuxプログラミング理論と実践
Bruce Molay
アスキー・メディアワークス / ¥ 6,090 ()
在庫あり。

そのあと、バッファするバージョンのwhoコマンドを書く。