Guest User

Untitled

a guest
Dec 10th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #ifndef SUBPROCESS_H_
  4. #define SUBPROCESS_H_
  5.  
  6. class Subprocess {
  7. public:
  8. Subprocess(const char* name_of_child_process);
  9. Subprocess(const Subprocess & obj);
  10. ~Subprocess();
  11. void Create_Process();
  12. private:
  13. pid_t pid;
  14. const char* cmdline;
  15. };
  16. void signal_handler(int signal);
  17.  
  18. #endif /* SUBPROCESS_H_ */
  19.  
  20.  
  21.  
  22. /*
  23. * Subprocess.cpp
  24. */
  25.  
  26. #include <iostream>
  27. #include <sys/types.h>
  28. #include <unistd.h>
  29. #include <sys/types.h>
  30. #include <sys/wait.h>
  31. #include <signal.h>
  32. #include <csignal>
  33. #include "Subprocess.h"
  34. #include <vector>
  35. #include <memory>
  36. using namespace std;
  37.  
  38. void signal_handler(int signal)
  39. {
  40. cout<<"I am a signal handler"<<endl;
  41. int child_status;
  42. pid_t pid;
  43. for(;;)
  44. {
  45. pid=waitpid(-1, &child_status, WNOHANG);
  46. if(pid>0)
  47. {
  48. cout <<"Process with pid "<<pid<<" terminated"<<endl;
  49. pid=fork();
  50. cout << "Process with pid "<<pid<<"restarted "<<endl;
  51. break;
  52. }
  53. pause();
  54. }
  55.  
  56. }
  57.  
  58. Subprocess::Subprocess(const char* name_of_child_process)
  59. {
  60. cout<<"Default constructor"<<endl;
  61. cmdline=name_of_child_process;
  62. pid=fork();
  63. if (pid==-1) cout<<"Error ,process not fork"<<endl;
  64. else if (pid==0) {
  65. cout <<"I am children process"<<getpid()<<" My pa_pid "<<getppid()<<endl;
  66. execlp("gnome-terminal","gnome-terminal","-e",cmdline,NULL);
  67. cout<<"Child process created with name "<<cmdline<<endl;
  68. for(;;);
  69. }
  70. else
  71. {
  72. cout<<"I am a parent with pid "<<getpid()<<endl;
  73. signal(SIGCHLD,signal_handler);
  74.  
  75.  
  76. }
  77.  
  78.  
  79. }
  80.  
  81.  
  82. void Subprocess::Create_Process()
  83. {
  84.  
  85. }
  86.  
  87. Subprocess::~Subprocess() {
  88. kill(pid,SIGTERM);
  89. }
  90.  
  91. int main()
  92. {
  93.  
  94. Subprocess process1("/bin/sh");
  95. //Subprocess process2("xclock");
  96. //vector<shared_ptr<Subprocess> > processes;
  97. //processes.push_back(process1("/bin/sh"));
  98. //processes.push_back(make_shared<Subprocess>("/bin/sh"));
  99. }
  100.  
  101. pid=fork();
  102. if (pid==-1) cout<<"Error ,process not fork"<<endl;
  103. else if (pid==0) {
  104. cout <<"I am children process"<<getpid()<<" My pa_pid "<<getppid()<<endl;
  105. execlp("gnome-terminal","gnome-terminal","-e",cmdline,NULL);
  106. // Этот код, следующий ПОСЛЕ execlp, не будет выполняться НИКОГДА
  107. cout<<"Child process created with name "<<cmdline<<endl;
  108. for(;;);
  109. }
  110. else
  111. {
  112. cout<<"I am a parent with pid "<<getpid()<<endl;
  113. // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  114. Запоминаем pid в списке запущенных процессов
  115.  
  116. }
Add Comment
Please, Sign In to add comment