linux下c/c++程序重启自己

在程序自动升级的时候需要自己重新启动自己
示例代码:

点击(此处)折叠或打开

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>

  4. int main(int argc, char **argv)
  5. {
  6.     char buf[32] = {};
  7.     char *exec_argv[] = { argv[0], buf, 0 };
  8.     int count = argc > 1 ? atoi(argv[1]) : 0;

  9.     printf("Running: %s %d\n", argv[0], count);
  10.     snprintf(buf, sizeof(buf), "%d", count+1);
  11.     sleep(5);

  12.     execv("/proc/self/exe", exec_argv);

  13.     /* NOT REACHED */
  14.     return 0;
  15. }
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
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(1850) | 评论(0) | 转发(0) |
0

上一篇:linux下mp3批量转wav与spx

下一篇:gsoap:import: Cannot open file "stlvector.h" for reading

给主人留下些什么吧!~~
评论热议

猜你喜欢

转载自blog.csdn.net/zmlovelx/article/details/80263008
今日推荐