So if you number your processes 1-8, only 2,3,4 need to spawn 2 children (grandchildren)? Do they have to report anything? like actual process ID or logical process ID (ie 1-8)?
So if you number your processes 1-8, only 2,3,4 need to spawn 2 children (grandchildren)? Do they have to report anything? like actual process ID or logical process ID (ie 1-8)?
OK let me sleep on it. Something in the am.
You know what the biggest problem I think I'm having? getpid() doesn't work the way it's supposed to. It doesn't store or print the current process. It prints the processes all the way up to the first, even though it's not supposed to. How do I get rid of the parents?
I don't know yet; I have never used fork() in this way....my preferred method of process creation/control is pthreads which are cake in comparison. I did not sleep well last night and just got back from the doctor who shot me with something that is making me woozy. I will still endeavor to give this a shot but bear in mind it may take a little more time and of course the results should be verified.
I am starting on this within 10m though. BTW the process ID of the new process is returned from the fork() call and I will probably leverage that in some way. You said you had to use fork() and getpid()....nothing was said about using fork() to get what getpid() should have returned, eh?
Last edited by jeffcobb; 02-10-2010 at 02:30 PM.
CheckMyBrain: are you on Linux? If so do you have the ability to install cmake? From any Debian-based system it is a simple apt-get install cmake. It will help you use my example if/when I get it sorted.
I can't install anything, it's a university server and even if I did get approval, the college is closed today for a snowday.
My professor's answer: "check man -s 2 fork and man -s 2 getpid". As if that link you posted didn't do a better job of it or anything.
I'm starting to get the idea that we're making a process tree. I'm also starting to get the idea of using process specific code. However, I'm still stumped on making one process fork into 2. I can do grandchildren fine (ie 1 makes 2 makes 3) but not what he wants.
Noodling on it. At a minimum I could give you a simple bash script to show compilation...
Ok, so I found waitpid(). I also found an example that was supposed to bear 3 children from the same process.
So I took the liberty to make it 2 children. Simpler, right? Well, here's the output:Code:#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void) { int i; pid_t one, two, three; for(i = 0; i < 2; i++) { one = fork(); switch(one) { case 0:printf("My PID is %d ", getpid()); printf("parent is %d\n", getppid()); default: waitpid(one, NULL, 0); } } }
So it prints out line 1 and 3 which are the same. Why?Code:My PID is 17438 parent is 17437 My PID is 17439 parent is 17438 My PID is 17438 parent is 17437 My PID is 17440 parent is 17437
Last edited by jeffcobb; 02-10-2010 at 03:44 PM.
I actually get this from that:
My PID is 1904 parent is 1903
My PID is 1905 parent is 1904
My PID is 1906 parent is 1903
The second one is the for loop completing in the child, producing a grandchild -- you have
parent i = 0 child1 = 0,
child1 i =1, grandchild1 i = 1,
parent i = 1 child2 i = 1.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Case = 0 means that it's the child. Otherwise, it's the adult. I'm not checking to see if it's negative, cause it wont be.
I don't mean to be rude, but could you elaborate? I've been working on this all day and it's getting to the point where I'm starting to lose focus.
Also, how you'd only get 3 lines? Did you include a library when you compiled?
Edit: Are you saying the loop is being run in the child? How do I stop that?
I'd say it's a better question as to why you get four lines! Are you sure you are running exactly the code you posted?
Anyway, to elaborate, the reason the loop continues in the child is because a child is an exact copy of it's parent; it includes the variable i set to it's current value. You can see this more easily by tweaking that code a bit:
Now watch what happens.Code:for(i = 0; i < 3; i++) { one = fork(); switch(one) { case 0:printf("My PID is %d, i = %d ", getpid(), i);
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Code:My PID is 17772, i = 0 My PID is 17773, i = 1 My PID is 17774, i = 2 My PID is 17772, i = 0 My PID is 17773, i = 1 My PID is 17772, i = 0 My PID is 17775, i = 2 My PID is 17772, i = 0 My PID is 17776, i = 1 My PID is 17777, i = 2 My PID is 17776, i = 1 My PID is 17778, i = 2 WinSCP: this is end-of-file:1
Hmm, but I don't want the child to be a complete clone. Otherwise I can't get anything done! I thought vfork() was the solution, but it's not.
I also did ask my professor for help, but he referred me to a page that explains how to seperate what happens by if statements....like I've tried before. I can't find any in-depth examples that are more than 2 processes. 2 processes are easy. I get that, but when you start adding loops or more than one fork() call, it all seems to go haywire.