When you fork and modify a variable, is it modified in the parent process too?
Printable View
When you fork and modify a variable, is it modified in the parent process too?
IIRC no. When the child process is created it gets it's own memory area.
Test program:
Result:Code:#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main (void)
{
int fork_return;
int x = 5;
x++;
fork_return = fork();
if ( fork_return == 0)
{
sleep(10);
printf("In parent: ");
x+=10;
}
else
{
x*= 10;
printf("In child: ");
}
printf(" %d\n", x);
return 0;
}
In child: 60
In parent: 16
Well, in that case, is there any way to start a new thread sharing the memory with its parent?
Nevermind. Just started reading up on pthreads.
Yea I find multi threading more useful the multi process.
I personally use the pth library from GNU.
or use "shared memory".
http://www.cs.cf.ac.uk/Dave/C/node27...00000000000000
Quote:
Originally Posted by Thantos
Shouldn't be like this:
Code:#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main (void)
{
int fork_return;
int x = 5;
x++;
fork_return = fork();
if ( fork_return == 0)
{
sleep(10);
printf("In child: ");
x+=10;
}
else
{
x*= 10;
printf("In parent: ");
}
printf(" %d\n", x);
return 0;
}
Meh small mistake. Basic idea lives on.
Just a simple question:
Consider this code:
Then this:Code:#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void) {
int fork_return;
fork_return = fork();
if( fork_return == 0 ) {
sleep(5);
printf("Child only ");
}
else {
printf("Parent only! ");
}
printf("I am touchabled by parent and child");
return 0;
}
Ok, I understand this two code. But look at this one:Code:#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void) {
int fork_return;
fork_return = fork();
if( fork_return == 0 ) {
sleep(5);
printf("Child only ");
return 0;
}
else {
printf("Parent only! ");
}
printf("Now I am touchabled by parent only");
return 0;
}
Why?????Code:#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void) {
int fork_return;
fork_return = fork();
if( fork_return == 0 ) {
sleep(5);
//Why does this not show up in console
printf("Child only ");
//End of Why
char *arg[] = {
"ls",
NULL
};
execvp("ls",arg);
}
else {
printf("Parent only! ");
}
printf("Now I am touchabled by parent only");
return 0;
}
Just as reference, I use Slackware 9.1 and GNU C++ compiler version 3.3.3.
> Why?????
1) Try printing a newline with that as well
2) Try calling fflush(stdout) before the exec call
Output is buffered, so my guess is the buffers get trashed along with everything else owned by the program, when the exec happens.
Incomplete (ie unflushed) output is lost.
Alsoneeds to go before the sleep(). You can only declare variables at the beginning of a block in C (which is what I'm guessing you are using since you are using printf() and linux)Code:char *arg[] = {
"ls",
NULL
};
>>You can only declare variables at the beginning of a block in C
It's a C99 extension that allows this, maybe his compiler has it too. Or maybe it's being compiled as c++ :)
Most likely the later then the former...
That works. Thank you.....Quote:
Originally Posted by Salem