I am trying to understand how fork()/Linux Kernel deals with global variables.
Given code:
#include<signal.h>
#include<unistd.h>
#include<stdio.h>
#include<errno.h>
#include <sys/types.h>
pid_t pid;
int counter = 2;
void handler1(int sig)
{
counter = counter - 1;
printf("%d", counter);
exit(0);
}
int main()
{
signal(SIGUSR1, handler1); //Install Handler
printf("%d", counter); //Print Parent global variable
pid = fork( ); //Fork(), child pid = 0, parent's pid = positive int.
if (pid == 0) //Parent skips this, child goes into infinite loop
{
while(1) {}; // simulate doing some work
}
kill(pid, SIGUSR1); //While child is the loop, parents calls to terminate the child.
//Child will stop the infinite loop, and will not proceed any
//Will it call handler1 ???
wait(NULL); //Wait for child to be ripped
//Will it call handler1 second time ???
counter = counter + 1; //This will surely increment global variable
printf("%d", counter);
exit(0);
}
The output is 2123
How does Unix/Linux kernel deals with global variables after fork() and signal handlers are called ??? Do they get shared between child & parent ?
Another issues I have with this code, is how kill() & wait() will deal with global variables and what set will they use - parent's or child's ?? And will they call signal handler ???
Thanks !
The child gets an independent copy of the global variables. The two copies are not shared.
After fork(), the entire process, including all global variables, is duplicated. The child is an exact replica of the parent, except that it has a different PID, a different parent, and fork() returned 0.
A signal handler in the child will use the child's independent copy of the global variable.
The reason you're seeing 2 printed twice is that you haven't flushed standard output after printing it. This is what happens:
counter is equal to 2.
Parent process executes printf("%d", counter);, which puts "2" into the stdout output buffer, but does not flush it. No output appears yet.
fork() is called, which duplicates the process. There are now two copies of the counter variable, and both are set to 2. There are also two instances of the stdout output buffer, both of which contain the string "2". No output appears yet.
The parent sends SIGUSR1 to the child, and blocks on wait().
The child executes handler1(), which decrements the child's copy of counter to 1, and puts "1" into the child's stdout output buffer (which now contains "21").
The child executes exit(0), which as a side-effect flushes stdout. The output "21" appears now, written by the child, and the child exits.
wait() returns in the parent process. The parent increments its copy of counter to 3, and then prints "3" into its stdout output buffer (which now contains "23").
The parent executes exit(0), which as a side-effect flushes stdout. The output "23" appears now, and the parent exits.
If you put fflush(stdout); before the fork(), the 2 will only be printed once, and the output will be "213". It is good practice to flush all buffered output streams before calling fork().
fork creates a copy of the process in its current state. Nothing is shared except explicitly-mapped shared memory resources (anonymous shared maps, shared file maps, sysv shared memory blocks, and POSIX shared memory blocks).
You should also be aware that while the new process has its own copy of the file descriptor table, these file descriptors refer to the same "open file descriptions" in the kernel. They share a current seek position, among other things.
For further details, see:
http://www.opengroup.org/onlinepubs/9699919799/functions/fork.html
Related
Hi I have a simple question, however the timing issue is troubling me. Assume this is the code.
#include <stdio.h>
int main() {
int p = fork();
if (p==0) {
printf("ok\n");
sleep(1);
} else {
printf("hey!");
sleep(1);
}
printf("done!");
return 0;
}
My question is, will "done!" always be executed twice when the sleep is 1sec for both parent and child. Because I notice that when I increase the sleep to 10 seconds in the child process (p==0 case), I only see "done!" once.
I think when you increase sleep time parent process exited faster and stdout file descriptor closed. note that child and parent process shared their file descriptors.
if you want you can use _exit() in your parent process so when it exited, child process file descriptors will not be closed. in this way after 10 sec you see "done!" in your terminal. for use of this method you must use printf("done!\n") to flush your buffer manually because _exit() did not flush your buffer.
If you want you can use something like wait() in your parent process to issue wait on your child process.
I am trying to change a variable value inside a child process and keep it for the rest of the program. So In the below example why my cout keeps printing 0, and how do I solve it?
int var = 0;
int pid = fork();
if (pid == 0){ //child process
var = 1;
exit(1);
}
else if (pid> 0){ //parent process
if (-1 == wait (0))
perror ("there was an error with wait");
}
cout << var;
This is how code execution is going to go.
Child Process
int var =0;
var = 1;
exit (1);
Parent Process
int var =0;
if (-1 == wait (0))
perror ("there was an error with wait);
cout << var;
So as you can see, var will not be changed for the parent process.
When you fork, the child and parent have separate copies of the variables that were present before they split off into execution paths. Processes do not share the same block of memory. Therefore, altering var for the child will not affect the var in the parent.
If you want multiple execution paths on the same piece of memory, you should use threads.
Thank you for reading.
Looks like the fork() call has failed. Check the errno value
RETURN VALUE
On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.
ERRORS
EAGAIN
fork() cannot allocate sufficient memory to copy the parent's page tables and allocate a task structure for the child.
EAGAIN
It was not possible to create a new process because the caller's RLIMIT_NPROC resource limit was encountered. To exceed this limit, the process must have either the CAP_SYS_ADMIN or the CAP_SYS_RESOURCE capability.
ENOMEM
fork() failed to allocate the necessary kernel structures because memory is tight.
CONFORMING TO SVr4, 4.3BSD, POSIX.1-2001.
What the code actually does is takes input from parent process, sends it to child process through pipe. Child process reverses it then sends it back to parent through another pipe. There is no waitpid() or wait() function in the code.
The question is: How the process switching is working here? How write() and read() functions are working here?
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <iostream>
#define li long int
using namespace std;
void ReverseAString(char input[])
{
li length = strlen(input),i;
char hold;
for(i=0;i<length/2;i++)
{
hold = input[i];
input[i] = input[length-(i+1)];
input[length-(i+1)] = hold;
}
}
int main()
{
pid_t ChildOrParentId;
int fifoParent[2],fifoChild[2],in;
if(pipe(fifoParent)==-1)
{
cout<<"Problem in creating Parent's Pipe"<<endl;
perror("Parent's Pipe");
exit(1);
}
if(pipe(fifoChild)==-1)
{
cout<<"Problem in creating Child's Pipe"<<endl;
perror("Child's Pipe");
exit(1);
}
ChildOrParentId = fork();
if(ChildOrParentId==0)
{
char buf[100],collected[100];
close(fifoParent[0]);
close(fifoChild[1]);
in = 0;
while(read(fifoChild[0],buf,1)>0)
{
collected[in]=buf[0];
in++;
}
collected[in]=0;
cout<<"Read from Child "<<collected<<endl;
ReverseAString(collected);
cout<<"After Reversing: "<<collected<<endl;
write(fifoParent[1],collected,sizeof(collected));
close(fifoParent[1]);
}
else
{
char buf[100],collected[100];
close(fifoParent[1]);
close(fifoChild[0]);
in = 0;
cout<<"Enter a string: ";
gets(buf);
write(fifoChild[1],buf,sizeof(buf));
close(fifoChild[1]);
while(read(fifoParent[0],buf,1)>0)
{
collected[in] = buf[0];
in++;
}
collected[in] = 0;
cout<<"Read from Parent "<<collected<<endl;
}
return 0;
}
Output window looks like this:
Enter a string: abc // abc input given
Read from child abc
After reversing: cba
Read from parent cba
Normally, read on an empty pipe blocks until data is made available by writing to the write end of the pipe.
Thus, the child process can't continue execution past this line until it receives data from the parent; it blocks waiting for it:
while(read(fifoChild[0],buf,1)>0)
Once it has read the string, it wakes up, reverses it, and writes it back to the parent. The parent might also be blocked when it reaches the following line, waiting for the child process to write the reversed string:
while(read(fifoParent[0],buf,1)>0)
The blocking behaviour of read is similar to the blocking behaviour of wait or waitpid, but it waits for data to arrive on the file descriptor, rather than waiting for a child process to change status.
In general, parent and child processes execute simultaneously, except when one or both are blocked on a system call.
The moment you call fork(), a second process is created, and both processes are at this point in the code. The only way to tell if you're the new child process or the original parent process is to look at the return value of fork(). In the documentation, you can see that if fork() returns 0, you are in the child process. So basically, the then block of the if(ChildOrParentId==0) statement only runs in the child process, and the else block only runs in the parent process.
The rest of the explanation is pretty straight forward if you look at those two blocks as different programs. The parent block asks for a string, sends it to the child, waits for the child to send something back, then prints what the child sent. Meanwhile, the child block waits for something from the parent, prints what it gets, reverses it and prints that, then sends the reversed string back to the parent.
There are lot of questions on fork() but I am little bit confused in this code.I am analyzing a code in c++ in that I have got this function.
int daemon(int nochdir, int noclose)
{
switch (fork())
{
case 0: break;
case -1: return -1;
default: _exit(0); /* exit the original process */
}
if (setsid() < 0) /* shoudn't fail */
return -1;
/* dyke out this switch if you want to acquire a control tty in */
/* the future -- not normally advisable for daemons */
printf("Starting %s [ OK ]\n",AGENT_NAME);
switch (fork())
{
case 0: break;
case -1: return -1;
default: _exit(0);
}
if (!nochdir)
{
chdir("/");
}
if (!noclose)
{
dup(0);
dup(1);
}
return 0;
}
So the fork will create an exact copy of the code from where the fork() has been called. so,
Is switch executed twice or once?
If twice then in the switch what if the child executes first? Will it just break or go to the other statements?
What If the parent executes? will the main process be terminated and child will continue?
Edit:
So the switch will also run twice once with parent and once with child. and behaves on the return values.
And the final thing is, the daemon is a predefined function and it has been redefined and used like user created daemon. How it will create the daemon process and what the
`if (!nochdir)
{
chdir("/");
}`
and
if (!noclose)
{
dup(0);
dup(1);
}
I am calling this function like this.
if (daemon(0, 0) < 0)
{
printf("Starting %s [ Failed ]\n",AGENT_NAME);
exit(2);
}
Is switch executed twice or once?
It is said that fork is the function that is called once but returns twice, that is once in each process: once in parent and once in a child.
man :
On success, the PID of the child process is returned in the parent,
and 0 is returned in the child. On failure, -1 is returned in the
parent, no child process is created, and errno is set appropriately
It might return just once (-1): only in parent if child wasn't created. It always returns in the parent ( -1 on error, > 0 on success).
If twice then in the switch what if the child executes first? Will it
just break or go to the other statements?
It is unknown whether child or parent returns first. After fork() all the memory segments are copied into child, but it continues with the correct value 0 returned from the fork(). Parent continues with pid of the child. You use return value of fork in the code to determine whether you are child or parent. Maybe this will get more clear if you write code this way
int daemon( int nochdir, int noclose)
{
pid_t pid; /* to drive logic in the code */
if ( ( pid = Fork()) < 0) /* fork and remember actual value returned to pid */
return -1;
if( pid > 0)
_exit(0); /* exit the original process */
// here pid is 0, i.e. the child
What If the parent executes? will the main process be terminated and
child will continue?
What if the parent exit() is called before any child instructions? Then yes, parent will terminate, child will do on its own. Both the parent and the child processes possess the same code segments, but execute independently of each other (unless you added some synchronization).
http://linux.die.net/man/2/fork
Yes, when the parent executes it will continue in the default: case as the switch will have returned the child process id.
The common convention of saying that fork() is a function which is called once and returns two times is a bit obfuscating as it only returns once in each process space. The question is whether a child was created or not which determines which of the two ways a parent returns. The parent never gets a result of '0' from fork(), only either -1 or >0. The child always (if at all) gets zero.
If the child wasn't created, then fork() never returns in its process space.
Unless there's an error, fork will return twice: once in the parent process and once in the child process. fork creates a copy of the current process, then continues execution in both processes and you can determine by the return value. Note that the copy (child) is not a "perfect" copy: for example, in the child, all threads are terminated except for the one executing fork. The exact behavior is a bit complex.
It's not specified whether the parent or child process continues execution first. This depends on your OS and might even be totally random on your OS. Since they are two separate processes (which happen to run the same code) the order doesn't matter. The parent process will get a return value >0 (or -1 on error) and thus execute the default: label. The child process will get a return value of 0 and thus execute the case 0: label. This return value of fork is how the parent process knows it's a parent and the child process that it is a child (the child can query its own PID using getpid(2) and the PID of its parent using getppid(2)).
Yes, the parent runs into the default: label and executes _exit, thus terminating. The child will continue to run (note that here setsid() is very important; without it, the child would not continue to run if the shell session of your parent exits). This is the usual pattern for creating a daemon: when you run the program, it spawns actual main program (the daemon) through forking and then exits. For example, in the shell, you'll see the program exits quickly, but when you enter ps you can see that there's a process with the same name (your daemon).
I am kind of newbie on C++, and working on a simple program on Linux which is supposed to invoke another program in the same directory and get the output of the invoked program without showing output of the invoked program on console. This is the code snippet that I am working on:
pid_t pid;
cout<<"General sentance:"<<endl<<sentence<<endl;
cout<<"==============================="<<endl;
//int i=system("./Satzoo");
if(pid=fork()<0)
cout<<"Process could not be created..."<<endl;
else
{
cout<<pid<<endl;
execv("./Satzoo",NULL);
}
cout<<"General sentance:"<<endl<<sentence<<endl;
cout<<"==============================="<<endl;
One of the problem I encounter is that I am able to print the first two lines on console but I cant print the last two lines. I think the program stops working when I invoke the Satzoo program.
Another thing is that this code invokes Satzoo program twice, I dont know why? I can see the output on screen twice. On the other hand if I use system() instead of execv(), then the Satzoo works only once.
I haven't figured out how to read the output of Satzoo in my program.
Any help is appreciated.
Thanks
You aren't distinguisng between the child and the parent process after the call to fork(). So both the child and the parent run execv() and thus their respective process images are replaced.
You want something more like:
pid_t pid;
printf("before fork\n");
if((pid = fork()) < 0)
{
printf("an error occurred while forking\n");
}
else if(pid == 0)
{
/* this is the child */
printf("the child's pid is: %d\n", getpid());
execv("./Satzoo",NULL);
printf("if this line is printed then execv failed\n");
}
else
{
/* this is the parent */
printf("parent continues execution\n");
}
The fork() function clones the current process and returns different values in each process. In the "parent" process, it returns the pid of the child. In the child process, it returns zero. So you would normally invoke it using a model like this:
if (fork() > 0) {
cout << "in parent" << endl;
} else {
cout << "in child" << endl;
exit(0);
}
I have omitted error handling in the above.
In your example, both of the above code paths (both parent and child) fall into the else clause of your call to fork(), causing both of them to execv("./Satzoo"). That is why your program runs twice, and why you never reach the statements beyond that.
Instead of using fork() and doing everything manually (properly managing process execution is a fair amount of work), you may be interested in using the popen() function instead:
FILE *in = popen("./Satzoo", "r");
// use "in" like a normal stdio FILE to read the output of Satzoo
pclose(in);
From the fork() manpage:
RETURN VALUE
Upon successful completion, fork() shall return 0 to the child process and shall return the process ID of the child process to the parent process. Both processes shall continue to execute from the fork() function. Otherwise, -1 shall be returned to the parent process, no child process shall be created, and errno shall be set to indicate the error.
You check to make sure it succeeds, but not whether the pid indicates we're in the child or the parent. Thus, both the child and the parent do the same thing twice, which means that your program gets executed twice and the ending text is never printed. You need to check the return value of fork() more than just once.
exec - The exec() family of functions replaces the current process image with a new process image.
system - Blocks on execution of the command. Execution of the calling program continues after the system command returns
There are three return value tests you want with fork
0: you are the child
-1: error
other: you are the parent
You ran the other program from both the child and the parent...