Results 1 to 9 of 9

Thread: wait() system call.

  1. #1
    Join Date
    Aug 2011
    Beans
    9

    wait() system call.

    Hello everyone,
    I am stuck with wait() system call in linux.


    #include<stdio.h>
    #include<unistd.h>

    void main()
    {
    int pid;
    if((pid = vfork()) == 0)
    execl("/bin/echo","echo","Hello There",(char *)0);
    else
    wait();
    }

    OUTPUT:
    Hello There

    ---------------------------------------------------------------

    #include<stdio.h>
    #include<unistd.h>

    void main()
    {
    int status;
    int pid;
    if((pid = vfork()) == 0)
    execl("/bin/echo","echo","Hello There",(char *)0);
    else
    wait(&status);
    }

    OUTPUT:
    Hello There


    //------------------------------------

    The above two programs run well on my system without any compilation warning.

    The difference in the two programs is that first one what call to "wait()" without any argument & the second one call it with and 'int * argument'.

    How come a system call have more that one interface ??
    Can someone please explain me whats happening ...

    Thank You.

  2. #2
    Join Date
    Dec 2008
    Location
    Deep Woods of PA
    Beans
    699
    Distro
    Kubuntu 11.10 Oneiric Ocelot

    Re: wait() system call.

    Quote Originally Posted by ankit3111 View Post
    Hello everyone,
    I am stuck with wait() system call in linux.

    The above two programs run well on my system without any compilation warning.

    The difference in the two programs is that first one what call to "wait()" without any argument & the second one call it with and 'int * argument'.

    How come a system call have more that one interface ??
    Can someone please explain me whats happening ...

    Thank You.
    You have an implicit declaration of wait(), which linker then links to the implementation of wait provided by standard C library. It's very likely that the call is being interpreted as wait(NULL).

    I would suggest when you compile, which I assume is on Ubuntu to use -Wall flag that way you can see the warnings that are actually being suppressed.

    Further read
    Code:
    man -s 2 wait
    and include the headers it suggest that way you are not relying on compiler for interpretation of what it is that you have written.
    Regards,

    Karlson

  3. #3
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: wait() system call.

    Quote Originally Posted by karlson View Post
    It's very likely that the call is being interpreted as wait(NULL).
    What makes you think that? I think it's very likely that you are wrong.

    EDIT: Case in point:

    Code:
    firas@applejack ~ % cat test.c                      
    #include <stdio.h>
    
    int main(void)
    {
            foo();
            return 0;
    }
    
    void foo(int* p)
    {
            printf("argument is %p\n", p);
            printf("NULL is %p\n", (int*)NULL);
    }
    firas@applejack ~ % gcc -o test test.c 
    test.c:9:6: warning: conflicting types for ‘foo’
    test.c:5:9: note: previous implicit declaration of ‘foo’ was here
    firas@applejack ~ % ./test 
    argument is 0x1
    NULL is (nil)
    firas@applejack ~ % gcc -m32 -o test test.c
    test.c:9:6: warning: conflicting types for ‘foo’
    test.c:5:9: note: previous implicit declaration of ‘foo’ was here
    firas@applejack ~ % ./test                 
    argument is 0x8048410
    NULL is (nil)
    Last edited by Bachstelze; September 2nd, 2011 at 08:51 PM.

  4. #4
    Join Date
    Dec 2008
    Location
    Deep Woods of PA
    Beans
    699
    Distro
    Kubuntu 11.10 Oneiric Ocelot

    Re: wait() system call.

    Quote Originally Posted by Bachstelze View Post
    What makes you think that? I think it's very likely that you are wrong.
    I'll have to retest this. The code you posted doesn't even compile for me.
    Last edited by karlson; September 2nd, 2011 at 10:04 PM.
    Regards,

    Karlson

  5. #5
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: wait() system call.

    Quote Originally Posted by karlson View Post
    Can't argue with that. I simply don't know what the behavior of wait is when there is nothing on the stack.
    The point is that there is not "nothing" on the stack. The function will just pick whatever is at the location where the argument is supposed to be. The code of the actual function is irrelevant, the method of argument-passing is a convention thal all functions must follow (otherwise nothing could work).

  6. #6
    Join Date
    May 2006
    Beans
    1,790

    Re: wait() system call.

    Quote Originally Posted by karlson View Post
    Can't argue with that. I simply don't know what the behavior of wait is when there is nothing on the stack.

    Given that nothing was pushed on the stack before the wait call pop from ESP is likely to return 0. In other instances it might not be the case. Don't have any other cases or wait's assembly to say otherwise.

    If I am wrong I stand corrected.
    The user program looks as if the process started life at the first line of 'main', but in reality 'main' is often called from a function '_start', which sets up the environment array envp, and also argc and argv. So there will be things on the stack.

    Even if that was not the case, the local variable 'pid' is maybe on the stack. Only maybe, because it's actually not used at all, and can be optimized away.

    If the stack is really empty when you attempt to look at an element on it, you will probably get a segmentation violation error.

  7. #7
    Join Date
    Dec 2008
    Location
    Deep Woods of PA
    Beans
    699
    Distro
    Kubuntu 11.10 Oneiric Ocelot

    Re: wait() system call.

    Ok. I am getting stupider... Must sleep.
    Regards,

    Karlson

  8. #8
    Join Date
    Aug 2011
    Beans
    9

    Re: wait() system call.

    Quote Originally Posted by Bachstelze View Post
    What makes you think that? I think it's very likely that you are wrong.

    EDIT: Case in point:

    Code:
    firas@applejack ~ % cat test.c                      
    #include <stdio.h>
    
    int main(void)
    {
            foo();
            return 0;
    }
    
    void foo(int* p)
    {
            printf("argument is %p\n", p);
            printf("NULL is %p\n", (int*)NULL);
    }
    firas@applejack ~ % gcc -o test test.c 
    test.c:9:6: warning: conflicting types for ‘foo’
    test.c:5:9: note: previous implicit declaration of ‘foo’ was here
    firas@applejack ~ % ./test 
    argument is 0x1
    NULL is (nil)
    firas@applejack ~ % gcc -m32 -o test test.c
    test.c:9:6: warning: conflicting types for ‘foo’
    test.c:5:9: note: previous implicit declaration of ‘foo’ was here
    firas@applejack ~ % ./test                 
    argument is 0x8048410
    NULL is (nil)
    This Worked for me.
    thanks a lot !!!

    This works as long as the prototype for foo [void foo(int* p);] is not declared at the start, once it is declared the program won't compile giving error message "too few arguments to function ‘foo’".

    The same is the case with wait();
    until we omit the statement #include<wait.h> "wait()" would work perfectly fine without any warning, but once we have included the include statement the program won't compile giving error message "too few arguments to function ‘wait’".

    Thank you Mr.Firas for the help.
    Last edited by ankit3111; September 2nd, 2011 at 11:15 PM.

  9. #9
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: wait() system call.

    Quote Originally Posted by ankit3111 View Post
    This Worked for me.
    thanks a lot !!!
    It was just an example, you should never do that!
    Last edited by Bachstelze; September 2nd, 2011 at 11:21 PM.

Tags for this Thread

Bookmarks

Posting Permissions

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