開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio 2017 (、Clang/C2(試験的))
- プログラミング言語: C(Visual C)
Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の9章(プロセスとシステムサービス - 限界を超える)、ごちゃごちゃのメッセージ(p. 412)を取り組んでみる。
ごちゃごちゃのメッセージ(p. 412)
コード
Sample1
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <process.h> // _execle
int main(int argc, char *argv[])
{
// donutsとcoffee.exe
char *my_env[] = { "FOOD=coffee.exe", NULL };
char s[80];
if (_execle("coffee.exe", "coffee.exe", "donuts", NULL, my_env) == -1)
{
strerror_s(s, 80, errno);
fprintf(stderr, "プロセス0を実行できません: %s\n", s);
return 1;
}
return 0;
}
Sample2
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <process.h> // _execle
int main(int argc, char *argv[])
{
// creamとdonuts
char *my_env[] = { "FOOD=donuts", NULL };
char s[80];
if (_execle("coffee.exe", "coffee.exe", "cream", NULL, my_env) == -1)
{
strerror_s(s, 80, errno);
fprintf(stderr, "プロセス0を実行できません: %s\n", s);
return 1;
}
return 0;
}
Sample3
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <process.h> // _execl
int main(int argc, char *argv[])
{
char *my_env[] = { "FOOD=donuts", NULL };
char s[80];
// coffee.exeとcoffee.exe
if (_execl("coffee.exe", "coffee.exe", NULL) == -1)
{
strerror_s(s, 80, errno);
fprintf(stderr, "プロセス0を実行できません: %s\n", s);
return 1;
}
return 0;
}
Sample4
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <process.h> // _execle
int main(int argc, char *argv[])
{
// coffee.exeとdonuts
char *my_env[] = { "FOOD=donuts", NULL };
char s[80];
if (_execle("coffee.exe", "coffee.exe", NULL, my_env) == -1)
{
strerror_s(s, 80, errno);
fprintf(stderr, "プロセス0を実行できません: %s\n", s);
return 1;
}
return 0;
}
入出力結果(コマンドプロンプト)
>Sample1.exe donutsとcoffee.exe >Sample2.exe creamとdonuts >Sample3.exe coffee.exeとcoffee.exe >Sample4.exe coffee.exeとdonuts >
Windows の場合、実行ファイルに拡張子「.exe」が付くから「coffee」ではなく「coffee.exe」になるけど、そのためのコードの修正は文字列の話で、目的のプロセスとシステムコールとは別の話になるからまたの機会に。
0 コメント:
コメントを投稿