日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

應(yīng)用部署為Solaris 10 SMF服務(wù)( 二 )


int main(int argc, char **argv)
{
FILE *fp;
time_t t;
/* Read the app configuration here if applicable. */
/* If error occurred, return non-zero. */
if (read_config() != RUN_OK)
{
printf("%s: read configuration failuren", argv[0]);
exit(CONFIG_ERROR);
}
/* Initialize application. Any error, return non-zero. */
if (app_init() != RUN_OK)
{
printf("%s: initialization failuren", argv[0]);
exit(FATAL_ERROR);
}
daemonize(); /* Make the application a daemon. */
/* Service logic is placed here. */
while (1)
{
sleep(5); /* Sleep for 5 seconds. In a real application, it could be */
/* waiting for service requests, e.g. waiting on message */
/* queue, etc. */
/* service logic */
if ((fp = fopen("/tmp/myapp.log","a")) != NULL)
{
t = time(0);
fprintf(fp, "myapp is running at %sn",asctime(localtime(&t)));
fclose(fp);
}
else
{
exit(FATAL_ERROR);
}
}
}
/* make the application a daemon */
void daemonize(void)
{
int pid;
int i;
if (pid = fork())
exit(RUN_OK); /* parent exits */
else if (pid < 0)
exit(FATAL_ERROR); /* fork() failed */
/* first child process */
setsid();
if (pid = fork())
exit(RUN_OK); /* first child exits */
else if(pid < 0)
exit(FATAL_ERROR); /* fork() failed */
/* second child */
for (i = 0; i < NOFILE;i) /* close all files */
close(i);
chdir("/"); /* change Directory to root(/) directory */
umask(0); /* set proper file mask */
}
/* read configuration */
int read_config(void)
{
return RUN_OK; /* OK */
}
/* initialize application */
int app_init(void)
{
return RUN_OK; /* OK */
}
利用Sun公司最新推出的C/C/Fortran開發(fā)及編譯環(huán)境Sun Studio 11,使用以下命令將myapp.c編譯成可執(zhí)行程序myapp 。
$ /opt/SUNWspro/bin/cc -o ./myapp ./myapp.c
或者直接使用Solaris 10自帶的gcc編譯器將myapp.c編譯成可執(zhí)行程序myapp 。
$ /usr/sfw/bin/gcc -o ./myapp ./myapp.c
編譯成功后在當(dāng)前目錄下會生成myapp可執(zhí)行程序 。本例假設(shè)當(dāng)前目錄為/export/home/smfdemo 。直接在命令行輸入. /myapp就可以啟動myapp為后臺守護進程,同時可以在/tmp目錄下找到myapp.log文件 。使用“/usr/bin/tail -f /tmp/myapp.log命令可以看到進程myapp不斷在日志文件中報告自己的存在 。為了使這個進程在服務(wù)器啟動時自動運行,傳統(tǒng)做法一般需要寫三個shell腳本 。其中一個shell腳本置于/etc/init.d目錄下用以實際啟動和停止myapp服務(wù)(例如<表2所示的/etc/init.d/myapp.sh) 。另外兩個shell腳本的名字分別以S和K開頭(例如,S79myapp和K79myapp),置于合適的/etc/rc?.d目錄下(例如,/etc/rc2.d目錄下),分別以不同參數(shù)(start和stop)調(diào)用 /etc/init.d/myapp.sh 。操作系統(tǒng)在boot時會自動運行S開頭的腳本以啟動myapp服務(wù),而在shutdown時自動運行K開頭的腳本以關(guān)閉myapp服務(wù) 。
表2.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/sbin/sh
###############################################################################
# /etc/init.d/myapp.sh #
###############################################################################
RUN_OK=0
CONFIG_ERROR=1
FATAL_ERROR=2
case "$1" in
'start')
/export/home/smfdemo/myapp
if [ $? -eq $CONFIG_ERROR ]; then
exit $CONFIG_ERROR
fi
if [ $? -eq $FATAL_ERROR ]; then
exit $FATAL_ERROR
fi

推薦閱讀