Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.

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.

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.

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. ;)

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

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()?

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

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
123Now 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 oneSo it seems int fork() defaults to extern modifier. Hope that I am clear enough.
.jareeN.

![]() |
![]() |
![]() |

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