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
Access()System call ramkrix High Level Programming 3 03-27-2008 12:42 AM
c system call rangaswamy High Level Programming 1 02-19-2008 01:53 PM
fork() system call arunviswanath UNIX for Dummies Questions & Answers 1 09-19-2007 06:40 AM
how to differentiate system call from library call muru UNIX for Advanced & Expert Users 2 07-19-2007 11:20 PM
Making a system call hardwickj High Level Programming 5 07-06-2006 09:28 PM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 02-11-2004
a25khan's Avatar
a25khan a25khan is offline
Registered User
  
 

Join Date: Dec 2003
Location: My home :D
Posts: 35
wait system call

hi there,
i had some trivial questions about this program here. i am kinda confused with these, hope you can help me to understand here.

#include<stdio.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<unistd.h>

int main(void)
{
int i,status,waitret;

for(i=0;i<3;i++)
{
if(fork()==0)
{
if(execl("exec2","exec2",0)== -1)
{
fprintf(stderr,"Child: execl failed\n");
exit(1);
}
}

printf("Parent: continuing execution after fork %d\n",i);
}
/** we see above the parent execution lasted till the closing
brace "}". now below we call the status function. what is the
calling process here? i mean which process gave a call to the function status? is it parent? if it's parent then my question is well we called
for fork inside the for loop and i guess it is supposed to run child and
parent process in the loop only. my confusion builds up in the statement
i am about to write. i read in the text "The wait function suspends the
calling process until one of its immediate child processes terminates. The
termination status of the child process is stored in the integer pointed
to by status. If the calling process does not care about the termination
status, and is only interested in waiting until the child process
terminates, status may be given as the null pointer. If a child process
has terminated prior to the call to wait , wait returns immediately with
the status for that process. The process ID of the process that terminated
is returned by wait; if there are no unwaited-for child processes, wait
returns -1." */

for(i=0;i<3;i++)
{
waitret=wait(&status);
/** wait function is going to return an int value, what is
the purpose of using the function here? what is the calling process here?
what is the termination status of child here? in fork we get the pid of
the child, but what exactly is the termination status? is it gona return
1 on success and 0 on failure if for termination? as seeen above the
child terminates prior to the call of wait, so is wait going to return
the status immediately? what if the wait was called from within the
child process? would that still return immediately? if you read above
it said " The process ID of the process that terminated
is returned by wait". whats the use of it, if we can get the pid of the process by using getpid() or getppid()? */
printf("Status from wait: %d (decimal), %x
(hex)\n",status,status);
}
/** another question: i thought the parent ended after the for loop was
over, we called for fork within that loop, so isn't the parent restricted
to live inside the opening and closing braces only!? */
printf("Parent: Ending\n");
}

Appreciate your answer.
Cheers!
 

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 04:32 PM.


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