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

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

 



On Thu, Apr 23, 2009 at 2:20 AM, Greg Freemyer <greg.freemyer@xxxxxxxxx> wrote:
> On Wed, Apr 22, 2009 at 3:38 PM, Rohit Sharma <imreckless@xxxxxxxxx> wrote:
>> 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.
>
> Decided to back and refresh my memory.  I was not thinking of passing
> a FD via a pipe.  I was thinking of doing it through sendmsg().
>

Well, this might incur some unnecessary overheads of sockets and their handlers.
The point is to simply pass the file descriptor across process.

I believe using sendmsg might be more useful if you have other needs to.
Else, IMO, using a simple fork and pipe should solve the issue easily.

A classical example.

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main(void)
{
        int     fd[2], nbytes;
        pid_t   childpid;
        char    string[] = "Hello, world!\n";
        char    readbuffer[80];

        pipe(fd);

        if((childpid = fork()) == -1)
        {
                perror("fork");
                exit(1);
        }

        if(childpid == 0)
        {
                /* Child process closes up input side of pipe */
                close(fd[0]);

                /* Send "string" through the output side of pipe */
                write(fd[1], string, (strlen(string)+1));
                exit(0);
        }
        else
        {
                /* Parent process closes up output side of pipe */
                close(fd[1]);

                /* Read in a string from the pipe */
                nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
                printf("Received string: %s", readbuffer);
        }

        return(0);
}


> I don't think I ever did that in Linux, so it may or may not work with
> a linux kernel.
>
> See http://archives.neohapsis.com/archives/postfix/2000-09/1476.html
> for a write-up of how it worked in at least some environments.  I
> don't even remember what flavor of Unix I was using when I did the
> sendmsg() trick to pass a FD.
>
> 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
>
>



-- 
Regards,
Sandeep.





 	
“To learn is to change. Education is a process that changes the learner.”

--
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