fork and execvp method

Posted on 2007-03-23
Medium Priority
Last Modified: 2013-12-26
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
Question by:khoama
  • 2
LVL 24

Assisted Solution

fridom earned 450 total points
ID: 18784612
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".

LVL 35

Accepted Solution

Duncan Roe earned 1050 total points
ID: 18788012
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.

Author Comment

ID: 18791364
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
LVL 35

Assisted Solution

by:Duncan Roe
Duncan Roe earned 1050 total points
ID: 18791629
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. http://www.die.net/doc/linux/man/man2/fork.2.html and http://www.die.net/doc/linux/man/man3/execvp.3.html. 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 http://www.google.com.au/linux 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.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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.

Join & Write a Comment

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In the video, one can understand the process of resizing images in single or bulk. Kernel Bulk Image Resizer is an easy to use tool for resizing large number of images. One can add and resize multiple images with this tool in single go. The video sh…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question