Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. /*--------------------------------------------------------------------------
  2. File: mon2.c
  3.  
  4. Description: This program creates a process to run the program identified
  5. on the commande line. It will then start procmon in another
  6. process to monitor the change in the state of the first process.
  7. After 20 seconds, signals are sent to the processes to terminate
  8. them.
  9.  
  10. Also a third process is created to run the program filter.
  11. A pipe is created between the procmon process and the filter
  12. process so that the standard output from procmon is sent to
  13. the standard input of the filter process.
  14. --------------------------------------------------------------------------*/
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <sys/types.h>
  18. #include <signal.h>
  19.  
  20.  
  21. /* It's all done in main */
  22. int main(int argc, char **argv)
  23. {
  24. char *program;
  25. printf("%s\n",argv[0] );
  26. printf("%s\n",argv[1] );
  27.  
  28. int forkInfo;
  29. int mProcessID = 0;
  30. int filterForkInfo;
  31. int procmonForkInfo;
  32. int prpid;
  33.  
  34. /* First Step: Create the first process to run the program from the command line */
  35.  
  36. //Running the M program and return the process id of that process
  37. prpid = fork();
  38.  
  39. if (prpid == -1) /* Error */
  40. {
  41. printf("First fork failed.\n");
  42. exit(-1);
  43. }
  44. else if (prpid == 0) /* Child - start the program */
  45. {
  46. execl(program, program, NULL);
  47. exit(-1); /* in case execl fails */
  48. }
  49.  
  50. int pipeInfo[2];
  51.  
  52. /* Second step: Create the pipe to be used for connecting procmon to filter */
  53. if (pipe(pipeInfo) == -1)
  54. {
  55. printf("Pipe creation failed.\n"); /* Error */
  56. exit(-1);
  57. }
  58.  
  59. /* Third step: Lets create the filter process - don't forget to connect to the pipe */
  60. filterForkInfo = fork();
  61. if(filterForkInfo == -1 ){
  62. perror("fork in filter");
  63. exit(1);
  64. }else if(filterForkInfo == 0){
  65. if(dup2(pipeInfo[0],0)==-1){
  66. perror("There was error conecting the filter file descriptor");
  67. exit(-1);
  68. }
  69.  
  70. close(pipeInfo[0]);
  71. //close(pipeInfo[1]);
  72. execl("filter", "filter", NULL);
  73. }
  74.  
  75. /* Fourth step: Lets create the procmon process - don't forget to connect to the pipe */
  76. //printf("The process id is %d\n", prpid);
  77. procmonForkInfo = fork();
  78. if(procmonForkInfo == -1 ){
  79. perror("The procmon Fork");
  80. exit(1);
  81. }else if(procmonForkInfo == 0){
  82. //start the procmon process
  83. if(dup2(pipeInfo[1], 1)==-1){
  84. perror("procmon dup2");exit(-1);
  85. exit(-1);
  86. }
  87.  
  88. close(pipeInfo[0]);
  89. close(pipeInfo[1]);
  90. printf("Launching procmon with PID : %d\n", prpid);
  91. execl("procmon", mProcessID, NULL);
  92. }
  93. /* Fifth step: Let things run for 20 seconds */
  94. sleep(20);
  95.  
  96. /* Last step:
  97. 1. Kill the process running the program
  98. 2. Sleep for 2 seconds
  99. 3. Kill the procmon and filter processes
  100. */
  101.  
  102. kill(prpid,SIGTERM);
  103. sleep(2);
  104. kill(filterForkInfo, SIGTERM);
  105. kill(procmonForkInfo, SIGTERM);
  106.  
  107.  
  108. return(0); /* All done */
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement