Advertisement
Xom9ik

Lab_2/15var (IV semester) OS&SP

Apr 3rd, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. //child.exe
  2. #include "stdafx.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <iostream>
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     std::cout << "Child started" << std::endl;
  10.     std::cout << "Count arguments: " << argc << std::endl;
  11.     std::cout << "Arguments:" << std::endl;
  12.     for (int i = 0; i < argc; i++) {
  13.         std::cout << i+1 << ") " << argv[i] << std::endl;
  14.     }
  15.     system("pause");
  16.     std::cout << "Child destroyed" << std::endl;
  17.     return 0;
  18. }
  19.  
  20. //parent_1_1.exe
  21. #include "stdafx.h"
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <iostream>
  25. #include <process.h>
  26. #include <time.h>
  27.  
  28. void main()
  29. {
  30.     std::cout << "Parent_1_1 started" << std::endl;
  31.     time_t rawtime;
  32.     struct tm * timeinfo;
  33.     time(&rawtime);
  34.     timeinfo = localtime(&rawtime);
  35.     std::cout << "Parent_1_1 call <execl(char *fname, char *arg0, ..., char *argN, NULL)> child.exe" << std::endl;
  36.     if (execl("child.exe", "Parent_1_1", "username", "password", "email", asctime(timeinfo), NULL) == -1)
  37.         std::cout << "Error: " << errno << std::endl;
  38.     else
  39.         std::cout << "All is well" << std::endl;
  40. }
  41.  
  42. //parent_1_2.exe
  43. #include "stdafx.h"
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <iostream>
  47. #include <process.h>
  48. #include <time.h>
  49.  
  50. void main()
  51. {
  52.     std::cout << "Parent_1_2 started" << std::endl;
  53.     time_t rawtime;
  54.     struct tm * timeinfo;
  55.     time(&rawtime);
  56.     timeinfo = localtime(&rawtime);
  57.     std::cout << "Parent_1_2 call <spawnl(int mode, char *fname, char *arg0, ..., char *argN, NULL)> child.exe" << std::endl;
  58.     if (spawnl(P_WAIT, "child.exe", "Parent_1_2", "username", "password", "email", asctime(timeinfo), NULL) == -1)
  59.         std::cout << "Error: " << errno << std::endl;
  60.     else
  61.         std::cout << "All is well" << std::endl;
  62.     std::cout << "Parent_1_2 destroyed" << std::endl;
  63.     system("pause");
  64. }
  65.  
  66. //parent_2_1.exe
  67. #include "stdafx.h"
  68. #include <stdio.h>
  69. #include <stdlib.h>
  70. #include <iostream>
  71. #include <process.h>
  72. #include <time.h>
  73. #include <vector>
  74.  
  75. void main()
  76. {
  77.     std::cout << "Parent_2_1 started" << std::endl;
  78.     time_t rawtime;
  79.     struct tm * timeinfo;
  80.     time(&rawtime);
  81.     timeinfo = localtime(&rawtime);
  82.     std::vector<char*> commandVector;
  83.  
  84.     commandVector.push_back(const_cast<char*>("child.exe"));
  85.     commandVector.push_back(const_cast<char*>("Parent_2_1"));
  86.     commandVector.push_back(const_cast<char*>("username"));
  87.     commandVector.push_back(const_cast<char*>("password"));
  88.     commandVector.push_back(const_cast<char*>("email"));
  89.     commandVector.push_back(const_cast<char*>(asctime(timeinfo)));
  90.     commandVector.push_back(NULL);
  91.  
  92.     std::cout << "Parent_2_1 call <execv(char *fname, char *arg[ ])> child.exe" << std::endl;
  93.     if (execv(commandVector[0], &commandVector[0]) == -1)
  94.         std::cout << "Error: " << errno << std::endl;
  95.     else       
  96.         std::cout << "All is well" << std::endl;
  97.     std::cout << "Parent_2_1 destroyed" << std::endl;
  98.     system("pause");
  99. }
  100.  
  101. //parent_2_2.exe
  102. #include "stdafx.h"
  103. #include <stdio.h>
  104. #include <stdlib.h>
  105. #include <iostream>
  106. #include <process.h>
  107. #include <time.h>
  108. #include <vector>
  109.  
  110. void main()
  111. {
  112.     std::cout << "Parent_2_2 started" << std::endl;
  113.     time_t rawtime;
  114.     struct tm * timeinfo;
  115.     time(&rawtime);
  116.     timeinfo = localtime(&rawtime);
  117.     std::vector<char*> commandVector;
  118.  
  119.     commandVector.push_back(const_cast<char*>("child.exe"));
  120.     commandVector.push_back(const_cast<char*>("Parent_2_2"));
  121.     commandVector.push_back(const_cast<char*>("username"));
  122.     commandVector.push_back(const_cast<char*>("password"));
  123.     commandVector.push_back(const_cast<char*>("email"));
  124.     commandVector.push_back(const_cast<char*>(asctime(timeinfo)));
  125.  
  126.     std::cout << "Parent_2_2 call <spawnv(int mode, char *fname, char *arg[ ])> child.exe" << std::endl;
  127.     if (spawnv(P_WAIT, commandVector[0], &commandVector[0]) == -1)
  128.         std::cout << "Error: " << errno << std::endl;
  129.     else
  130.         std::cout << "All is well" << std::endl;
  131.     std::cout << "Parent_2_2 destroyed" << std::endl;
  132.     system("pause");
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement