Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Creating child processes
hi,
i'm quite new to programming for various unix flavours, so excuse me
if i ask something dumb. i have done some research on my own, but
haven't come to a solution, so i hope someone here can help me out :)

i'm using C++ and, if possible, i'd like to stick to the C or C++
standard libraries or some widely supported libraries and standards,
such as POSIX, so i don't have too much trouble porting to other
platforms or compilers.
so, what i need to do is create child processes that are not
necessarily the same executable as the parent process. for what i have
found out, i need to fork another instance of the callee and use
execvp or something similar. this is where i have my first question:
fork copies nearly every resources of the callee, such as file
descriptors, doesn't it? will these resources be closed automatically
when i call a function of the exec* family? does the child process
write to stdout and stderr and read from stdin just as the parent
process? how do i wait for the child to complete and get its return
code?

somewhere else in my code, i also need to create a child process
similar as above, but there are additional requirements: i need to set
the environment of the child process and redirect the stdin, stdout
and stderr streams in the child so i can read from/write to them from
the parent. i guess i have to do something with pipes, but i haven't
figured out how to do it exactly.


thank you in advance :)

Report this thread to moderator Post Follow-up to this message
Old Post
pleexed@gmail.com
04-03-08 11:27 AM


Re: Creating child processes
On Apr 3, 1:49 am, plee...@gmail.com wrote:

> fork copies nearly every resources of the callee, such as file
> descriptors, doesn't it?

Yes.

> will these resources be closed automatically
> when i call a function of the exec* family?

It depends on exactly what resources you are talking about and what
you mean by "closed". The short answer is that almost all resources
will except file descriptors that have the FD_CLOEXEC flag cleared.

> does the child process
> write to stdout and stderr and read from stdin just as the parent
> process?

Typically, these descriptors are set to remain open across an 'exec',
so the new process will have the sane stdout, stderr, and stdin as the
parent. However, you can certainly change them *after* the 'fork' and
*before* the 'exec' so that the other executable has whatever stdout,
stderr, and stdin you want it to have.

> how do i wait for the child to complete and get its return
> code?

'man wait', 'man waitpid'

> somewhere else in my code, i also need to create a child process
> similar as above, but there are additional requirements: i need to set
> the environment of the child process and redirect the stdin, stdout
> and stderr streams in the child so i can read from/write to them from
> the parent. i guess i have to do something with pipes, but i haven't
> figured out how to do it exactly.

After the 'fork' but before the 'exec' you can manipulate the
environment, change stdin, stdout, and stderr, and otherwise monkey
around with the context the new process gets. You can replace the
standard I/O descriptors with pipes if you like and then talk to the
child process from the parent using the other ends of those pipes.

DS

Report this thread to moderator Post Follow-up to this message
Old Post
David Schwartz
04-03-08 11:27 AM


Re: Creating child processes
pleexed@gmail.com wrote in news:757bf237-28f3-4411-954c-
bca64942194a@z38g2000hsc.googlegroups.com:

> hi,
> i'm quite new to programming for various unix flavours, so excuse me
> if i ask something dumb. i have done some research on my own, but
> haven't come to a solution, so i hope someone here can help me out :)

<snip>

What UNIX text are your reading that doesn't cover fork() and exec()?

Report this thread to moderator Post Follow-up to this message
Old Post
Lloyd Bonafide
04-03-08 01:41 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:01 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.