Thread: handling exits: main calls a function which creates processes

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    23

    handling exits: main calls a function which creates processes

    Code:
    void call_me(){
    	pid_t child;
    	
    	if((child=fork()) < 0){
    		exit(-1);
    	}
    	else if(child==0){
    		execl("./someexample","./someexample",(char*)0);
    		exit(1);		
    	}
    	else{
    		int status;
    		pid_t   waitpid = wait(&status);
    		exit(0);
    	}
    }
    
    main(){
    	
       call_me();
    }
    Question I have is where do I check the exit(-1), exit(1), exit(0)? Inside main? or check exit(-1),exit(1) inside parent process of call_me and check exit(0) inside main?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    1. If any of the exit() are called, there is no return to main.
    2. If execl() is successful, exit(1) never happens.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    23
    >1. If any of the exit() are called, there is no return to main.

    Salem, I don't understand what you mean by no return to main.
    For example, main had other statements to execute after "call_me" and call_me was called. Inside call_me, one of the exit occurs, does it mean it will never came back to main. Main program will crash or call_me processes parent-child crash, or call_me memory stack will be removed from memory and main function continues?

  4. #4
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    Quote Originally Posted by 911help View Post
    >1. If any of the exit() are called, there is no return to main.
    When exit() is called, the program terminates. Any statement after exit want be executed at all. If you want to pass values you may use return(). e.g:


    Code:
    int test(){
     return(1);
    }
    
    int main() {
      int i;
    
      if(test() == 1)
         printf("test returned 1\n");
      else
         printf("test returned not 1\n");
    
      exit(0);
    }

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    23
    When you fork a process, what memory is passed to the forked process (whole program memory or in the above example, only "call me" function's memory stack is passed to the forked process?)
    fedora 6, gcc 4.1.2

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Each process runs in its own virtual memory space - it inherits nothing from the process that launched it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Elysia View Post
    Each process runs in its own virtual memory space - it inherits nothing from the process that launched it.
    fork() causes a copy of the current process to be made, including the value of all variables.

    Since everything is virtual, in terms of addressing, from the application point of view, you're dealing with an exact copy that points to the same variables, which of course you're not. The value of all variables are copied.

    Case in point:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    
    int main(void)
    {
            size_t i;
            pid_t p = fork();
            char *szMsg = "Testing!";
    
            if(p == 0)
            {
                    /* Child */
                    printf("szMsg is at address: &#37;p\n", szMsg);
                    for(i=0;i<strlen(szMsg);i++)
                    {
                            printf("szMsg[%d] = [%d, %c]\n", i, szMsg[i], szMsg[i]);
                    }
            }
            else if(p > 0)
            {
                    /* Parent */
                    wait(NULL);
                    printf("szMsg is at address: %p\n", szMsg);
                    for(i=0;i<strlen(szMsg);i++)
                    {
                            printf("szMsg[%d] = [%d, %c]\n", i, szMsg[i], szMsg[i]);
                    }
            }
            else
            {
                    fprintf(stderr, "Error.  Unable to fork.\n");
                    return 1;
            }
    
            return 0;
    }
    Output:

    Code:
    >gcc -W -Wall -ansi -pedantic -O2 fork.c
    >a.out
    szMsg is at address: 108d8
    szMsg[0] = [84, T]
    szMsg[1] = [101, e]
    szMsg[2] = [115, s]
    szMsg[3] = [116, t]
    szMsg[4] = [105, i]
    szMsg[5] = [110, n]
    szMsg[6] = [103, g]
    szMsg[7] = [33, !]
    szMsg is at address: 108d8
    szMsg[0] = [84, T]
    szMsg[1] = [101, e]
    szMsg[2] = [115, s]
    szMsg[3] = [116, t]
    szMsg[4] = [105, i]
    szMsg[5] = [110, n]
    szMsg[6] = [103, g]
    szMsg[7] = [33, !]
    >
    Even though the addresses match, you're not dealing with the same exact memory space (because it's virtual).

    So I would argue you are actually inheriting everything, or most of everything, from the parent process. In this context, "inheriting" means all variables retain their same values. In some cases, such as file descriptors, you actually are pointing to the same files, but this is somewhat indepth to get into atm.
    Last edited by MacGyver; 12-30-2007 at 01:44 AM. Reason: spelling correction

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah, so fork does copy mostly everything from the parent process to the child process? I would never have imagined such a thing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I would recommend you try some *nix programming some time. I think you might enjoy it.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Been thinking about linux, but hardware support and all the different versions (for lack of a better name) and all that has steered my away all this time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You might be able to try cygwin if you want to stay in the safety of your Windows machine, but practice with the *nix system calls. It provdes wrappers for them, so you're still able to get into a bit of the mindset of how things should run on Unix or Linux. It may not be the best if you really wanted to get into it, but it might be fun to try.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps this as well.
    http://www.colinux.org/
    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.

  13. #13
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Salem View Post
    Perhaps this as well.
    http://www.colinux.org/
    Every time I try to install this Cygwin it download the package and all goes right and i can get through the installation process as well. But when I try to launch the application, doom the console comes and disappears. And found that one of those Cygiconv-2.dll files and many are missing and on top it says reinstalling will solve this problem.

    Ohh, i have re-installed million times by now lol, no success I gave up. I will try Colinux now. Buy the way have anyone had this problem before.

    ssharish

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Every time I try to install this Cygwin it download the package...
    No, works every time for me.

    Have you got the latest setup.exe from cygwin.com ?
    Do you run setup.exe as admin?
    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.

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    23
    So what's the final verdict?

    Program memory is copied, not function memory stack, even when fork is called from within a function?
    fedora 6, gcc 4.1.2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM