Re: Is it possible to use file descriptor after main returns

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Arun, i suggest that if you are using this for IPC then , better use pipes.

And if you really want to use fd in another application then
pass the file descriptor to exec after doing a fork.

something like this.

in application_1

ret = fork();

if(ret == 0){
sprintf(buff, "%d", fd_1);
(void)exec("application_2","application_2", buff, (char*)0);
}

.......
.........

in application_2

int fd_1;
sscanf(argv[1],"%d", &fd_1);

read(fd_1, buff, BUFFSIZE);

....
.....

I hope that helps.


On Wed, Apr 22, 2009 at 7:12 PM, Greg Freemyer <greg.freemyer@xxxxxxxxx> wrote:
> On Wed, Apr 22, 2009 at 5:12 AM, Arun raj <arun.raj.linux@xxxxxxxxx> wrote:
>>
>> On Wed, Apr 22, 2009 at 5:56 PM, pradeep singh <pradeep.rautela@xxxxxxxxx>
>> wrote:
>>>
>>> On Wed, Apr 22, 2009 at 1:33 PM, Arun raj <arun.raj.linux@xxxxxxxxx>
>>> wrote:
>>> > Hello All,
>>> >
>>> > I  want to use the file descriptor opened by  application_1 in another
>>> > application_2, i.e
>>> >
>>> > ./application_1 :
>>> > main()
>>> > {
>>> >     fd_1 = open( file );
>>> >     return fd_1;
>>> > }
>>> >
>>> > ./application_2  fd_1
>>> > main()
>>> > {
>>> >     ret = read( fd_1 );
>>> >     return fd;
>>> > }
>>> >
>>> > My concern is once application_1 main returns all open fds will be
>>> > closed
>>> > right ?
>>>
>>> Right. So better try to fork a new process or create a new thread,
>>> that way you may share the same fd between them.
>>> Once you have the fd, do whatever you want to do with it.
>>>
>>> Si this what you want ?
>>>
>>> HTH
>>> >
>>> > Thanks in advance .
>>> >
>>> > Thanks,
>>> > A.R
>>> >
>>> >
>>>
>>>
>>>
>>> --
>>> Pradeep
> <moved below to eliminate top post>
>> Thanks a lot pradip.
>>
>> I would like to know  is  there any method/mechanism/logic to share
>> fd opened by application_1 which can be used in application_2.
>>
> You mean in addition to Pradeep's recommendation of "fork() and
> exec()" and multithreading?
>
> As Sandeep said you can pass an open file descriptor across a pipe.
>
> Also, system() is a specialized version of fork & exec that shares
> only stdin, stdout, stderr.
>
> So you have at least 4 ways to do it,
>
> FYI: I'm not sure why this discussion is happening on this mailing
> list.  This list is about modifying the kernel, not making calls into
> it.
>
> Greg
> --
> Greg Freemyer
> Head of EDD Tape Extraction and Processing team
> Litigation Triage Solutions Specialist
> http://www.linkedin.com/in/gregfreemyer
> First 99 Days Litigation White Paper -
> http://www.norcrossgroup.com/forms/whitepapers/99%20Days%20whitepaper.pdf
>
> The Norcross Group
> The Intersection of Evidence & Technology
> http://www.norcrossgroup.com
>
> --
> To unsubscribe from this list: send an email with
> "unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
> Please read the FAQ at http://kernelnewbies.org/FAQ
>
>

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux