#Understanding pipes and dups

26 messages · Page 1 of 1 (latest)

scarlet dirge
#

Can anyone help explain to me how I can get such an output

Process 5 begins
Process 4 begins
Process 3 begins
Process 2 begins
Process 1 begins
Process 1 ends
Process 2 ends
Process 3 ends
Process 4 ends
Process 5 ends

Using pipes and forks? I'm having a hard time grasping this

Here's a bit of a description:
Create the child, passing prcNum-1 to it. Use prcNum
as the identifier of this process. Also, read the
messages from the reading end of the pipe and sends it to
the standard output (df 1). Finish when no data can
be read from the pipe.

willow copperBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

scenic trout
#

pid_t pidofchild = fork() to create a child proces

#

waitpid(pid,&reason,0) to wait for a pid

#

or wait(&reason) to wait for any of them

#

to user fork()

#

use a switch(returnValue)

#

case -1 : error

#

case 0 : code for the child

#

default : code for the parent (wait)

#

if you are using pipes

#

you do int[2] pi;

#

pipe(pi) before the fork so that its known by the parent and child

#

to make the pipe an output for exemple

#

you can do dup2(STDOUT_FILENO,pi[0])

#

write(pi[0],something,size) or pintf(something)

#

and it will output

#

But all of that is only on POSIX systems (Linux) and not windows

scarlet dirge
#

Alright I'll give it a shot thanks

willow copperBOT
#

@scarlet dirge Has your question been resolved? If so, run !solved :)

scarlet dirge
# scenic trout case -1 : error

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#define BUFFER_SIZE 128
#define READ_END 0
#define WRITE_END 1

void createChildAndRead (int);
  


int main (int ac, char **av)
{
 int processNumber; 

 if (ac == 2)
 {
 if (sscanf (av [1], "%d", &processNumber)== 1)
 {
    createChildAndRead(processNumber);
 }
    else fprintf(stderr, "Cannot translate argument\n");
 }
    else fprintf(stderr, "Invalid arguments\n");
    return (0);
}
void createChildAndRead(int prcNum)
{

    int fd[2];
    int processCount;
    char write_msg[BUFFER_SIZE];
    char read_msg[BUFFER_SIZE];



// make pipe
    if (pipe(fd) == -1){
        fprintf(stderr, "Error in pipe");
    }

    if (prcNum == 1){
        sprintf("Process %d starting\n", prcNum);
        sleep(5);
        sprintf("Process %d ending", prcNum);
    }
// fork
    else if (prcNum > 1){
        processCount = fork();
    }


// setup the pipe on both parent and child process

    if (processCount < 0){
        fprtinf(stderr, "Error in fork");
    }


    if (processCount == 0) { //child
        // close write fd on child
        close(fd[WRITE_END]);
        /* read from the pipe */
        // assign read fd to standard input on child
        sprintf(read_msg, "Process %d starting", prcNum);

        dup2(fd[READ_END], (prcNum+25));

        close(fd[READ_END]);

        sprintf(read_msg,"%d", prcNum-1);
 
        /* close the write end of the pipe */
        close(fd[READ_END]);

    }
    else { /* parent process */



            // close read fd on parent
        close(fd[READ_END]);
        // write to the pipe 
        // assign write fd to standard output on parent
        sprintf(write_msg, "Process %d starting", prcNum);
        write(fd[WRITE_END], write_msg, sizeof(read_msg)+1);
        /* close the write end of the pipe */
        close(fd[WRITE_END]);
    }


// parent -> write
// child -> start from 1. (unless it is the last child)



}






#

This is what I have so far

#

If anyone could help me, would be great

scenic trout
#
pid_t child_pid = fork()
switch(child_pid)
{
    case -1:
        exit(EXIT_FAILURE);
    case 0: //child
        //child code
        //exit() if you don't it will bleed into the parent code
    default: //father
        int reason;
        wait(&reason);
        if(WIFEXITED(reason))
        {
            printf("child exited wxhit code : %d",WEXITCODE(reason))
        }
}``` might not be 100% working, done from the top of my head