I am trying to create a program that uses fork to create 4 processes, which I am understanding to be 2 parents and 2 children.
The way I have set up my code is:
for(int i = 0; i < 2; ++i){
pid_t pid1 = fork();
switch(pid1){
case -1:
fatal("fork failed");
break;
case 0:
child(i);
break;
default:
parent(i);
break;
}
}
In child() and parent() respectively, I am calling getpid(). Inside child(), I exit(0) once I am done. Inside parent() I wait for the child using wait(0) When I run the program, it outputs 2 different children pids and 2 same parent pids. Is this happening because I am calling the same fork() twice?
Process 1 calls fork for 1st loop iteration, creating process 1.1.
Then process 1 calls fork again for 2nd loop iteration, creating process 1.2.
Then process 1.1 (which is essentially process 1 duplicated when fork was done) also enters 2nd loop iteration, creating process 1.1.1.
So processes 1.1 and 1.2 have same parent, process 1. And there are total 4 processes (1, 1.1, 1.2, 1.1.1).
Note that steps 2 and 3 may happen in different order, depending on how OS decides to schedule processes.
Because you have used exit function in child(i) function, child will exit and hence only parent process will continue it's execution in for loop. So you only get 2 new process forked which are childs of same parent . So parent id stays same but since two childs are created, so get 2 distinct child pids! If you want four process to be forked, then you will have to erase the exit statement from child(i) function and use if else statements after each fork() call.
Related
I don't know if this is the best way but I have a random number of child processes who have beed execed and wanted to implement a way to kill them without using ctrl+c. I was thinking of keeping a set of their pids and then check that set whenever I want to kill them from the parent process.
The way I was trying to do it was something like this
set<pid_t> pids;
pid_t id = fork();
if(id == 0)
{
pids.insert(getpid());
execlp("./somewhere", "./somewhere", something.c_str(), NULL);
cout << "Didn't exec" << endl;
exit(0);
}
for(auto i : pids)
{
kill(i, something?)
}
I still don't quite know how to use the kill function or how pids work so I don't know if this will work in any way, I just did a simple project in c for college and though I could try something more complex in c++.
Anyways, the objective of this is to be able to have the parent process kill a single child process out of an undefined number of running child processes, or kill them all whenever the user writes quit
kill() on pid 0 sends the signal to all members of the calling process group:
If pid is 0, sig shall be sent to all processes (excluding an
unspecified set of system processes) whose process group ID is equal
to the process group ID of the sender, and for which the process has
permission to send a signal.
If you want to kill only certain processes (as seems to be your case) take a look to Grouping child processes with setpgid()
My program has the following parent child layout:
int main() {
std::vector<pid_t> kids;
pid_t forkid = fork();
if (forkid == 0) {
//child process
pid_t fork2 = fork()
if (fork2 == 0) {
// child process
}else {
//parent
kids.push_back(fork2);
}
}else {
// code here
kids.push_back(forkid);
}
// Not killing the fork2 process - only the first process(forkid) is terminated
for (pid_t k : kids) {
int status;
kill(k, SIGTERM);
waitpid(k, &status, 0);
}
}
I am not able to kill the child process (fork2) - the first process gets terminated. The kids vector seems to only contain the process id of the first process. Never gets the pid of the child process.
What am I doing wrong here. Any help will be appreciated. Thanks.
std::vector<pid_t> kids;
pid_t forkid = fork();
fork() creates a complete duplicate image of the parent process as its child process. Emphasis on: duplicate.
This means that, for example, the child process has its very own kids vector, that has nothing to do, whatsoever, with the parent process's original kids vector.
Since this is the very first thing that happens in main, this is really no different than you running this executable twice, individually as two distinct and separate processes, instead of forking off a process. You can't expect the kids vector in one of the two processes to have any effect on the kids vector in the other one.
The first child process creates a 2nd child process, and also adds the 2nd child process's pid into its own kids vector. But that's just the first child process's kids vector.
The only process id that the original parent process's kids vector ends up having is the first child's process it, so that's all you get to SIGTERM.
Your options are:
Restructure your logic so that both child process get created by the parent process, so it, alone, puts their process ids into its kids vector.
Instead of using fork use multiple execution threads, and the same process. However, neither std::vector, nor any other C++ library container is thread safe. A massive pile of code will need to be written to properly synchronize the threads, in order for things to work themselves out correctly (not to mention that the analogue for the SIGTERM, with respect to multiple execution threads, needs to be invented in some way).
The simplest alternative is the first one.
Im trying to emulate shell through C program. In my program whenever I run any normal (foreground) commands it works fine. Also I have handled background process with commands ending with '&'. Now to handle this I have avoided the parent waiting for a child process.
The problem is whenever for the first time in my shell I run any background command(i.e ending in '&') then it works fine. But then after that each command(normal) doesnot terminate. I guess it waits for the previously opened process. How to rectify. Please you can ask questions so that i can make myself more clear to you. This is the snippet which is doing the above mentioned task.
child_id=fork();
if(child_id==0){
//logic fo creating command
int ret=execvp(subcomm[0],subcomm);
}
//Child will never come here if execvp executed successfully
if(proc_sate!='&'){
for(i=0;i<count_pipe+1;i++){
waitpid(0,&flag,0);
}
//something to add to make it not wait for other process in my scenario for second time
}
Here proc_state just determines whether it is background or foreground.It is just a character. count_pipe is just a variable holding number of pipes (e.g ls -l|wc|wc this contains 2 pipes). Dont worry this all is working fine.
waitpid(0, &flag, 0) waits for any child process whose process group ID is equal to that of your shell. So if you have not called setsid() after the fork() of the disconnected child process, the code above will wait for that too.
pid_t pid = fork();
if (pid == 0) { /* child process */
setsid(); /* Child creates new process group */
... /* redirections, etc */
execvp(...);
}
I have a string (fileContents) in shared memory that consists of a 9 lines:
sprintf(shared_memory, fileContents.c_str());
I want to call on fork() to create the same number of processes as lines. These processes will manipulate each line. However, I have no idea where to start when calling fork(). Every example I have looked at just consists of returning the process ID of parents and child processes and not showing how or when the processes executes something.
Any guidance would be appreciated, thanks!
Every example I have looked at just consists of returning the process ID of parents and child processes
That's not correct.
The parent process will get the process id of the child process, but the child process will know it's the child process because fork() returns 0.
This code will fork 9 times, with each child doing specific work.
for( int line = 1; line <= 9; ++line ) // *cough*
{
if ( fork() == 0 )
{
// Child process. Handle line, and exit()
}
}
I have managed to fork and exec a different program from within my app. I'm currently working on how to wait until the process called from exec returns a result through a pipe or stdout. However, can I have a group of processes using a single fork, or do I have to fork many times and call the same program again? Can I get a PID for each different process ? I want my app to call the same program I'm currently calling many times but with different parameters: I want a group of 8 processes of the same program running and returning results via pipes. Can someone please point me to the right direction please ? I've gone through the linux.die man pages, but they are quite spartan and cryptic in their description. Is there an ebook or pdf I can find for detailed information ? Thank you!
pid_t pID = fork();
if (pID == 0){
int proc = execl(BOLDAGENT,BOLDAGENT,"-u","2","-c","walkevo.xml",NULL);
std::cout << strerror(errno) << std::endl;
}
For example, how can I control by PID which child (according to the parameter xml file) has obtained which result (by pipe or stdout), and thus act accordingly? Do I have to encapsulate children processes in an object, and work from there, or can I group them altogether?
One Fork syscall make only one new process (one PID). You should organize some data structures (e.g. array of pids, array of parent's ends of pipes, etc), do 8 fork from main program (every child will do exec) and then wait for childs.
After each fork() it will return you a PID of child. You can store this pid and associated information like this:
#define MAX_CHILD=8
pid_t pids[MAX_CHILD];
int pipe_fd[MAX_CHILD];
for(int child=0;child<MAX_CHILD;child++) {
int pipe[2];
/* create a pipe; save one of pipe fd to the pipe_fd[child] */
int ret;
ret = fork();
if(ret) { /* parent */
/* close alien half of pipe */
pids[child] = ret; /* save the pid */
} else { /* child */
/* close alien half of pipe */
/* We are child #child, exec needed program */
exec(...);
/* here can be no more code in the child, as `exec` will not return if there is no error! */
}
}
/* there you can do a `select` to wait data from several pipes; select will give you number of fd with data waiting, you can find a pid from two arrays */
It's mind-bending at first, but you seem to grasp that, when you call fork( ):
the calling process (the "parent") is
essentially duplicated by the
operating system and the duplicate process
becomes the "child"
with a unique PID all its own;
the returned value from the fork( )
call is either: integer
0,1 meaning that the
program receiving the 0 return is the
"child"; or it is the non-zero integer PID
of that forked child; and
the new child process is entered into
the scheduling queue for execution.
The parent remains in the scheduling
queue and continues to execute as
before.
It is this ( 0 .xor. non-0 ) return from fork( ) that tells the program which role it's playing at this instant -- 0 returned, program is the child process; anything else returned, program is the parent process.
If the program playing the parent role wants many children, he has to fork( ) each one separately; there's no such thing as multiple children sharing a fork( ).
Intermediate results certainly can be sent via a pipe.
As for calling each child with different parameters, there's really nothing special to do: you can be sure that, when the child gets control, he will have (copies of) exactly the same variables as does the parent. So communicating parameters to the child is a matter of the parent's setting up variable values he wants the child to operate on; and then calling fork( ).
1 More accurately: fork( ) returns a value of type pid_t, which these days is identical to an integer on quite a few systems.
It's been a while since I've worked in C/C++, but a few points:
The Wikipedia fork-exec page provides a starting point to learn about forking and execing. Google is your friend here too.
As osgx's answer says, fork() can only give you one subprocess, so you'll have to call it 8 times to get 8 processes and then each one will have to exec the other program.
fork() returns the PID of the child process to the main process and 0 to the subprocess, so you should be able to do something like:
int pid = fork();
if (pid == 0) {
/* exec new program here */
} else {
/* continue with parent process stuff */
}