LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-21-2012, 06:06 AM   #1
hari_sahaya
LQ Newbie
 
Registered: Jun 2012
Posts: 5

Rep: Reputation: Disabled
wait call in linux


Hi folks,

I was trying to understand fork() call in linux.

pid_t childpid;

childpid = fork();

Here parent process gets child process's PID in childpid and child process gets childpid's value as zero.

Then unless parent executes wait() call, child process doesn't exit. Can someone elaborate the sequence in which child and parent process execute ?

Thanks,
Hari.
 
Old 06-21-2012, 09:12 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
This stuff can get a little complicated but here goes.

In the parent, fork returns the process id (PID) of the child (a number between 1 and 30,000 inclusive), and in the child, fork returns zero:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

void	main	(void)
{
	if (fork () == 0)
		(void) fprintf (stdout, "this is the child\n");
	else
		(void) fprintf (stdout, "this is the parent\n");
	exit (EXIT_SUCCESS);
}
Executing this returns, on my system (and most likely on yours)
Code:
this is the parent
this is the child
The parent come out first, then the child, but you're not guaranteed that will be true for every UNIX/Linux system (compile the above and run it to see).

You can control the execution of child processes by calling wait in the parent; wait forces the parent to suspend execution until the child is finished:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
#include <sys/types.h>

void	main	(void)
{
	if (fork () == 0)
		(void) fprintf (stdout, "this is the child\n");
	else {
		(void) wait ((int *) NULL);
		(void) fprintf (stdout, "this is the parent\n");
	}
	exit (EXIT_SUCCESS);
}
When executed,
Code:
this is the child
this is the parent
Note that wait takes an integer pointer as an argument; the exit status of the child is placed in the location the argument points to. If a null pointer is supplied, as in the above, the exit status is not stored.

The following may be overkill, but provides a look at how to use fork and exec.

Where fork becomes extremely handy is using the exec routines, usually called after a call to fork. The following is a simple command interpreter that uses execlp to execute command typed by the user:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <wait.h>
#include <sys/types.h>

void	main	(void)
{
	char	line [BUFSIZ];
	int	process;

	for ( ; ; ) {
		(void) fprintf (stderr, "cmd: ");
		if (gets (line) == (char *) NULL)
			exit (EXIT_FAILURE);
		/*	create new process	*/
		if ((process = fork ()) > 0)
			(void) wait ((int *) NULL);
		else if (process == 0) {	/* child	*/
			/*	execute program			*/
			(void) execlp (line, line, NULL);
			/*	some problem if exec returns	*/
			(void) fprintf (stderr, "can't execute %s\n", line);
			exit (errno);
		} else if (process == -1) {	/* can't create	*/
			(void) fprintf (stderr, "can't fork\n");
			exit (errno);
		}
	}
}
You compile this and execute it to type commands (like, oh, ls or something) until you enter ^D to exit the program. Kinda cute.

Hope this helps some.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] In C, select() call does not wait archieval Programming 3 07-22-2010 05:28 AM
OSE system call functions to Linux Sytem Call functions required roshantraj30 Linux - General 0 06-08-2009 02:06 AM
Hello... new to Linux, why did I wait so Long! markmowbray LinuxQuestions.org Member Intro 3 11-09-2007 06:39 AM
tracing the error code returned by wait call lg3 Linux - Software 0 02-21-2005 03:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration