Skip to main content

Thread: doubt in reading from stdout


hey,
tried searching , have written many sources put together.

code:
#include <stdio.h>  #include <unistd.h>  #include <sys/wait.h>  #include <string.h>    #define len 512  int main()  {          char buffer[len];          int fd[2];          pipe(fd);            pid_t pid = fork();            /*i understand child writing*/          if(pid == 0)          {                  dup2(fd[1], stdout_fileno);                  close(fd[0]);                    //execlp("ls", "ls", null);                  printf("\nhello world");          }            /*i understand parent reading*/          else          {                  dup2(fd[0], stdin_fileno);                  close(fd[1]);                    while (fgets(buffer, len, stdin))                  {                      printf("%s", buffer);                  }                  waitpid(pid, null, 0);          }            return 0;  }
i tried reading man pages a)pipe, b)fork, c)dup/dup2 , d)waitpid. still have couple of questions.

1.i understand pipe(fd) creates array of 2 fd's fd[0] used reading , fd[1] writing. but in code, mention has read stdout? , not other file in hard disk ?

2.in dup2(fd[1], stdout_fileno);, oldfd fd[1] , newfd stdout_fileno right ?(from man pages). don't this. stdout_fileno ? it's not declared. don't line, have atleast 4-5 doubts in line, i'll wait reply i'll know ask.

3.
code:
while (fgets(buffer, len, stdin))                  {                      printf("%s", buffer);                  }
you reading in loop , going on printing ? didn't understand.

4.what watitpid ? waits until state of parent changes ? changes ?

i'm sorry asking 4 doubts in around 10 lines of code. first try @ ipc.

thanks.
please advise.

you trying lot of new things @ once. before try use fork(), pipe(), dup2(), etc. in single program, use them each in separate program , experiment them until understand do.


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk doubt in reading from stdout


Ubuntu

Comments

Popular posts from this blog

Could not place because the source rectangle is empty

Thread: Using smartcard reader with vpnc

Adobe Font Folio 7.0 or just 7?