Does anyone know the answer to question 20 of mid sem?
I believe it's c. because the fork() function duplicates
twice resulting in 4 processes. But I'm uneasy with the overall
syntax.
Thank you!!
> Does anyone know the answer to question 20 of mid sem?
> I believe it's c. because the fork() function duplicates
> twice resulting in 4 processes. But I'm uneasy with the overall
> syntax.
> Thank you!!
I don't have the question in front of me at the moment, but if I recall it was
something like
fork();
fork();
printf("hello world\n");
In this case, "hello world" will be printed 4 times, on 4 separate lines.
> fork call is in the lectures after the test, does this mean we can expect it to not
> be in the test?
Yes, we will talk more about fork() and process creation after the test,
but we have 'discussed' its role, in lectures, sufficiently to answer this and similar
questions.
Hi, I compiled my code using the following syntax:
void function(void)
{
fork();
fork();
printf("hello world\n");
}
int main(void)
{
function();
return 0;
}
And, as expected it prints "hello world" 4 times on seperate lines; However,
the solutions to the mid-semester test state that 'D' is the right answer. Which
says "The form of the output cannot be determined from the above code", is this
because a different operating system may give a different result, or is this
a typo in the solutions?
I thought that C language is Operating System independent, meaning that it can be
compiled on most systems. I don't know, please let me know if I'm going wrong. I read it
in the book that is prescribed on this website.
> I thought that C language is Operating System independent, meaning that it can be
> compiled on most systems. I don't know, please let me know if I'm going wrong. I read it
> in the book that is prescribed on this website.
Yeah not always as the Microsoft MSDN states.
"One of the largest areas of difference is in the process model. UNIX has fork; Win32 does not"
Got me too even after I wrote a test program it failed to compile on a windows machine ( but it
worked on Linux). I said C. ( 4x hello) I guess the lesson here is to read the actual question.
If it said "What would the output be under Linux" then it would be a different story.
https://msdn.microsoft.com/en-us/library/y23kc048.aspx
> And, as expected it prints "hello world" 4 times on seperate lines; However,
> the solutions to the mid-semester test state that 'D' is the right answer. Which
> says "The form of the output cannot be determined from the above code", is this
> because a different operating system may give a different result, or is this
> a typo in the solutions?
What actually happens depends on the interleaving/scheduling of the 4 running processes,
whether they buffer their output (and maybe some external factors, but that would be too
pedantic to ask about).
In short, from the short bit of code, we can't tell what may happen.
> I thought that C language is Operating System independent, meaning that it can be
> compiled on most systems. I don't know, please let me know if I'm going wrong. I read it
> in the book that is prescribed on this website.
Yes, C is OS independent, meaning that the language makes no strong demands of the OS on which its
programs run. But that's not related to the question - fork() is not a function defined as part of
the standard C library, it's a system-call (function) provided by some OSs.
Similarly, a C program, compiled and running on Windows, may be able to call a function named
Start_Microsoft_Office(), but that's unrelated to whether or not C is OS independent.
> Got me too even after I wrote a test program it failed to compile on a windows machine ( but it
> worked on Linux). I said C. ( 4x hello) I guess the lesson here is to read the actual question.
> If it said "What would the output be under Linux" then it would be a different story.
No, don't try and read too much into the question - it's not trying to be tricky.
The question asks "If the above code executes successfully....",
so the code must have already compiled and linked successfully to get that far.
> "David Chinnery" <21*4*7*1@s*u*e*t*u*a*e*u*a*> wrote:
>
> > Got me too even after I wrote a test program it failed to compile on a windows machine ( but it
> > worked on Linux). I said C. ( 4x hello) I guess the lesson here is to read the actual question.
> > If it said "What would the output be under Linux" then it would be a different story.
>
> No, don't try and read too much into the question - it's not trying to be tricky.
>
> The question asks "If the above code executes successfully....",
> so the code must have already compiled and linked successfully to get that far.
But if the questions asks "If the above code executes successfully..." and we can therefore assume that it
compiled fine - why wouldn't we know sorry? Surely the OS supports the call and therefore it would be four?