inter process communcation, parent - child

This is a discussion on inter process communcation, parent - child within the C Programming forums, part of the General Programming Boards category; Hi there, i have a problem with the ipc, i want to start a shell in the child process like ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    inter process communcation, parent - child

    Hi there,

    i have a problem with the ipc, i want to start a shell in the child process
    like
    Code:
    //child
    if (pid == 0){
           system("sh");
    }
    //parent
    else{
            //readin from the command line (not the "sh" in child )
            //send read to child an execute it in the "sh"
    }

    the readline stuff is not the problem, but i dont know how i could switch between child and parent because the shell is running all the time, i just can enter the parent if i exit the shell, is there a way to switch the process after finishing a shell command ?

    like:
    child start sh
    switch process
    parent readin "command"
    parent send "command" to child
    child execute "command"
    child send the result of the command back to parent
    parent printf ....

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tallan View Post
    is there a way to switch the process
    No. That's why it's called "inter process communication" and not "process switching".

    Pretend the parent and the child are seperate programs. If you had two separate programs running at the same time and you wanted one to send a message to the other, how would you do that?

    - you could write to and read from a file
    - you could write to and read from a pipe
    - you could write to and read from a socket
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    hope you understand my english correct, it isnt that good, with switch i mean i want to tell the child to sleep so the parent is working, like in


    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/types.h>
    
    int main (){
       int pid, j, i;
       int pfds[2];
       pipe(pfds);
       char buf[30];
    
       int once = 0;
       pid = fork();
    
    
    
       if (pid == 0)
       {
    
          /* Kindprozess
           * wenn fork eine 0 zurückgibt, befinden wir uns im Kindprozess
           */
          for (j=0; j < 10; j++)
          {
        	  printf ("Kindprozess:  %d (PID: %d)\n", j, getpid());
        	  write(pfds[1], "test", 5);
            sleep (1);
          }
          exit (0);
       }
       else if (pid > 0)
       {
          /* Vaterprozess
           * Gibt fork einen Wert größer 0 zurück, so ist dies die PID des Kindprozesses
           */
          for (i=0; i < 10; i++)
          {
             printf ("Vaterprozess: %d (PID: %d)\n", i, getpid());
             read(pfds[0], buf, 5);
             printf("PARENT: read \"%s\"\n", buf);
    
             sleep (1);
          }
       }
       else
       {
          /* Wird ein negativer Wert zurückgegeben, ist ein Fehler aufgetreten */
          fprintf (stderr, "Error");
          exit (1);
       }
       return 0;
    }
    but with an running shell in the child
    so i cant tell the shell to sleep to"switch"




    so is there no way to do that without making a second programm ?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Have a look at "popen".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tallan View Post
    so is there no way to do that without making a second programm ?
    The point of fork() is that it is the same thing as a second program.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    i have taken a look on "popen" but its the same problem i think or i dont get it....

    i want to run a commandline programm in the child like sh, bash or sqlplus
    and send commands from parent to the child witch base on the programm running on the child


    example :

    //child run sqlplus
    //parent send sqlcommand to child
    //child read the command
    //child execute the command
    //child send result to the parent
    //parent read result

    i really dont get it could someone plz give me an example ? sitting her for days but i dont find a way to "activate" the parent after starting sqlplus in the child...

    so if there is anyone out there who know's the answer plz ..........

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 11:05 AM
  2. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  3. Replies: 3
    Last Post: 10-15-2008, 10:24 AM
  4. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 08:53 AM
  5. Sending a message to parent from child process
    By maxorator in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2005, 05:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21