logo
down
shadow

pipe() data is not transferred to child process


pipe() data is not transferred to child process

By : Théo Galy-Fajou
Date : October 20 2020, 06:10 PM
hop of those help? I think you have a slight understanding of what you want and how to do it, but you're not 100% there. If you want to create a pipe, write information there and have another program read it: you start by creating the pipe, create a new process via a fork(), for example, (some people advise against the use of fork: https://www.microsoft.com/en-us/research/publication/a-fork-in-the-road/) and change the file descriptors around via dup2() (and dup()) calls. The process of creating a pipe, forking and doing an exec can be easly done via a popen() call, and I advise that you look into it.
Here's an example that might help you:
code :
char* generate_hash(char* password, char* salt)
{
    char password_plus_salt[MAX_PASSWORD_LEN + SALT_LEN];
    strcpy(password_plus_salt, password);
    strcat(password_plus_salt, salt);

    int fd[2];
    pipe(fd);

    int stdout_save = dup(STDOUT_FILENO);
    dup2(fd[WRITE], STDOUT_FILENO);

    FILE* input = popen("sha256sum", "w");

    fprintf(input, password_plus_salt, "%s");

    FILE* output = fdopen(fd[READ], "r");

    pclose(input);

    char* hash = (char*)malloc(sizeof(char) * (HASH_LEN + 1));
    memset(hash, '\0', (HASH_LEN + 1) * sizeof(char));

    for (int i = 0; i < HASH_LEN; i++) {
        hash[i] = (char)fgetc(output);
    }

    dup2(stdout_save, STDOUT_FILENO);

    return hash;
}
if (fdr < 0 || fdw < 0) { //validation for error
             perror("failed to open input or output files");
             exit(EXIT_FAILURE);
         }
if (fdr < 0) 
{ 
    perror("fdr");
    exit(EXIT_FAILURE);
}
if (fdw < 0) 
{ 
    perror("fdw");
    exit(EXIT_FAILURE);
}



Share : facebook icon twitter icon
How can the child process read stdout from the pipe and the parent process write stdin to the pipe?

How can the child process read stdout from the pipe and the parent process write stdin to the pipe?


By : Mario Chaushki
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , The result of fork() is that one process becomes two (by asexual reproduction). So while it is still the case that exactly one branch of the if/else block will be taken in a process, there are two processes, and one path will be taken by each.
More specifically, look at what fork() returns: a PID to the parent, and 0 to the new child. Apart from that the two processes are almost identical. So the if (cpid == 0) check is a common pattern after fork() so that you can proceed with distinct logic in each process. In your case, that's reading in one process and writing in the other.
Pipe issue about reading data from multi-child process

Pipe issue about reading data from multi-child process


By : Vikrant Singh
Date : March 29 2020, 07:55 AM
I hope this helps . You didn't mention what the problem with your current code is, and the code you showed doesn't compile, so I can only guess that this is an approximation of your real code.
Nevertheless, here's what caught my eye:
code :
for(i=0; i < childCount; i++) {
   kids[i] = fork();
   if(kids[i] < 0){
      //fork fail
   }
   else if(kids[i] == 0) {
     /* child process */
        sum = max + min;//sum and dif are results from each child process
        dif = max - min;
        /* close the unused end of pipe, in this case, the read end */
        close(fd[READ_END]);
        /* write to the pipe */
        write(fd[WRITE_END], &sum, sizeof(sum));
        write(fd[WRITE_END], &dif, sizeof(dif));
        /* close write end */
        close(fd[WRITE_END]);
        exit(0);
   }
   else {
        waitpid(kids[i], &status, 0);
        read(fd[READ_END], &sum, sizeof(float));
        read(fd[READ_END], &dif, sizeof(float));
   }
}

close(fd[READ_END]);
close(fd[WRITE_END]);
Parent process is unable to read data written by child process in pipe

Parent process is unable to read data written by child process in pipe


