gpbarga's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
Members
Active Posts:
16 (0.01 per day)
Joined:
29-April 12
Profile Views:
273
Last Active:
User is offline Jun 28 2015 11:39 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   gpbarga has not set their status

Posts I've Made

  1. In Topic: Need assistance with a lingering child process in C

    Posted 28 Jun 2015

    Well I think the issue lies within the sleep function, is it possible that if a child process is sleeping that it's probably delaying the message in the pipe? because if I don't implement the code with sleep, all the processes will terminate immediately after it has been signaled.. so could it be I either placed my sleep system call incorrectly?
  2. In Topic: Help with Child processes

    Posted 28 Jun 2015

    Ugh I believe I was thinking about this all wrong, I came up with a new strategy. Instead of the original parent process constantly creating new processes to run the file, I just changed it so that the child 3, which is the one running the file, have it create a fork of its own and run the exec file from there, therefore eliminating my problem with the parent waiting for multiple children and what not. Now I am having a problem with the pipes, since child 3 is forking and exec'ing the file, it is reliant on the signal from the other sibling for it to quit. but for some reason the signal is not going through even though one of the other siblings receive the signal. I am under the impression that if a child process is only listening on one end of a pipe, it is possible to just have 1 pipe where one sibling writes to it and never listens while the other two are only listening and not writing. Is this somehow a mistake? should I include two separate pipes for the two processes? or can I keep it as is? unless maybe there is also a flaw in my logic again.

    this is my revised runFile() code

    void runFile(){
       int status = RUN;
       int newStatus = RUN;
       pid_t pid;
       
       printf("in runFile() my pid is %d\n", getpid());
       
       if(close(fd[1]) == -1){
          printf("Error closing writing end of pipe runFile\n");
          _exit(EXIT_FAILURE);
       }
       while(status != STOP){
          if((pid = fork()) == -1){
             printf("Fork failed\n");
    	 _exit(EXIT_FAILURE);
          }
          else if(pid == 0){
             printf("child %d within %dchild running exec\n", getpid(), getppid());
    	 execl("/usr/bin/uptime", "uptime", NULL);
             printf("exec failed!\n");
    	 _exit(EXIT_FAILURE);
          }
          else{
             int ret;
    	 pid_t justLeft;
    	 justLeft = wait(&ret);
    	 printf("The parent %d, child pid:%d exit status %d\n" , getpid(), justLeft, (ret >>
    	 8));
    	 read(fd[0], &newStatus, sizeof(newStatus));
             status = newStatus;
             printf("Status before going top of while looprunFile - %d\n", status);
          }
          sleep(5);
       }
       
       printf("Signal has been received, closing pipes and exiting uptime mananger\n");
       
       if(close(fd[0]) == -1){
          printf("Error closing reading end of runFile pipe\n");
          _exit(EXIT_FAILURE);
       }
       
       _exit(EXIT_SUCCESS);
       
    }
    
  3. In Topic: Help with Child processes

    Posted 28 Jun 2015

    So I tried implementing your idea, I created a while loop (starts at line 146) specifically for waiting and handling the process that ends early to create a new fork and run the same code, but for some reason, when it creates the new child to re-run the execlp code it either is not running it at the right time or running it at all. I want it to run every 5 seconds and then when the other two processes end it would have received the signal from the pipe and terminate it self then the parent process would break out of it's while loop.. can you help me find where the error is in my logic?

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/wait.h>
    #include <sys/types.h>
    #include <time.h>
    
    #define DEFAULT 10
    #define pipeSize 2
    #define NUMBEROFCHILD 3
    #define STOP 1
    #define RUN 0
    
    int fd[pipeSize];
    
    void wallClock(){
       int status = RUN;		/*signals*/
       int newStatus = RUN;
    
       time_t now;
       struct tm *lcltime;
       if(close(fd[1]) == -1){
          printf("Error closing writing end of pipe\n");
          _exit(EXIT_FAILURE);
       }
       
       while(status != STOP){								/*loop until signal occurs*/
          now = time(NULL);
          lcltime = localtime(&now);
          printf("the time is %d:%d:%d\n", lcltime->tm_hour,lcltime->tm_min,lcltime->tm_sec);
          sleep(1);
          read(fd[0], &newStatus, sizeof(newStatus));					/*check for status*/
          status = newStatus;
       }
       
       if(close(fd[0]) == -1){
          printf("Error closing reading end of pipe\n");
          _exit(EXIT_FAILURE);
       }
       printf("Message has been received in wallClock()--Terminating now\n");
       _exit(EXIT_SUCCESS);
    }
    
    void runFile(){
       int status = RUN;
       int newStatus = RUN;
       
       if(close(fd[1]) == -1){
          printf("Error closing writing end of pipe runFile\n");
          _exit(EXIT_FAILURE);
       }
       
       read(fd[0], &newStatus, sizeof(newStatus));				/*check for status*/
       status = newStatus;
        
       if(status != STOP){
          if(close(fd[0]) == -1){
             printf("Error closing reading end of pipe\n");
    	 _exit(EXIT_FAILURE);
          }
          execlp("/usr/bin/uptime", "uptime", NULL);
       }
       else{								/*signal received stop running runFile*/
          if(close(fd[0]) == -1){
             printf("Error closing reading end of pipe\n");
    	 _exit(EXIT_FAILURE);
          }
          printf("Signal received, exiting runFile()");
          _exit(EXIT_SUCCESS);
       }
    }
    
    void countDown(int start){
       int stopNow = STOP; 		/*signals*/
       int cont = RUN;
       
       if(close(fd[0]) == -1){
          printf("Error closing reading end of pipe\n");
          _exit(EXIT_FAILURE);
       }
       
       while(start > 0){				/*while loop until start reaches 0*/
          if(start >= 10){
          printf("Count Down: 00:%2d\n", start);
          }
          else{
          printf("Count Down: 00:0%d\n", start);
          }
          write(fd[1], &cont, sizeof(cont));	/*write in pipe to continue still*/
          start--;
          sleep(1);
       }
       
       printf("Count Down: 00:00 -- sending message to terminate other processess\n");
       write(fd[1], &stopNow, sizeof(stopNow));     /*Tell others to terminate*/
       
       if(close(fd[1]) == -1){
          printf("Error closing writing end of pipe\n");
          _exit(EXIT_FAILURE);
       }
       
       _exit(EXIT_SUCCESS);
    }
    
    int main(int argc, char *argv[]){
       
       int time , i, status, n, newChild, numExited;
       pid_t child[NUMBEROFCHILD];
       pid_t pid;
       printf("Parent pid is %d\n", getpid());
       if(argc <= 2){			/*check arguments*/
          if(argv[1] != '\0'){
             time = atoi(argv[1]);
          }
          else
             time = DEFAULT;
       }
       else{
          printf("Too many parameters -- exiting program\n");
          return -1;
       }
       
       if(pipe(fd) == -1){			/*open pipes*/
          printf("Error opening pipe\n");
          exit(EXIT_FAILURE);
       }
       
       for(i = 0; i < NUMBEROFCHILD; i++){   	/* create children */    
          if((child[i] = fork())  == -1){
             printf("Error in creating child\n");
    	 exit(EXIT_FAILURE);
          }
          if(child[i] == 0){
             switch(i){
    	    case 0:   wallClock();
    	    break;
    	    case 1:   runFile();
    	    break;
    	    case 2:   countDown(time);
    	    break;
    	 } 
          }
       }
       n = NUMBEROFCHILD;
       
       while(numExited <= NUMBEROFCHILD){			/*Wait for all children to finish*/
          pid = wait(&status);
          if(pid == -1){
             printf("Error exiting\n");
    	 exit(EXIT_FAILURE);
          }
          if(pid == child[0] || pid == child[2]){	/*If wallClock child and countDown child exited, increment numExited*/
             printf("PID:%d exited\n", pid);
             numExited++;
          }
          if(numExited == 2){			
             pid = wait(&status);			/*If other processes have exited, waiting for last one*/
    	 if(pid == -1){
    	    printf("Error exiting\n");
    	    exit(EXIT_FAILURE);
    	 }
    	 numExited++;
    	}
          else{
             if((newChild = fork()) == -1){		/*create new child to execute file*/
                printf("Error creating newChild\n");
    	    exit(EXIT_FAILURE);
             }   
             if(newChild == 0){
                sleep(5);
    	    printf("newChild is created, pid is %d, PPID is %d, now going to run runFile()\n", getpid(), getppid());
    	    runFile();
             }
          } 
       }
       
       if(close(fd[1]) == -1){			
          printf("Error closing parent reading pipe\n");
          exit(EXIT_FAILURE);
       }
       
       if(close(fd[0]) == -1){			
          printf("Error closing parent writing pipe\n");
          exit(EXIT_FAILURE);
       }
       
       printf("All processes has ended, exiting program --\n");
       return 0;
       
    }
    
    
  4. In Topic: Help with Child processes

    Posted 27 Jun 2015

    Sort of, because what you are doing is you are creating a new child over and over again.. is there no way at all to reuse the same child process? or is it pointless to re-use the same child process? I guess if there's no choice then I think I can implement a way to keep on creating a new child

    View Posthorace, on 27 June 2015 - 11:53 PM, said:

    thinking again could you use the system() call
    The parent process is suspended and the command line is passed to the shell command interpreter which, assuming all is well, executes the command. When the command interpreter terminates, the parent process is resumed with system returning the return code from the command processor. which suspends the current process
    e.g.
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/wait.h>
    int main(void)
    {
      int child=0;
      // run child 
      while(1)
        {
          printf("\nsystem() call %d\n",child++);
          system("ps -l");
          sleep(5);
        }
        return 0;
    }
    

    a run gives
    $gcc system.c
    $./a.out
    
    system() call 0
    F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
    0 S  1000  1469  1465  0  80   0 -  3672 wait   pts/0    00:00:00 bash
    0 S  1000  2885  1469  0  80   0 -  1017 wait   pts/0    00:00:00 a.out
    0 R  1000  2886  2885  0  80   0 -  6117 -      pts/0    00:00:00 ps
    
    system() call 1
    F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
    0 S  1000  1469  1465  0  80   0 -  3672 wait   pts/0    00:00:00 bash
    0 S  1000  2885  1469  0  80   0 -  1017 wait   pts/0    00:00:00 a.out
    0 R  1000  2887  2885  0  80   0 -  6117 -      pts/0    00:00:00 ps
    
    system() call 2
    F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
    0 S  1000  1469  1465  0  80   0 -  3672 wait   pts/0    00:00:00 bash
    0 S  1000  2885  1469  0  80   0 -  1017 wait   pts/0    00:00:00 a.out
    0 R  1000  2888  2885  0  80   0 -  6117 -      pts/0    00:00:00 ps
    
    
    


    No, unfortunately I am not allowed to use system calls for this assignment
  5. In Topic: Help with Child processes

    Posted 27 Jun 2015

    I have another question, and since it is related to this program I hope it is okay to ask it here.

    My problem now is with implementing the 3rd child to run an execl command continuously until it reads the signal to terminate from the pipe. From what I understand is when it runs an execl command, it runs the file and then quits the process immediately, so I am assuming in order to keep it running I need some type of handler in the parent process to catch that process and somehow re-run it again. I was thinking of using fork() from the same child id but I don't think that works since it creates a new process with a new pid and such. Is there another command I don't know about to re-run the same child process or is there another way?

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

gpbarga hasn't added any friends yet.

Comments

gpbarga has no profile comments yet. Why not say hello?