How do I use pipes to communicate with another proccess that is being run using an execl command?
I know how I can send info to that process but how do I get info back from that proccess?

Some more information on what Im trying to do: I am trying to write a small database program. There are two programs, one program is an interface program where one will write the commands, the other program, the database program, will take in the commands and process them.

the interface program has a parent and a child process. The child is the process that calls the execl and the parent scans for keyboard input

  1. fork - this returns the child PID to the parent, and 0 to the child, unless there is an error, in which case it returns -1 and errno is set and no child process is started.
  2. In the child, run execl - this will transform the child process into the exec'd one, returning -1 if it fails, setting errno appropriately and not returning if it succeeds since the exec'd process will take over the child's PID.
  3. Whichever process is to be the database/server, it will open a socket on the appropriate TCP/IP port, accept connections, listen for connections, and when it gets a connection request use the select function to wait for data to arrive. Usually this is done with a timer so that if no data (requests) come in within a set period, it will go back to checking for other connections (assuming more than one client can connect to the server). If you only will accept the one connection, and that from the parent, then the timer is not required. Alternatively, you can fork a thread to process incoming requests for each client, but that is a more complex model.
  4. The client process will create a socket and connect to the specified port, and once it gets the connection, it can then send request data to the server and get the reply data in turn.
  5. When the server's select call indicates there is data from the client, it can read the request, process it, and return anything it wants to the client.

Since this looks like homework, I am not going to give you code. Write it using the general instructions I have provided, and I will, time willing, critique it for you. FWIW, I have been doing this stuff for about 30 years.

You may also look into the popen call. popen allows you to spawn a process and communicate with that process without all of the bookkeeping of the fork, exec, and pipe calls.

Here is some very primitive code that will be the basis, a lot of the printf are testing lines. Can anyone spot why my child wont read the right thing?

//parent
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>

int main (int argc, char **argv, char **envp)
{
     int err, status;
     int toChild[2];
     char * param;
     err = pipe(toChild);
     if(err==-1)
     {
            printf("error creating pipe");
     }
     /* Create a child process. */
     err = fork();
     if (err == -1)
    {
        printf ("parent: fork failed, errno = %d\n", errno);
        exit (1);
     }
     else if ( err == 0 )
    { /* in child */
         close (toChild[1]);
         printf("\n%d\n", toChild[0]);
         printf("in child");
         sprintf(param, "%d", toChild[0]);
         printf("\nparam: %s\n", param);
        printf("in child");
         err = execl("./child", param, 0);

        if ( err == -1 )
        {
            printf ("parent: execl failed, errno = %d\n", errno);
            exit (2);
         }
     }
    else
    {
     close (toChild[0]);
     write(toChild[1], "cat", sizeof("cat"));
     /* Parent terminates after displaying the process id of child. */
     waitpid(-1, &status, 0);
     printf ("parent: child created with pid = %d\n", err);
    }
    }

and

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
int main (int argc, char **argv) {
int pid;

 pid = getpid();
  char * buffer;
  printf("\nargv: %s\n", argv[0]);
    int toChild = atoi(argv[0]);
    printf("\n%d\n", toChild);
    read(toChild, buffer, 10);

 printf ("child: string from parent = %s, done.\n", buffer);
}

fork() returns the child pid in the parent, and 0 in the child, unless there is an error in which case it returns -1 to the parent. Then you need to run execl() in the child. Re-read my description of the process.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.