The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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

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 High Level Programming 8 04-09-2008 11:22 PM
how to know that a perticular process is started!!! zing_foru UNIX for Advanced & Expert Users 11 05-19-2007 10: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 12:19 AM
PID of process started in background?? stilllooking Shell Programming and Scripting 2 11-16-2004 03:47 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-16-2006
Crab Crab is offline
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 03:50 PM..
  #2 (permalink)  
Old 09-16-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
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
Crab Crab is offline
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
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
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
jenmead jenmead is offline
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
Crab Crab is offline
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 On




All times are GMT -4. The time now is 09:55 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-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0