By : Prinsh Kumar
Date : March 29 2020, 07:55 AM
To fix this issue I see five bugs in this code. I'm going to list them from least to most important. I haven't tried to fix any of the bugs, so there may be more that are hidden behind these.
Why child process pipe is showing in parent process with different pipe details

Why child process pipe is showing in parent process with different pipe details


By : user2292019
Date : March 29 2020, 07:55 AM
To fix this issue You have these things happening.
parent creates pipe 354240 stored in parentpip child1 is forked from parent, inheriting pipe 354240 parent creates pipe 354241 stored in child1pip child1 creates pipe 357693 stored in child1pip child2 is forked from child1, inheriting pipe 354240 and 357693
code :
int child1pip[2];
pipe(child1pip);

if(child1pid==0)
{
if(child1pid==0)
{
     int child1pip[2];
     pipe(child1pip
Child process reads data before data is being written to pipe using parent

Child process reads data before data is being written to pipe using parent


By : user2756110
Date : March 29 2020, 07:55 AM
I wish did fix the issue. The problem is that the filedescriptors are not initialized by the pipe-function. When the read-function is reached, fd[0] is probably an invalid file descriptor. 0 bytes are read, the integers, which are written to stdout are just what was in the uninitialized array parentData. Just add:
code :
pipe(fd);
Related Posts Related Posts :
  • dynamic memory for 2D char array
  • C compiler producing lightweight executeables
  • Cannot Open Shared Object cygmpfr-1.dll
  • How to access a structure member in a function that get it as void* type?
  • Replace for loop with formula
  • how to find if stack increases upwards or downwards?
  • Why does my homespun sizeof operator need a char* cast?
  • Setting exit status when creating core dump
  • Signal Handling in C
  • How to implement B+ Tree for file systems?
  • Stack storage at function call
  • Manipulate the Clipboard in C on Mac OS X
  • Problem with writing a hexadecimal string
  • Why does this code read all ' ' for the anything after the 4th character?
  • GUI for linux c
  • Where do I put all these function-like #defines, in C?
  • Padding in 24-bits rgb bitmap
  • how to acess and change variable of kernel space from user space
  • Can a conforming C implementation #define NULL to be something wacky
  • send() always interrupted by EPIPE
  • Socket Select with empty fd set
  • Coding self similar fractals in computer graphics using C
  • Forking with Pipes
  • sscanf + c99 not working on some platforms?
  • Problem with C function of type char pointer, can someone explain?
  • Macros in C.... please give the solution
  • Unix Piping using Fork and Dup
  • Any difference between these two while loops?
  • Hashtable implementation for C
  • Problems with Linked List in C
  • Stack Allocation in C
  • Random function stuck on one number
  • printf following zeros to a certain degree
  • Measuring CPU clocks consumed by a process
  • How is a CRC32 checksum calculated?
  • Tips on redefining a register bitfield in C
  • Converting a program from c to bash script
  • Static library not included in resulting LLVM executable
  • What is the meaning of "wild pointer" in C?
  • Reading DWORD from binary file
  • Efficient way to find divisibility
  • Whats the fastest, portable way to hash pointers we know are pointer aligned to a fixed size int?
  • Why does my summation program behaves weird?
  • How to determine values saved on the stack?
  • Thread scheduling C
  • C - converting to 2s complement
  • wireless networks c program
  • From where does the uboot get the information about flash, RTC, RAM in uboot/kernel development
  • printf behaviour in C
  • Measure size and way-order of L1 and L2 caches
  • Efficient copy of entire directory
  • Efficient way to create/unpack large bitfields in C?
  • getnameinfo prototype asks for sockaddr not sockaddr_in?
  • Linked List Sorting with Strings In C
  • Typecasting a floating value or using the math.h floor* functions?
  • Problem implementing sorting algorithm in C with an array of structs
  • How to copy a ram_base file to disk efficiently
  • How to fix casting a pointer to an integer?
  • How to round floating point numbers to the nearest integer in C?
  • PIC C - Sending 200 values over USB, but it only sends 25 or so of them
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk