C++ character array not being read right through pipe -


i'm trying check cmd variable set "listall" isn't when try printing out.

#include <stdio.h> #include <unistd.h> #include <cstring> #include <stdlib.h> #include <iostream> #include <sys/wait.h>  int main(int argc, char **argv) {        pid_t cpid = fork();     int p2c[2];     int c2p[2];     pipe(p2c);     pipe(c2p);      char cmd[50];     char* listofprocesses = new char[1024];      if (cpid == 0)     {         ...         read(p2c[0], cmd, 50);           printf("%s\n", cmd);         if(strcmp(cmd,"listall") == 0)         {             //printf("executing command: %s", cmd);             write(c2p[1], getlistofprocesses("ps -ax -o pid,cmd"), 1024);             ...         }     }     else if (cpid > 0)     {         ...         write(p2c[1], "listall", 50);         wait(null);         read(c2p[0], listofprocesses,1024);         ...     }     else     {         // fork failed         printf("forking failed!\n");         exit(1);     }     return 0; } 

what mini box symbol 00 @ top , 01 or 02 @ bottom. tried pasting symbol here doesn't show.

you create 4 pipes: 2 in parent process , 2 in child process.

create pipes before forking! fork, check whether in parent process or in child process.

that way have 2 pipes, both processes know these pipes , can communicate reading or writing appropriate file descriptors of pipes.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -