Go Back   The UNIX and Linux Forums > Top Forums > Programming
.
google site



Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-16-2006
Registered User
 

Join Date: Sep 2006
Location: NewYork
Posts: 7
Child process is not getting started

Hi,
When i m trying to run below code,its entering into wait stage.

output: In parent
pid=2134 // some random value assigned to child process
parent waiting.....

and then it keeps on waiting for child to get terminate

Y this child is not getting started....atleast i should get " ---in child"



Code:

printf("In parent");
for(i=0;i<3;i++)
	{	
	  pid=fork();
          printf("pid= %d",pid);
	  if(pid==0)
		{
		  printf("---in child");
		  memset(temp,0,100);
		  strcpy(temp,commandList[i]);
		  intial_command=strtok(temp," ,\0,");
  
		  if(i==0)
		   {
				close(1);
				dup2(pipes[0][0],1);
				close(pipes[0][1]);
				execvp(intial_command,commandList[i]);
			}
		  else
			{
			  if(i==countCommands-1)
				{
					close(0);
					dup2(pipes[i][1],0);
					close(pipes[i][0]);
					execvp(intial_command,commandList[i]);
				}
			  else
				{
				  close(0);
				  dup2(pipes[i][1],0);
				  close(1);
				  dup2(pipes[i][0],1);
				  execvp(intial_command,commandList[i]);
				}
			}
		}
		printf("parent waiting.....");
		waitpid(pid,&processStatus,0);
	}

..pls help me out...its bit urgent

Last edited by reborg; 09-16-2006 at 04:50 PM..
Sponsored Links
  #2 (permalink)  
Old 09-16-2006
Technorati Master
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 3,007
please use code tags ...

you are checking only with pid == 0
what if the system (kernel) has failed to fork a process ?

check the condition if pid < 0 also

check from ps -ef that both parent and child are running !!!
  #3 (permalink)  
Old 09-16-2006
Registered User
 

Join Date: Sep 2006
Location: NewYork
Posts: 7
Both parent and child processes are running....still its not goiing into child's code

i tried to check which all processes are running....actaully both child and parent processes are running...but its not entering into child's code
  #4 (permalink)  
Old 09-17-2006
Technorati Master
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 3,007
i doubt here

Code:
printf("---in child");

since the buffer is not filled with just '---in child'
and the printf is not directed to flush immediately
i think it is delayed...

try as


Code:
printf("---in child\n");

  #5 (permalink)  
Old 09-17-2006
Registered User
 

Join Date: Sep 2006
Posts: 3
You really need to check the return from your fork, like for instance:

fork_return = fork();
if( fork_return < 0)
{
curtime = time (NULL); /* get current time */
loctime = localtime (&curtime);
fprintf(fplog,"Unable to spawn child process, exiting. ");
fputs (asctime (loctime), fplog);
fprintf(fplog, "errno: %s\n",strerror(errno));
exit(-1);
}

Add in errno.h and then look at the errno string. Good luck,
Jen
  #6 (permalink)  
Old 09-17-2006
Registered User
 

Join Date: Sep 2006
Location: NewYork
Posts: 7
thanx alot...

by using printf("--in child\n") instead of printf("--in child") , i got the desired output
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Can a child process return a specific value to a parent process ? Ametis1970 Programming 8 04-10-2008 12:22 AM
how to know that a perticular process is started!!! zing_foru UNIX for Advanced & Expert Users 11 05-19-2007 11:04 AM
Process started on console zverzak UNIX for Dummies Questions & Answers 2 12-07-2006 04:33 AM
is there any way to know how much time process was running from the moment it started umen UNIX for Dummies Questions & Answers 2 04-03-2006 01:19 AM
PID of process started in background?? stilllooking Shell Programming and Scripting 2 11-16-2004 03:47 AM



All times are GMT -4. The time now is 02:33 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0