i wanna ask about forking in unix...how can you do forking...??i don't understand and i have an assignment about it...the output should look like this...
Question 1 : what's your name ?
Answer : if no answer, write to file...----Out of time-----because for each question it is given a specific time to answer, if the user failed to answer it within the specified time, it prints out that message and output ---No answer--- on the question file( we have to read the questions from a file)...and there are 3 questions...the execution for this program should be....
$ a.out 5 question.txt
5 is the time specified and question.txt is the question file...it was taken in the line...
int main(int argc, char* argv[])
{...}
well, anybody can help me??please....
fork() is used to create a copy of a process. When you call fork(), the calling process is copied and you now have a parent and child process. THe only difference is the return value of fork(). It will return 0 to the child process, and a process ID number for the parent. That's how you can tell which process you're currently in.
Well, I won't write your assignment for you, but here's how a fork goes:
<pre>
int main(int argc, char* argv[])
{
pid_t pid;
printf("This will be seen once.");
pid = fork();
printf("This will get seen twice. Once for the parent, once for the child process");
if (pid == 0) /* This is the child process /
{
/ Do child process stuff here /
exit(0);
}
else if (pid < 0) / Some sort of error in fork() /
{
/ Process error here /
}
else / This means we are in the parent process /
{
/ Do parent process stuff /
wait(NULL); / wait for the child to finish */
}
return(0);
}
</pre>