When you fork and modify a variable, is it modified in the parent process too?
When you fork and modify a variable, is it modified in the parent process too?
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
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?
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
Nevermind. Just started reading up on pthreads.
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
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
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
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; }
A man asked, "Who are you?"
Buddha answered, "I am awaked."
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.
A man asked, "Who are you?"
Buddha answered, "I am awaked."
> 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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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++
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
That works. Thank you.....Originally Posted by Salem
A man asked, "Who are you?"
Buddha answered, "I am awaked."