Computing.Net > Forums > Programming > fork system call

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

fork system call

Reply to Message Icon

Name: .jareeN.
Date: August 16, 2003 at 13:05:43 Pacific
OS: linux
CPU/Ram: p4/256
Comment:

Hi all,

Please have a look at the following code...


#include <stdio.h>
{
int fork(void),value;
value=fork();
printf("In Main: value = %d\n",value);
return 0;
}

The code compiles and runs perfectly. But what does int fork(void),value; means.

.jareeN.




Sponsored Link
Ads by Google

Response Number 1
Name: zeroguy
Date: August 16, 2003 at 17:00:27 Pacific
Reply:

Are you asking what the line

'int fork(void),value;'

itself means, or what the significance of value's value is?

If it's the latter, it's an easy question to answer, but as for the former, I don't know. If you take out 'fork(void),' the program still compiles and runs perfectly.

If you are asking what fork() does, though, it is simple enough. This is an odd way of using the call, but what the program would do is output the process ID (PID) of the program when it executes, and output 0 as a second PID (the PID of it's child process). What this program did was to have fork() spawn a child process, and then output the PIDs of the parent process and the child process (which is always 0 inside the program).

But, the conventional way to use fork(), is something like this:

//...
if( fork() == 0 ) {
// do some stuff
}
else {
// do some other stuff at the same
// exact time!
}
//...

So, it will execute the first code block as the child process, and execute the second code block (the parent process) at the same time. You also may want to store the PID at the time of the if-statement if you want to send signals, or stuff like that.

Hope that helped.


0

Response Number 2
Name: .jareeN.
Date: August 16, 2003 at 22:08:00 Pacific
Reply:

Well I was talking about the former ie fork(void). I also ran the program with and without it. The question is for what it is there ? And why don't we get syntax errors ?

Thanks for help anyway.

.jareeN.


0

Response Number 3
Name: zeroguy
Date: August 17, 2003 at 17:06:15 Pacific
Reply:

Well, I don't exactly know why, but I have one guess. (Or theory I guess is a better word).

'int fork(void)' is a valid function declaration (in the scope of main) for calling the fork() function. Now, you would think that this would create a conflict with the system call fork() (or at least a case of shadowing), but I think that the system call fork() may have a return type of p_type or PID, whatever, while not exactly being an int, but performing essentially the same role.

So 'int fork(void)' would just be overloading the system fork(). Now, as for how it can tell which fork() to call, my guess is that fork(void) only has a prototype, so it calls the fork() with an implementaion, the system fork().

Remember, that's only a guess, feel free to correct/criticize me on any points. ;)


0

Response Number 4
Name: Infinite Recursion
Date: August 17, 2003 at 21:20:09 Pacific
Reply:


From what I gather, your line of code
int fork(void),value; is a bit overkill.

At anyrate, when separated, the line breaks into the following two lines:

int fork(void);
int value;

The function fork returns a value of type
integer. So for all practical purposes
int fork(void); is not needed. Being that
the fork function can return a value, you can
test the call to the fork() based on the
integer value it returns... for instance:

if ((id = fork()) == 0)
{
// do code
}

Maybe that clears things up a bit.

Infinite Recursion


0

Response Number 5
Name: zeroguy
Date: August 18, 2003 at 21:03:39 Pacific
Reply:

Yes, well, I already realized all of that, but our question really is: why doesn't int fork(void); cause a conflict with the already predefined fork()?


0

Related Posts

See More



Response Number 6
Name: Infinite Recursion
Date: August 18, 2003 at 21:23:33 Pacific
Reply:

Perhaps function overloading is at play...


0

Response Number 7
Name: .jareeN.
Date: August 19, 2003 at 00:09:03 Pacific
Reply:

Perhaps function overloading is at play...

Remember this is C.

.jareeN.


0

Response Number 8
Name: Infinite Recursion
Date: August 19, 2003 at 12:58:08 Pacific
Reply:

oops. overlooked that...
i don't know what the problem is... if it isn't broke don't fix it ;)


0

Response Number 9
Name: .jareeN.
Date: August 20, 2003 at 08:27:21 Pacific
Reply:

I think I have found what is happening here. To understand consider the code:

#include <stdio.h>

int f()
{
printf("Hello\n");
return 123;
}

int main()
{
int f(),n;
n=0.0;
n=f();
printf("%d\n",n);
return 0;
}

Now I get the output:
Hello
123

Now let us change the code a little:

#include <stdio.h>

int f()
{
printf("Hello\n");
return 123;
}

int main()
{
float f(),n;
n=0.0;
n=f();
printf("%d\n",n);
return 0;
}

Now on compiling I get the following output (remember the compiler is gcc):
functest.c: In function `main':
functest.c:11: conflicting types for `f'
functest.c:4: previous declaration of `f'
functest.c:11: warning: extern declaration of `f' doesn't match global one

So it seems int fork() defaults to extern modifier. Hope that I am clear enough.

.jareeN.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: fork system call

fork system call www.computing.net/answers/programming/fork-system-call/19196.html

System call in C www.computing.net/answers/programming/system-call-in-c/661.html

System call without focus www.computing.net/answers/programming/system-call-without-focus/10234.html