Drkcore

11 10 2009 Linux Tweet

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

別のプログラムを起動するプログラム

#include <stdio.h>

int main()
{
  char *arglist[3];
  arglist[0] = "ls";
  arglist[1] = "-l";
  arglist[2] = 0;

  printf("* * * About to exec ls -l\n");
  execvp( "ls" , arglist );
  printf("* * * ls is done. bye\n");
}

arglist[2]に0が入ってるのがわからん。arglist[0]はプログラム名であり変更できる。

#include <stdio.h>

int main()
{
  char *arglist[2];
  arglist[0] = "sleeeeeeeeeeeP!";
  arglist[1] = "10";

  printf("* * * About to exec ls -l\n");
  execvp( "sleep" , arglist );
  printf("* * * ls is done. bye\n");
}

これを実行してpsで見てみると

$ ps
  PID TTY           TIME CMD
  149 ttys000    0:00.06 -bash
86661 ttys002    0:00.21 -bash
87689 ttys002    0:00.01 sleeeeeeeeeeeP! 10

コマンド名は実行した側が任意につけられるのか。

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

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021