Thread: C Processing Issues (oh fork()!)

  1. #16
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    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)?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  2. #17
    Registered User
    Join Date
    Feb 2010
    Posts
    25
    Quote Originally Posted by jeffcobb View Post
    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)?
    I think actual process ID. The instructions specifically say to use fork() and getpid(). Man it would be so easy if it were just logical process IDs!

  3. #18
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    OK let me sleep on it. Something in the am.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #19
    Registered User
    Join Date
    Feb 2010
    Posts
    25
    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?

  5. #20
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    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.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  6. #21
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    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.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  7. #22
    Registered User
    Join Date
    Feb 2010
    Posts
    25
    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.

  8. #23
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Noodling on it. At a minimum I could give you a simple bash script to show compilation...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  9. #24
    Registered User
    Join Date
    Feb 2010
    Posts
    25
    Ok, so I found waitpid(). I also found an example that was supposed to bear 3 children from the same process.

    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 I took the liberty to make it 2 children. Simpler, right? Well, here's the output:
    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
    So it prints out line 1 and 3 which are the same. Why?

  10. #25
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by CheckMyBrain View Post
    Ok, so I found waitpid(). I also found an example that was supposed to bear 3 children from the same process.

    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 I took the liberty to make it 2 children. Simpler, right? Well, here's the output:
    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
    So it prints out line 1 and 3 which are the same. Why?
    Not sure but your switch is a little weird. At the end of case 0: you should have a break unless you intend for the default to execute everytime no matter the value of 'one'. Also there seems to be no code for the child to execute (if pid > 0)....
    Last edited by jeffcobb; 02-10-2010 at 03:44 PM.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  11. #26
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CheckMyBrain View Post
    So it prints out line 1 and 3 which are the same. Why?
    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

  12. #27
    Registered User
    Join Date
    Feb 2010
    Posts
    25
    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.

  13. #28
    Registered User
    Join Date
    Feb 2010
    Posts
    25
    Quote Originally Posted by MK27 View Post
    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.
    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?

  14. #29
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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:
    Code:
    	for(i = 0; i < 3; i++)
    	{
    		one = fork();
    		switch(one)
    		{
    			case 0:printf("My PID is %d, i = %d ", getpid(), i);
    Now watch what happens.
    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

  15. #30
    Registered User
    Join Date
    Feb 2010
    Posts
    25
    Quote Originally Posted by MK27 View Post
    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:
    Code:
    	for(i = 0; i < 3; i++)
    	{
    		one = fork();
    		switch(one)
    		{
    			case 0:printf("My PID is %d, i = %d ", getpid(), i);
    Now watch what happens.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed on parallel processing using fork() in C
    By smithu.simi in forum C Programming
    Replies: 7
    Last Post: 03-27-2009, 07:15 AM
  2. Parallel processing with fork()
    By Mastiff in forum C Programming
    Replies: 7
    Last Post: 08-27-2008, 07:42 AM
  3. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 06:58 AM
  4. Daemon programming: allocated memory vs. fork()
    By twisgabak in forum Linux Programming
    Replies: 2
    Last Post: 09-25-2003, 02:53 PM
  5. file writing crashes
    By test in forum C Programming
    Replies: 25
    Last Post: 08-13-2002, 08:44 AM