CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2003
    Location
    Nova Scotia, Canada
    Posts
    7

    pthread_create returns -1?

    Hi all.
    Anyone had any trouble with pthread_create returning -1? I'm not sure what to do about it, so any and all suggestions would be appreciated!

    Thanks in advance!

    // Code:
    /////////////////////////////////////////////////////////////////

    coid *Client(coid *);

    void main(){
    ......
    pthread_t clientid[MAXCLIENTS] /* MAXCLIENTS == 32 */

    pthread_attr_init(&attr);
    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
    ......
    for ( ; ; ){
    ......
    pthread_create(&clientid[i], &attr, Client, (void *) i);
    ......
    }
    }

  2. #2
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    Is it a real code? But here U dont check for pthread_create return code.
    for(;; ) ???
    Did U try to create only 32 as declared above?

    Possible error for pthread_create is only EAGAIN, which means no more system resources available,
    ofcourse if U write for(;; ) and try to create unbounded count of threads.
    "UNIX is simple; it just takes a genius to understand its simplicity!"

  3. #3
    Join Date
    Feb 2003
    Location
    Nova Scotia, Canada
    Posts
    7
    Attached is the entire server code.

    I will add checking to make sure I only allow 32 clients.

    http://www.opengroup.org/onlinepubs/...ad_create.html

    ERRORS

    The pthread_create() function will fail if:

    [EAGAIN]
    The system lacked the necessary resources to create another thread, or the system-imposed limit on the total number of threads in a process PTHREAD_THREADS_MAX would be exceeded.
    [EINVAL]
    The value specified by attr is invalid.
    [EPERM]
    The caller does not have appropriate permission to set the required scheduling parameters or scheduling policy.

    The pthread_create() function will not return an error code of [EINTR].
    Last edited by malletelf; February 12th, 2003 at 05:11 AM.

  4. #4
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    It returns -1 on the 1-st step of iteration?
    Look system errno variable for error code.
    "UNIX is simple; it just takes a genius to understand its simplicity!"

  5. #5
    Join Date
    Feb 2003
    Location
    Nova Scotia, Canada
    Posts
    7
    Yes, it returns -1 on the first iteration.

    perror/errno return an error code of 0.

    Were you able to see my attached files? That was the first time I've tried and I wasn't sure how it worked.

  6. #6
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    Strange that 0
    That means no error.
    Are U try setscope for return code?
    "UNIX is simple; it just takes a genius to understand its simplicity!"

  7. #7
    Join Date
    Feb 2003
    Location
    Nova Scotia, Canada
    Posts
    7
    I tried setscope, and everything else dealing with the thread. All variables are correct and all parameters seem to be correct.

    I have class shortly, I can ask the professor. Hopefully he can shed some light on it, but I'm not so sure.

    As this assignment is due in 15 1/2 hours, and there's plenty left to do, don't be surprised to see more posts here, lol.

  8. #8
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    U can attach here your sources if it will be possible for U. May be it will give some light.
    "UNIX is simple; it just takes a genius to understand its simplicity!"

  9. #9
    Join Date
    Feb 2003
    Location
    Nova Scotia, Canada
    Posts
    7
    Hopefully these clear things up a little.

    Thanks for all your help!!
    Attached Files Attached Files

  10. #10
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    I compiled your server, run it, then with telnet opened a connection. So all was OK! No any error with pthread_create(). perror gave "Invalid seek" for pthread_attr_setscope(). Why do U use it? This default value for posix linux threads, as I remember, or U use another UNIX?
    So for some time it works until telnet breaked it. I put printf() into Client() - function and it worked.
    So I hadn't error with pthread_create().
    "UNIX is simple; it just takes a genius to understand its simplicity!"

  11. #11
    Join Date
    Feb 2003
    Location
    Nova Scotia, Canada
    Posts
    7
    I'm working from a Sparc server running SunOS 5.8.

    I'll have to say it is the computers that make the difference, because even doing what you did, I can't make it work.

    Sometimes if feels as though I'm beating a dead horse with the network programming we've been doing.

  12. #12
    Join Date
    Feb 2003
    Location
    Nova Scotia, Canada
    Posts
    7
    Well, what can I say?
    I'm terribly sorry to have wasted your time on this.

    Turns out that I didn't include the proper compiler directives in my make file.

    I guess that's why I'm a student though.

    Thanks again.

  13. #13
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    I didn't work with Spark platform and SunOS. This is very simple sample U 've provided. And quike view didn't recognise any problems with network part. Of course, it has it, but they cannot make problem U have.
    So I don't know, may be it depends of Sun posix realization or something else.
    Did U try this without pthread_attr_setscope()?

    Else why fork()? U create a child process, then create thread in it, but then after a little a child process is ended and thread U created too.
    It is enough to call pthread_create() to create a client thread.
    Last edited by dimm_coder; February 13th, 2003 at 03:08 AM.
    "UNIX is simple; it just takes a genius to understand its simplicity!"

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured