Like Tree1Likes
  • 1 Post By anduril462

A Qn on Posix threads

This is a discussion on A Qn on Posix threads within the C Programming forums, part of the General Programming Boards category; Hi, A sample program using POSIX threads (with basic pthread functions) runs well perfectly for 1 thread/2 threads. But for ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    18

    A Qn on Posix threads

    Hi,

    A sample program using POSIX threads (with basic pthread functions) runs well perfectly for 1 thread/2 threads. But for more than 2 threads it fails with Segmentation fault.
    The function which is threaded, makes use of a variable 'flag' & contains an if statement. If the "if" condition is true, flag will be set to 0 and the loop will be break in the next iteration. To test the next case, again the flag is set to 1.
    The server has 2 cores.
    The memory allocation statement is:

    // params is a structure
    params *thread_params = (params*) malloc( nthreads * sizeof(params*));

    What could be the reason for "seg fault" here?

    Let me know if the complete program is required.

    Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    You compile with the -g flag.
    You run the code in the debugger.
    You get a much better idea of where the problem REALLY is.

    As opposed to posting 1 line of code, and hoping we can guess what it is you're seeing.
    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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Code:
    params *thread_params = (params*) malloc( nthreads * sizeof(params*));
    If you're programming in C, there is no need to cast the return value of malloc, and doing so can hide more insidious issues like failing to include the stdlib.h header required for malloc, which will cause further problems. On the bright side of things, good job on your sizeof() call; using sizeof(*type) is the right way to do it.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,438
    Quote Originally Posted by rags_to_riches View Post
    On the bright side of things, good job on your sizeof() call; using sizeof(*type) is the right way to do it.
    Actually, the sizeof in your malloc is incorrect. I read it wrong too at first. You did params*, which is the size of one pointer, not one param (whatever that is). The preferred malloc usage is:
    Code:
    params *thread_params = malloc(nthreads * sizeof(*thread_params));
    The name of the variable goes in the sizeof call, with a star in front of it.
    Last edited by anduril462; 06-11-2011 at 11:18 AM. Reason: forgot closing code tag
    rags_to_riches likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Posix Threads
    By tsiknas in forum Linux Programming
    Replies: 2
    Last Post: 11-28-2009, 03:07 AM
  2. POSIX threads
    By Boylett in forum Linux Programming
    Replies: 2
    Last Post: 09-10-2008, 01:33 PM
  3. Using POSIX threads in C on Linux
    By muthus in forum C Programming
    Replies: 1
    Last Post: 01-19-2008, 04:34 PM
  4. POSIX-Threads
    By posixunil in forum Linux Programming
    Replies: 2
    Last Post: 04-17-2007, 06:43 AM
  5. creating POSIX threads in UNIX
    By v3dant in forum C++ Programming
    Replies: 7
    Last Post: 09-22-2004, 05:07 PM

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