O/P is not coming properly....(fork() system call)

This is a discussion on O/P is not coming properly....(fork() system call) within the C Programming forums, part of the General Programming Boards category; Hey, i want to create a child of a child process i have written some codes which is working but ...

  1. #1
    Dweeb dojha00's Avatar
    Join Date
    Feb 2012
    Location
    Global
    Posts
    23

    Lightbulb O/P is not coming properly....(fork() system call)

    Hey, i want to create a child of a child process i have written some codes which is working but it's output is not coming properly...
    Code:
    #include<stdio.h>
    #include<sys/types.h>
    void main()
    {
        pid_t pid,pid1;
        pid=fork();
        if(pid == 0)
        {
            printf("\nThis is the child of main process with pid=%d and parent id= %d",getpid(),getppid());
            pid=fork();
            if(pid == 0)
            {
                printf("\nThis is child of child process with pid = %d and ppid= %d",getpid(),getppid());
            }    
        }
        printf("\nProcess with pid %d is terminating.",getpid());
    }
    AND OUTPUT IS:-

    Process with pid 2185 is terminating.
    This is the child of main process with pid=2186 and parent id= 2185
    Process with pid 2186 is terminating.This is the child of main process with pid=2186 and parent id= 2185
    This is child of child process with pid = 2187 and ppid= 1
    Process with pid 2187 is terminating.
    ================================================== =========
    In the output if process 2186 is terminated then how again it's printing the next line
    which is getting duplicated and that's also without any newline??
    Last edited by dojha00; 02-08-2012 at 10:37 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    How are you printing positive numbers when your test is <= 0 ?

    Also, post your code as text, between [code][/code] tags, not as some screenshot.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Dweeb dojha00's Avatar
    Join Date
    Feb 2012
    Location
    Global
    Posts
    23
    Sorry Salem, i have posted some other program (test version). Now i think it's okay.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Well because you have no else clauses like
    Code:
    if ( pid == 0 ) {
      // child
    } else {
      // parent
    }
    It means line 16 is executed in every process you create (parents and children)
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Dweeb dojha00's Avatar
    Join Date
    Feb 2012
    Location
    Global
    Posts
    23
    Quote Originally Posted by Salem View Post
    Well because you have no else clauses like
    Code:
    if ( pid == 0 ) {
      // child
    } else {
      // parent
    }
    It means line 16 is executed in every process you create (parents and children)
    That's okay but line no. 9 is repeating two times for same process id(2186) whereas it's showing that process no. 2186 is terminated ..
    just see the o/p.
    and second thing if there is no else part then parent will get terminated.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    New lines at start.
    Code:
    #include<stdio.h>
    #include<sys/types.h>
    void main()
    {
        pid_t pid,pid1;
        pid=fork();
        if(pid == 0)
        {
            printf("\nThis is the child of main process with pid=%d and parent id= %d",getpid(),getppid());
            pid=fork();
            if(pid == 0)
            {
                printf("\nThis is child of child process with pid = %d and ppid= %d",getpid(),getppid());
            }    
        }
        printf("\nProcess with pid %d is terminating.",getpid());
    }

    Process with pid 2185 is terminating.
    This is the child of main process with pid=2186 and parent id= 2185
    Process with pid 2186 is terminating.This is the child of main process with pid=2186 and parent id= 2185
    This is child of child process with pid = 2187 and ppid= 1

    Process with pid 2187 is terminating.

    The child prints the blue text, but it is NOT flushed at the point the fork() is called.
    This means both it, AND it's child have it in the output buffer (and also in the forked output buffer).

    Done properly, the underlined text should never have been forked in the first place.

    The output buffer gets flushed when the process exits (both of them), but by then you see the same output twice.

    Most sensible and sane people put the \n at the end (\n at the beginning seems to be some TurboC thing, along with the void main horror).

    Eg.
    Code:
    #include<stdio.h>
    #include<sys/types.h>
    int main()
    {
        pid_t pid,pid1;
        pid=fork();
        if(pid == 0)
        {
            printf("This is the child of main process with pid=%d and parent id= %d\n",getpid(),getppid());
            pid=fork();
            if(pid == 0)
            {
                printf("This is child of child process with pid = %d and ppid= %d\n",getpid(),getppid());
            }
        }
        printf("Process with pid %d is terminating.\n",getpid());
        return 0;
    }
    
    
    Process with pid 2401 is terminating.
    This is the child of main process with pid=2402 and parent id= 2401
    Process with pid 2402 is terminating.
    This is child of child process with pid = 2403 and ppid= 1
    Process with pid 2403 is terminating.
    which is altogether far easier to explain.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot call properly?
    By cmadore in forum C Programming
    Replies: 3
    Last Post: 03-27-2011, 12:12 PM
  2. Replies: 1
    Last Post: 09-19-2010, 06:21 PM
  3. how to properly call an executable from C code?
    By remy06 in forum C Programming
    Replies: 3
    Last Post: 05-14-2009, 03:48 AM
  4. Calling other binaries, use fork/execl or system?
    By spotvt01 in forum Linux Programming
    Replies: 3
    Last Post: 08-16-2008, 04:20 AM
  5. C system call and library call
    By Coconut in forum C Programming
    Replies: 6
    Last Post: 08-22-2002, 11:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21