Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*--------------------------------------------------------------------------
- File: mon2.c
- Description: This program creates a process to run the program identified
- on the commande line. It will then start procmon in another
- process to monitor the change in the state of the first process.
- After 20 seconds, signals are sent to the processes to terminate
- them.
- Also a third process is created to run the program filter.
- A pipe is created between the procmon process and the filter
- process so that the standard output from procmon is sent to
- the standard input of the filter process.
- --------------------------------------------------------------------------*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <signal.h>
- /* It's all done in main */
- int main(int argc, char **argv)
- {
- char *program;
- printf("%s\n",argv[0] );
- printf("%s\n",argv[1] );
- int forkInfo;
- int mProcessID = 0;
- int filterForkInfo;
- int procmonForkInfo;
- int prpid;
- /* First Step: Create the first process to run the program from the command line */
- //Running the M program and return the process id of that process
- prpid = fork();
- if (prpid == -1) /* Error */
- {
- printf("First fork failed.\n");
- exit(-1);
- }
- else if (prpid == 0) /* Child - start the program */
- {
- execl(program, program, NULL);
- exit(-1); /* in case execl fails */
- }
- int pipeInfo[2];
- /* Second step: Create the pipe to be used for connecting procmon to filter */
- if (pipe(pipeInfo) == -1)
- {
- printf("Pipe creation failed.\n"); /* Error */
- exit(-1);
- }
- /* Third step: Lets create the filter process - don't forget to connect to the pipe */
- filterForkInfo = fork();
- if(filterForkInfo == -1 ){
- perror("fork in filter");
- exit(1);
- }else if(filterForkInfo == 0){
- if(dup2(pipeInfo[0],0)==-1){
- perror("There was error conecting the filter file descriptor");
- exit(-1);
- }
- close(pipeInfo[0]);
- //close(pipeInfo[1]);
- execl("filter", "filter", NULL);
- }
- /* Fourth step: Lets create the procmon process - don't forget to connect to the pipe */
- //printf("The process id is %d\n", prpid);
- procmonForkInfo = fork();
- if(procmonForkInfo == -1 ){
- perror("The procmon Fork");
- exit(1);
- }else if(procmonForkInfo == 0){
- //start the procmon process
- if(dup2(pipeInfo[1], 1)==-1){
- perror("procmon dup2");exit(-1);
- exit(-1);
- }
- close(pipeInfo[0]);
- close(pipeInfo[1]);
- printf("Launching procmon with PID : %d\n", prpid);
- execl("procmon", mProcessID, NULL);
- }
- /* Fifth step: Let things run for 20 seconds */
- sleep(20);
- /* Last step:
- 1. Kill the process running the program
- 2. Sleep for 2 seconds
- 3. Kill the procmon and filter processes
- */
- kill(prpid,SIGTERM);
- sleep(2);
- kill(filterForkInfo, SIGTERM);
- kill(procmonForkInfo, SIGTERM);
- return(0); /* All done */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement