why wrong file descriptor ??

This is a discussion on why wrong file descriptor ?? within the C Programming forums, part of the General Programming Boards category; hi friends, its all code, but not getting why it is telling wrong descriptor. Code: 1 //redirecting output of ls ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    17

    Question why wrong file descriptor ??

    hi friends,
    its all code, but not getting why it is telling wrong descriptor.
    Code:
          1 //redirecting output of ls -l command to wc, with help of tmp file
          2
          3 #include<stdio.h>
          4 #include<fcntl.h>
          5
          6
          7 void main()
          8     {
          9         int fd ;
         10         switch(fork())
         11             {
         12                 default : break ;
         13
         14                 case -1 : fprintf(stderr, "system error\n") ;
         15                           exit(1) ;
         16                 case 0 :  if((fd = open("tmp", O_CREAT|O_WRONLY,0600\
         17                               )) == -1)
         18                             {
         19                                 perror("open") ;
         20                                 printf("Couldn't create tmp file\n") ;
         21                                 exit(2) ;
         22                             }
         23
         24                          close(1) ; // closing 1 row
         25                          if(dup(fd) != 1)
         26                             {
         27                                 fprintf(stderr, " Dup error...");
         28                                 exit(3) ;
         29
         30                             }
         31                         close(fd) ; // closing the extra file des
         32                         execl("/bin/ls","ls","-l", NULL) ;
         33                         fprintf(stderr,"error exit 4") ;
         34             } // end of switch
         35
         36         switch(fork()) // this fork for wc
         37             {
         38                 case -1 : // if not able to fork
         39                          break;
         40                 case 0 :if((fd = open("tmp", O_CREAT|O_WRONLY,0600\
         41                               )) == -1)
         42                             {
         43                                 perror("open") ;
         44                                 printf("Couldn't create tmp file\n") ;
         45                                 exit(2) ;
         46                             }
         47
         48                         close(0); // freeing the 0th row in file table
         49
         50                         //printf("fd = %d\n", dup(fd)) ;
         51                         //close(0) ;
         52                         if(dup(fd) != 0)
         53                             {
         54                                 fprintf(stderr," wc input error(dup)");
         55                                 exit(4) ;
         56                             }
         57
         58                 //      close(fd) ;
         59                         execl("/usr/bin/wc", "wc",NULL) ;
         60                         fprintf(stderr, "error wc exit 5");
         61                         exit(5) ;
         62             }// end of wc switch
         63
         64
         65
         66         wait(NULL) ;
         67         exit(0) ;
         68     }// end of main

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > void main()
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    > fd = open("tmp", O_CREAT|O_WRONLY
    Both of them try and create a write_only file.
    Unfortunately, one of them is expecting to be able to read the file.

    Your understanding of fork() is fundamentally broken. Everything is asynchronous after you call a fork(), so it's quite easy for the "wc" to happen before the "ls" and for you to end up with a problem.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    man 3 popen


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21