09-23-05 10:55 PM
"au.faisal" <au.faisal@gmail.com> wrote:
# David Schwartz wrote:
# > "au.faisal" <au.faisal@gmail.com> wrote in message
# > news:1127464048.702596.5190@g44g2000cwa.googlegroups.com...
# >
# > > I am having a problem with my school assignment. I have to write
# > > programm which uses fork to create a new process and then replace with
# > > another process by using execvp. now up to this point this is fine. bu
t
# > > the problem is the replaced function will take value in command line
# > > argument to use it as its own status.
# > >
# > > what i did is following:
# > >
# > > char *file_name = "child"; /*child is the child programm*/
# > > char *file_name1[] = {"child"; NULL};
This is the argument vector. By convention the first argument is
the name or path of execked function, but it is not required by
the kernel. To pass additional arguments, just include more strings.
char *file_name1[] = {"child","argv[1]","argv[2]",...,0};
You can also dynamically allocate the argv array with malloc,
compute array values, etc, using the usual miscellanous C
functiions.
# > > if((pid = fork()) <0)
# > > {
# > > perror("ERROR");
# > > exit(EXIT_FAILURE);
# > > }
You probably want to distinguish three cases
pid < 0, fork error
pid > 0, this is the parent process
and pid is the child process
pid = 0, this is the child process
# > 2) You call 'execvp' in both the parent and the child!
#
# I didn't understand why I need to call execvp in child program. Because
exec functions do not create new processes. You use fork() for that.
exec functions release the memory (but not process information),
and then load a new program and begin it in the same process.
if your parent is going to immediately exit after the fork,
it has no useful work to do managing pipes or waiting for status
after the fork, you can just exec in the original process and
not bother to fork. In a shell if you do something like
command p1 p2 ... pn
the shell forks; the child execs command while the parent waits
for the child; the parent shell resumes its dialog when the child
completes. If instead you do
exec command p1 p2 ... pn
the shell replaces itself with command runs to completion.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
Don't say anything. Especially you.
[ Post a follow-up to this message ]
|