process and fork()

Process is a program in execution. Process execution must progress in sequential fashion. A process includes program counter, stack, and data section. In UNIX, a process is created by fork() system call. Below is the main fork() program based on C language.


int main()
{
  pid_t pid = fork();

  if (pid < 0)
  {
		/*error occured*/
		fprintf(stderr,"error forking childn");
		exit(-1);
  }
  else if (pid == 0)
  {
		/*child process*/
		/* put your application here*/
		exit(0);
  }
  else
  {
		/*parent process*/
		int status;
		/*wait for child process to complete*/
		/*when child process has been completed, */
		/*it will return status 0 */
		wait(&status);
		exit(0);
  }
}

You may download a sample application to count fibonacci sequence that uses fork() system call, 3.5_fork.zip.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s