Code Comments
Programming Forum and web based access to our favorite programming groups.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 :)
Post Follow-up to this messageOn 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
Post Follow-up to this messagepleexed@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()?
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.