Find the answer to your Linux question:
Results 1 to 5 of 5
Hai Im doing a study in how to use fork(). I test a simple code and it is as follow Code: int pid; pid = fork(); printf("%d", pid); sleep(20); Then ...
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Posts
    19

    fork() basics

    Hai

    Im doing a study in how to use fork(). I test a simple code and it is as follow
    Code:
    int pid;
    
    pid = fork();
    printf("%d", pid);
    sleep(20);
    Then i compile and run the code, it sleeps for 20 seconds so that i can view the process list (ps -ux). My new process ID created which is also printed out to the screen was 4744. However, when I view the process list, i also found another process id running together with the new process id created and this process id is 4743. Im not sure where this process id comes from. Hope can get some advice for you guys. Another thing, what does setsid() do and what is the difference if I use the data type of the process id as int with data type pid_t. Thanks

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    open a terminal and info fork or man fork...it will explain whats going on...Hope this helps..Gerard

    welcome to thelinux forums

  3. #3
    Just Joined!
    Join Date
    Dec 2007
    Posts
    19
    Thanks gerard. I have gone through the man page before I post this question. I have certain view on my understanding but the reason I post this question is to certify my uncertainty. However, I now understand what is going on behind the scene. It is not because of the fork process but it was just a silly thing i missed which is the command running in the terminal is also a process with a unique process ID. Sorry for giving basic questions.

    But then I'm not quite clear about setsid() function. Im not sure, when we open a new session ID for a process, does it exits from the terminal shell parent and will directly be a child of init() or it will remain under shell parent but it has its own independant group ID. I just need to clear up my understanding the hierarchy of the parent child processes. Thanks again.

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Try this link...it explains a use of setsid()...

    Linux Daemon Writing HOWTO

  5. #5
    Just Joined!
    Join Date
    Dec 2007
    Posts
    19
    Thanks gerard. The link really helps. Thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...