Sponsored Content
Full Discussion: dup()
Top Forums Programming dup() Post 302259460 by joey on Tuesday 18th of November 2008 07:02:44 AM
Old 11-18-2008
MySQL

I have tried dup by itself in a little program its working. But when am using it in my original program its hanging...

Code:
// included everything needed. sys/types.h fcntl.h ...
int in, out, out1;

int doable(char Buffer[], char *args[], int *back_ground)
{
         //------watever here...

           // length is length of the Buffer.
           for(i = 0; i<length;i++)
           {
                   switch(Buffer[i])
                   {
                          case '>':
                              argument[0] = &Buffer[i+2];//consider &buffer[i+1] space . the user will write ls > myfile
                              out = open (argument[0] , O_WRONLY 
O_CREAT);
                              dup2(out,1);
                              i = length+1;
                   }
              }
}



int main()

{
int x1 = 0;
int back_ground = 0;
while(1)
{
      x1 = doable(Buffer, arguments, &back_ground)
      child = fork()
      if(child == 0)
      {      // i call execvp
       }
      else
       {
               // if backgroun or not.and i do the wait..and return if no error from execvp.(i       use       waitpid)
        }

// after both parent & child finish
       close(out);
      printf("new command line");
}}


The program is working with me without adding the dup and stuff.But after I added the dup & stuff..Whenever I run command ls > myfile ...the program hangs..and in my file nthg is printed...

Last edited by joey; 11-18-2008 at 08:09 AM..
 

7 More Discussions You Might Find Interesting

1. Programming

dup()

Would anyone be so kind to explain to me the function of dup() in UNIX? As far as I am concerned, it duplicates a file descriptor. Under what circumstances would we need to duplicate a file descriptor in a UNIX environment? Thank you. vinchen (3 Replies)
Discussion started by: vinchen
3 Replies

2. Programming

fork() and dup()

I have met this code: switch(fork()) { case 0: close(1); dup(p); close(p); close(p); execvp(<whatever>); perror("Exec failed"); } Can anyone tell me what this piece of code does? Thx alot.. (1 Reply)
Discussion started by: AkumaTay
1 Replies

3. Programming

Understanding the purpose of dup/dup2

I'm having difficulty understanding the purposes of using dup/dup2 when involving forks. for example, if we call fork() once, that is, we are creating a child process. In what cases would we need to use dup or dup2 to duplicate the file descriptors for standard output and standard error? What... (1 Reply)
Discussion started by: Yifan_Guo
1 Replies

4. Red Hat

ping error (DUP!)

Ntop is running on redhat. But It gives DUP! error while pinging to any places I dont know why DUP! error is occured. # ping google.com PING google.com (74.125.39.147) 56(84) bytes of data. 64 bytes from fx-in-f147.1e100.net (74.125.39.147): icmp_seq=1 ttl=44 time=54.1 ms 64 bytes from... (6 Replies)
Discussion started by: getrue
6 Replies

5. Shell Programming and Scripting

Identify duplicates and update the last 2 digits to 0 for both the Orig and Dup

Hi, I have a requirement where I have to identify duplicates from a file based on the first 6 chars (It is fixed width file of 12 chars length) and whenever a duplicate row is found, its original and duplicate row's last 2 chars should be updated to all 0's if they are not same. (I mean last 2... (3 Replies)
Discussion started by: farawaydsky
3 Replies

6. Shell Programming and Scripting

How to count dup records in file?

Hi Gurus, I need to count the duplicate records in file file abc abc def ghi ghi jkl I want to get below result: abc ,2 abc, 2 def ,1 ghi ,2 ghi, 2 jkl ,1 or abc ,2 def ,1 (3 Replies)
Discussion started by: ken6503
3 Replies

7. UNIX and Linux Applications

Deja-dup make my / full. So I cannot restore my back up

The problematic directory is the following: /root/.cache/deja-dup This directory grows until my "/" is full and then the restoring activity fails. I already tried to create a symbolic link with origin another partition where I have more space. However during the restoring activity ... (4 Replies)
Discussion started by: puertas12
4 Replies
DUP(2)							      BSD System Calls Manual							    DUP(2)

NAME
dup, dup2 -- duplicate an existing file descriptor LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <unistd.h> int dup(int oldd); int dup2(int oldd, int newd); DESCRIPTION
The dup() system call duplicates an existing object descriptor and returns its value to the calling process (newd = dup(oldd)). The argument oldd is a small non-negative integer index in the per-process descriptor table. The new descriptor returned by the call is the lowest num- bered descriptor currently not in use by the process. The object referenced by the descriptor does not distinguish between oldd and newd in any way. Thus if newd and oldd are duplicate refer- ences to an open file, read(2), write(2) and lseek(2) calls all move a single pointer into the file, and append mode, non-blocking I/O and asynchronous I/O options are shared between the references. If a separate pointer into the file is desired, a different object reference to the file must be obtained by issuing an additional open(2) system call. The close-on-exec flag on the new file descriptor is unset. In dup2(), the value of the new descriptor newd is specified. If this descriptor is already in use and oldd != newd, the descriptor is first deallocated as if the close(2) system call had been used. If oldd is not a valid descriptor, then newd is not closed. If oldd == newd and oldd is a valid descriptor, then dup2() is successful, and does nothing. RETURN VALUES
These calls return the new file descriptor if successful; otherwise the value -1 is returned and the external variable errno is set to indi- cate the cause of the error. ERRORS
The dup() system call fails if: [EBADF] The oldd argument is not a valid active descriptor [EMFILE] Too many descriptors are active. The dup2() system call fails if: [EBADF] The oldd argument is not a valid active descriptor or the newd argument is negative or exceeds the maximum allowable descriptor number SEE ALSO
accept(2), close(2), fcntl(2), getdtablesize(2), open(2), pipe(2), socket(2), socketpair(2), dup3(3) STANDARDS
The dup() and dup2() system calls are expected to conform to ISO/IEC 9945-1:1990 (``POSIX.1''). HISTORY
The dup() and dup2() functions appeared in Version 7 AT&T UNIX. BSD
June 1, 2013 BSD
All times are GMT -4. The time now is 03:31 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy