[Help]Fork Command

Posts 1–7 of 7 · Page 1 of 1
[Help]Fork Command
Hello,

I have a university assignment in which we are asked to use the fork() programming construct to create some number of processes, specified on the command line - for example - the command ./FORK1.exe 6 should create 6 new simultaneously - executing processes as quickly as possible. Each process except the original should then print out its own process ID using getpid() which returns the current process ID when it is called. After printing out the process ID, each process should wait for two (2) seconds by calling sleep(2) and then exiting: exit().

The main - parent process should wait three (3) seconds before exiting. Use atoi() to convert the input from command line from ascii to integer.

I am using fork() command in while loop for the creation of child processes. The problem is that each fork() command further creates more child processes. How can I prevent this to happen using while loop or any other solution?

I hope I was clear enough in explaining the problem. Please let me know if you need any clarification.
Help will be much appreciated.

Thanks,
ANK
First thing i see is Sleep(2); Thats going to sleep for 2 miliseconds not 2 seconds

And second i really dont know Fork() but from what i read you just want to create 6 child processes..
So my Question how many child processes does it create when calling fork?
whenever the fork command is executed a child process is created but what I want over here is that the child process should not create any more child and also can print their own process id.
Quote Originally Posted by ANK View Post
whenever the fork command is executed a child process is created but what I want over here is that the child process should not create any more child and also can print their own process id.
Fork() reacts differently on different systems. Some systems have blocked fork() because of the so called fork bombs. Could you please tell us on which system you are on?

Read here is a description of the fork function, I hope it helps.

Note:

Since Fork() Creates an exact copy of the caller, you want your code to check if they are child processes or not. Try looking at:

getppid() (get parent process id)
getpid() (get process id)

Lasty you should have your parent process calling fork, check if it's parent is the shell, I don't know if this is possible on your system, but you can try using the built-in variable $$.

For example:

echo $$

Would return the PID of the shell
check the if another copy of the same process is already running and if it is print pid?
the call is being made by the parent only but as the child has the exact image of the parent so the child also inherits fork() command and create further child. I just want to know how can i stop this if more than two child are to be created by one parent process and none of the child can create its own child process while printing the id.
Quote Originally Posted by ANK View Post
the call is being made by the parent only but as the child has the exact image of the parent so the child also inherits fork() command and create further child. I just want to know how can i stop this if more than two child are to be created by one parent process and none of the child can create its own child process while printing the id.
I just told you how that could be done, here's a piece of pseudo code:
Code:
int main(){

Dword pidShellPid = NULL;
pidShellPid = GetShelPid();           //imaginary function, you need (somehow) to get the Pid of the shell that spawned you


if(getppid() == pidShellPid){               //see if the parent is the shell, if it is we are parent process and we need to fork
       for(int i = 0; i != 6; i++){
           Fork();
       }
} else {
printf("Child pid: %x\n", getpid());
Sleep(2000); // sleep 2 seconds);
quit();
}

Sleep(3000);
quit();

}
Posts 1–7 of 7 · Page 1 of 1

Post a Reply

Talk with us