C program on how to use "dup" system call
neelkanth_surekha#cat dup.c
#include
"fcntl.h"
#include
<stdio.h>
#include
<stdlib.h>
#include
<unistd.h>
#include
<sys/types.h>
int main()
{
int i,j;
char buf1[512],buf2[512];
i = open("neel", O_RDONLY);
/* by duplicating i, we can also use j to read */
j = dup(i);
read(i, buf1, sizeof(buf1));
lseek(i, 0,SEEK_SET);
read(j, buf2, sizeof(buf2));
printf("buf 1:%s buf 2: %s", buf1, buf2);
close(i);
read(j, buf2, sizeof(buf2));
printf("buf 2: %s", buf2);
return 0;
}
Output
neelkanth_surekha#./a.out
buf 1:dfasdfsadfsadfsadfasf
buf 2: dfasdfsadfsadfsadfasf
buf 2: dfasdfsadfsadfsadfasf