在程序自动升级的时候需要自己重新启动自己
示例代码:
-
#include <unistd.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
int main(int argc, char **argv)
-
{
-
char buf[32] = {};
-
char *exec_argv[] = { argv[0], buf, 0 };
-
int count = argc > 1 ? atoi(argv[1]) : 0;
-
-
printf("Running: %s %d\n", argv[0], count);
-
snprintf(buf, sizeof(buf), "%d", count+1);
-
sleep(5);
-
-
execv("/proc/self/exe", exec_argv);
-
-
/* NOT REACHED */
-
return 0;
-
}
zengming@linux-eshv:~/test> g++ main.cpp
zengming@linux-eshv:~/test> ./a.out 1
Running: ./a.out 1
Running: ./a.out 2
Running: ./a.out 3
^C
程序员群:31843264
阅读(5138) | 评论(0) | 转发(0) |