PDA

View Full Version : Forking



XSquared
04-16-2004, 09:16 PM
When you fork and modify a variable, is it modified in the parent process too?

Thantos
04-16-2004, 09:24 PM
IIRC no. When the child process is created it gets it's own memory area.

Test program:

#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;
}

Result:
In child: 60
In parent: 16

XSquared
04-16-2004, 10:31 PM
Well, in that case, is there any way to start a new thread sharing the memory with its parent?

XSquared
04-16-2004, 10:36 PM
Nevermind. Just started reading up on pthreads.

Thantos
04-16-2004, 11:14 PM
Yea I find multi threading more useful the multi process.
I personally use the pth library from GNU.

Hammer
04-17-2004, 03:49 AM
or use "shared memory".
http://www.cs.cf.ac.uk/Dave/C/node27.html#SECTION002700000000000000000

gandalf_bar
04-19-2004, 10:10 AM
IIRC no. When the child process is created it gets it's own memory area.

Test program:

#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;
}

Result:
In child: 60
In parent: 16


Shouldn't be like this:



#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;
}

Thantos
04-19-2004, 10:29 AM
Meh small mistake. Basic idea lives on.

gandalf_bar
04-19-2004, 11:24 AM
Just a simple question:

Consider 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;
}


Then this:



#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;
}


Ok, I understand this two code. But look at this one:


#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;
}


Why?????

Just as reference, I use Slackware 9.1 and GNU C++ compiler version 3.3.3.

Salem
04-19-2004, 01:17 PM
> 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.

Thantos
04-19-2004, 02:39 PM
Also

char *arg[] = {
"ls",
NULL
}; needs 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)

Hammer
04-19-2004, 03:08 PM
>>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++ :)

Thantos
04-19-2004, 03:47 PM
Most likely the later then the former...

gandalf_bar
04-22-2004, 03:13 AM
> 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.

That works. Thank you.....