Thread: Strings and Ints through pipes between processes.

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    1

    Strings and Ints through pipes between processes.

    Hi, New to C. Cannot find an answer to the question, please have patience I an new and trying.
    I have to spawn child processes from the parent, using execl. I need to pass string and int from parent to child, and child will print to screen. I think I am able to get child to read whatever I send, just stuck on how to send it.
    Below is only parts of my logic I am having trouble with, not full program. If more of the program of full one is needed to understand please let me know.

    Please if anyone can help it would be appreciated.

    Code:
    int status;
    //Declare pipe arrays.
    int pfd[2];
    
    
    
    
    int main()
    {
    //Declare descriptors to write to in pipes
    int pipeFileDescript1;
    char pipeFileDescript2[10];
    
    
    
    
    //Set up pipes before fork
    //1st pipe to get PID information to display, check started correctly
        if (pipe(pfd) < 0)
        {
            perror("pipe");
    //Exit negative 1 to show exit with incorrect operation.
            exit(-1);
        }
    
    
    //Set string for Write end of pipes.
    sprintf(pipeFileDescript2, "%d", pfd1[WRITE]);
    
        for (int count = 0; count < NUMBER_OF_CHILDREN; count++)
        {
    
    
            pid_t pid = fork();
    
    
    //Functions for Child process
            if (pid == 0)
            {
    //First Child created to display all communications
                if (count == 0)
                {
    //Write child process ID to display
                    char *str = ("Drone Simulator Started with PID %i \n", pid);
                    write(pfd[WRITE], str, strlen(str));
    //Close write side of pipe between Display and parent/Start up.
                    close(pfd[WRITE]);
                   execl("../Display/Display.exe", "Display", (char *)NULL);
                }
    
    
    //Close pipes to parent as not needed..
                    close(pfd[READ]);
                    close(pfd1[WRITE]);
                    close(pfd1[READ]);
    
    
            wait(NULL);
        }
        for (int count = 0; count < NUMBER_OF_CHILDREN; count++)
        {   int status;
    //Get status of the Child processors. and print results.
            pid_t pid = wait(&status);
            pipeFileDescript1 = ("Child process with PID %i terminated with status %d", pid, status);
            write(pfd[WRITE], &pipeFileDescript1, sizeof(pipeFileDescript1));
            close(pfd[WRITE]);
        }
       return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Before the exec call, do this
    Code:
    dup2(pfd[READ], STDIN_FILENO);
    This will join the appropriate end of the pipe to the stdin of the child process.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Edge Detection done using processes and pipes
    By Bakenshake in forum C Programming
    Replies: 10
    Last Post: 12-04-2014, 09:14 AM
  2. Pipes between child processes
    By nikey007 in forum C Programming
    Replies: 2
    Last Post: 05-06-2014, 10:56 AM
  3. Replies: 4
    Last Post: 11-17-2013, 01:30 PM
  4. Replies: 4
    Last Post: 09-21-2003, 03:00 PM
  5. Strings into Ints
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2002, 04:07 PM

Tags for this Thread