fork and execvp method

I'm newbie in Unix but my homework is to build a shell to execute the built-in command of Unix such as ls,rm etc...I have to use fork() to create a child process and this child process will execute the command which user input from command line with execvp() method.But i'm confused about the purpose of these two method.I just know that the fork() method when invoked will create a copy of the parent process but what do parent process and child process mean?How to distinguish them?I dont know what happen behind?Can anyone give me a clear,simple example and explain how it works?Thank you very much
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Some of the best examples can be found in "Advanced Programming in the Unix Environment" from Stevens
A book about where on example is shell development on Linux is in Linux Application Development Michael K. Johnson

I guess you know that one base unit of execution on Unices is the idea of an Process, the other unit are threads. howerver Processes are "stand-alone" programs. Processes can spawn other processes this are then called childs. the first process ever created on Unix have the name init. fork does exactly thatit creates a new process.

Now comes the role of fork. It forks, and well fork is unusual it returns one time in the parent another time in the parent. And you have to check the return value of fork to figure out in which you are, another thing is that 0 as return value indicates a failure.

the Parent and the child do share nearly everything. In fact you can say you could not tell them apart. But of course it would be bad if the child changes something in the environment of the parent, just imagine you set  crucial pointer to NULL in th child and after that access it in the parent. Now execxxx comes into play it is used to "clean" up the environment for the child after that they are divided and a child can (that's of course not fully true) not mess up the parent process any longer....

I think this process stuff is not really easy to understand, but I expec that your teacher has given you at least some ideas. Now you just have to write few programs to see the development side. After a few rounds it's probably "easy".

Duncan RoeSoftware DeveloperCommented:
First of all, you should read the man pages for fork & execvp:
   man fork
   man execvp
fork() is an operating system call, so is in section 2 of the manual. Hence FORK(2) at the head of the man page.
execvp() is one of a number of library wrappers for the execve() operating system call, hence the header EXEC(3).
execvp() is easier to use than execve() for at least 2 reasons:
1. You don't have to specify the environment
2. You don't have to know the absolute pathname of the command (program) you want to execute.

Parent & Child:
The essential function of fork() is to create a new process. The process that invokes fork() is called the parent. The process that fork() creates is called the child.
When Linux boots, there is only 1 process. Linux has a short list of programs that it will try to start as that process: it tries each in turn until one can be started. If none can be started, Linux panics.
The list starts with /sbin/init (the usual case) and includes /bin/sh. (You can override that list at boot time and tell Linux to run a shell, say /bin/bash).
The point is that all processes except process 1 were created as a child of some other process.

How to distinguish them (Parent & Child):
The created (child) process has a copy of the parent's address space, and all the same open files. The operating system will have a separate and different task control block for it however. Like the parent, the child's program counter is at the instruction to which the fork() call returns. The code at this point is almost invariably written to determine if it is now running in the original / parent or new/child process. The way it does this is to examine the return result from the fork() call (this is returned in a register, and the register set of the child is not created the same as that of  the parent - it comes from the task control block).  The child receives a return of zero from fork(); the parent receives a return code greater than zero, being the PID (Process ID) of the child. A typicat action of the parent process now is to wait for the child to finish - or it may simply exit (in wich case process 1 "inherits" the need to wait for the child to finish. I don't know which of these your assignment calls for - I'd guess simply exit initially and next assignment incorporate a wait. BTW ls, rm &c are not "built-in" commands - Unix has no built-in commands. For an operating system to have builtin commands, it must have a builtin command line interpreter, and Unix doesn't have one of those. Prime Computer's Primos had one, ICL's George 3 had one, but not Unix / Linux.

What exec Does
A child process frequently -  as in your case - has little use for the code and general addrsss space it inherited from its parent: it wants to run some other program (like ls, rm &c.).
This is where the exec family of calls (e.g. execvp) comes in. execvp frees all of the current address space (i.e. the "copied" address space of the parent in the case of a freshly created child) and replace it with a freshly loaded copy of the target program with static data areas initialised (including external data) and the program counter pointing to the first instruction. The new program will have the same files open as when execvp() was called, except for any that had their "close on exec" flag set by the caller or its parent. Likely your assignment doesn't include setting "close on exec" - so the new running program will have the same stdin, stdout & stderr as the old: if interactive it will be able to take input from and send output to  the terminal.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
khoamaAuthor Commented:
I installed Ubuntu on VMWare but it can not find manual for fork or execvp. Do i miss any things?Thank you for your comments
Duncan RoeSoftware DeveloperCommented:
It appears some distros no longer install man pages - or perhaps they don't install by default. Possibly you can still install them from your Ubuntu CD, otherwise you can get them off the web or just read them in your browser: e.g. and If you look around, I expect you can get them in man format (not much help if your system doesn't have the man command installed).
I searched for "fork man page" and got the first link (you might want to iss out .au from the URL depending where you are). From that I guessed execvp (I knew in was insection 3 because it's a linrary wrapper to execve() which wis in section 2). You could have jkust googled for it though.
Installing a good implementation of the "man" program is a bit of a dark art - Slackware (that I have) does it very well with most man pages utilising the full screen width (up to 200 chars or so per line). Red Hat man hasn't done that for years, if it ever did.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.