fork download
  1. #include <vector>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. void ParserCmd(char *p_sCmd, vector<string> &p_vCmdSet)
  6. {
  7. string sCmd = p_sCmd;
  8. int p = sCmd.find(" ");
  9. while (p != string::npos)
  10. {
  11. string sTemp = sCmd.substr(0, p);
  12. sCmd = sCmd.substr(p + 1, sCmd.length() - p - 1);
  13. p_vCmdSet.push_back((char*)sTemp.c_str());
  14. p = sCmd.find(" ");
  15. }
  16. p_vCmdSet.push_back((char*)sCmd.c_str());
  17. }
  18.  
  19. bool MyCreateProcess(char *p_sAppName, char *p_sCmdArg, char *p_sEnvArg, int *p_nRetVal)
  20. {
  21. if (!p_sAppName)
  22. return false;
  23.  
  24. vector<string> vCmdSet;
  25. vCmdSet.clear();
  26. vCmdSet.push_back(p_sAppName);
  27.  
  28. if (p_sCmdArg)
  29. ParserCmd(p_sCmdArg, vCmdSet);
  30.  
  31. // Set environmnet pwd
  32. vector<string> vEnvArgSet;
  33. vEnvArgSet.clear();
  34.  
  35. string sEnvPath;
  36. char *pEnvPath = NULL;
  37.  
  38. sEnvPath = "LD_LIBRARY_PATH=";
  39. pEnvPath = getenv("LD_LIBRARY_PATH");
  40. char *pCurrentPath = getcwd(NULL, 0);
  41. if (pEnvPath)
  42. sEnvPath += (string(pEnvPath) + string(":") + string(pCurrentPath));
  43. else
  44. sEnvPath += string(pCurrentPath);
  45. free(pCurrentPath);
  46. vEnvArgSet.push_back((char*)sEnvPath.c_str());
  47.  
  48. if (p_sEnvArg)
  49. ParserCmd(p_sEnvArg, vEnvArgSet);
  50.  
  51. printf("-------------new--------------\n");
  52. char **ppCmdArg = new char*[vCmdSet.size() + 1];
  53. for (int i = 0; i < vCmdSet.size(); i++)
  54. {
  55. ppCmdArg[i] = NULL;
  56. ppCmdArg[i] = new char[vCmdSet[i].length() + 1];
  57. strcpy(ppCmdArg[i], vCmdSet[i].c_str());
  58. ppCmdArg[vCmdSet[i].length()] = 0;
  59. printf("ppCmdArg[%d] = %s\n", i, ppCmdArg[i]);
  60. }
  61. ppCmdArg[vCmdSet.size()] = NULL;
  62. printf("-------------new end--------------\n");
  63.  
  64. printf("-------------test 1--------------\n");
  65. for (int i = 0; i < vCmdSet.size(); i++)
  66. {
  67. printf("ppCmdArg[%d] = %s\n", i, ppCmdArg[i]);
  68. }
  69. printf("-------------test 1 end--------------\n");
  70.  
  71. char **ppEnvArg = new char*[vEnvArgSet.size() + 1];
  72. for (int i = 0; i < vEnvArgSet.size(); i++)
  73. {
  74. ppEnvArg[i] = new char[vEnvArgSet[i].length() + 1];
  75. strcpy(ppEnvArg[i], vEnvArgSet[i].c_str());
  76. ppEnvArg[vEnvArgSet[i].length()] = 0;
  77. }
  78. ppEnvArg[vEnvArgSet.size()] = NULL;
  79.  
  80. printf("-------------test 2--------------\n");
  81. for (int i = 0; i < vCmdSet.size(); i++)
  82. {
  83. printf("ppCmdArg[%d] = %s\n", i, ppCmdArg[i]);
  84. }
  85. printf("-------------test 2 end--------------\n");
  86.  
  87. //-------------------------------------------------------
  88. bool bRet = false;
  89. pid_t pID = fork();
  90. if (pID == -1)
  91. {
  92. printf("Error! CreateProcess %s fails, error = %d\n", p_sAppName, errno);
  93. return bRet;
  94. }
  95. else if (pID == 0) // sub-process start here
  96. {
  97.  
  98. if (execve(p_sAppName, ppCmdArg, ppEnvArg) < 0)
  99. {
  100. CUbiLogger::GetThis().Write("Error! Process %s fails, error = %d\n", p_sAppName, errno);
  101. _exit(-127);
  102. }
  103. else
  104. {
  105. bRet = true;
  106. }
  107. }
  108. else // parent process wait here
  109. {
  110. int status;
  111. if (waitpid(pID, &status, 0) == -1)
  112. {
  113. prtinf("waitpid failed");
  114. return bRet;
  115. }
  116.  
  117. if (WIFEXITED(status))
  118. {
  119. int nRet;
  120. nRet = WEXITSTATUS(status);
  121. if (p_nRetVal)
  122. {
  123. *p_nRetVal = nRet;
  124. if ((*p_nRetVal) == 0)
  125. bRet = true;
  126. }
  127. else
  128. bRet = true;
  129. }
  130. }
  131. #endif
  132. //-------------------------------------------------------
  133.  
  134. for (int i = 0; i < vCmdSet.size(); i++)
  135. {
  136. printf("ppCmdArg[%d] = %s\n", i, ppCmdArg[i]);
  137. if (ppEnvArg[i])
  138. {
  139. delete[] ppCmdArg[i];
  140. ppCmdArg[i] = NULL;
  141. }
  142. }
  143. delete[] ppCmdArg;
  144.  
  145. printf("vEnvArgSet size = %d\n", vEnvArgSet.size());
  146. for (int i = 0; i < vEnvArgSet.size(); i++)
  147. {
  148. printf("ppEnvArg[%d] = %s\n", i, ppEnvArg[i]);
  149. if (ppEnvArg[i])
  150. {
  151. delete[] ppEnvArg[i];
  152. ppEnvArg[i] = NULL;
  153. }
  154. }
  155. delete[] ppEnvArg;
  156.  
  157. return bRet;
  158. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:131:3: error: #endif without #if
  #endif
   ^~~~~
prog.cpp: In function ‘bool MyCreateProcess(char*, char*, char*, int*)’:
prog.cpp:40:26: error: ‘getcwd’ was not declared in this scope
     char *pCurrentPath = getcwd(NULL, 0);
                          ^~~~~~
prog.cpp:40:26: note: suggested alternative: ‘getw’
     char *pCurrentPath = getcwd(NULL, 0);
                          ^~~~~~
                          getw
prog.cpp:57:9: error: ‘strcpy’ was not declared in this scope
         strcpy(ppCmdArg[i], vCmdSet[i].c_str());
         ^~~~~~
prog.cpp:57:9: note: ‘strcpy’ is defined in header ‘<cstring>’; did you forget to ‘#include <cstring>’?
prog.cpp:3:1:
+#include <cstring>
 using namespace std;
prog.cpp:57:9:
         strcpy(ppCmdArg[i], vCmdSet[i].c_str());
         ^~~~~~
prog.cpp:75:9: error: ‘strcpy’ was not declared in this scope
         strcpy(ppEnvArg[i], vEnvArgSet[i].c_str());
         ^~~~~~
prog.cpp:75:9: note: ‘strcpy’ is defined in header ‘<cstring>’; did you forget to ‘#include <cstring>’?
prog.cpp:89:17: error: ‘fork’ was not declared in this scope
     pid_t pID = fork();
                 ^~~~
prog.cpp:98:13: error: ‘execve’ was not declared in this scope
         if (execve(p_sAppName, ppCmdArg, ppEnvArg) < 0)
             ^~~~~~
prog.cpp:98:13: note: suggested alternative: ‘qecvt’
         if (execve(p_sAppName, ppCmdArg, ppEnvArg) < 0)
             ^~~~~~
             qecvt
prog.cpp:100:13: error: ‘CUbiLogger’ has not been declared
             CUbiLogger::GetThis().Write("Error! Process %s fails, error = %d\n", p_sAppName, errno);
             ^~~~~~~~~~
prog.cpp:101:13: error: ‘_exit’ was not declared in this scope
             _exit(-127);
             ^~~~~
prog.cpp:101:13: note: suggested alternative: ‘_Exit’
             _exit(-127);
             ^~~~~
             _Exit
prog.cpp:111:7: error: ‘waitpid’ was not declared in this scope
   if (waitpid(pID, &status, 0) == -1)
       ^~~~~~~
prog.cpp:113:4: error: ‘prtinf’ was not declared in this scope
    prtinf("waitpid failed");
    ^~~~~~
prog.cpp:113:4: note: suggested alternative: ‘printf’
    prtinf("waitpid failed");
    ^~~~~~
    printf
prog.cpp:145:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘std::vector<std::__cxx11::basic_string<char> >::size_type’ {aka ‘long unsigned int’} [-Wformat=]
     printf("vEnvArgSet size = %d\n", vEnvArgSet.size());
            ^~~~~~~~~~~~~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~~
stdout
Standard output is empty