Thread Prog in C language (seg fault)

This is a discussion on Thread Prog in C language (seg fault) within the C Programming forums, part of the General Programming Boards category; I am able to create multiple threads and a shared memory segment... Now I want to write into one block ...

  1. #16
    Registered User
    Join Date
    Jun 2008
    Posts
    14

    Thread Prog in C language (seg fault)

    I am able to create multiple threads and a shared memory segment...

    Now I want to write into one block of shared memory (need to divide the single shared memory into multiple blocks) as follows:

    blockid, threadid, and data.

    For example one single block of a shared memory should contain blockid, threadid and data.

    where the blockid will be block1, block2, block3 and so on......
    threadid will be thread1, thread2, thread3 and so on....
    data will be user input data "string format"

    Once I am able to write as above, then I should be in a position to read the data from the reader thread by specifying the threadid and blockid and also if the reader threads want to modify the data, so it can modify the data.

    On top of it we need to assure that either of the reader thread or writer thread should access any particular block of shared memory at any instance of time, but not both at a time.

    I am sending you the code attached along with this post, could you please help me out.....
    Attached Files Attached Files

  2. #17
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,660
    The easiest thing to do is to create a structure of containing all the shared data for each block. Then use the shared memory as an array of these blocks.
    Code:
    typedef struct  
    {
        int block_id;
        int thread_id;
        char data[1024];
    } DataBlock;
    You could also put a pthread_mutex_t in each block and use it to synchronize access to each block. In order to use a pthread mutex in shared memory (by multiple processes), you have to call pthread_mutexattr_setpshared(). This is not supported by all systems. If yours doesn't support it, then you'll have to use a SysV or Posix semaphore instead.

    Header files should not contain definitions (declarations that allocate storage). Your header.h has several definitions (the global variables). Each source file that includes that header will get it's own copy of each global (which can result in linker errors). If you want to share a global variable across source files, you put the definition of the global in only one source, then put an extern declaration in the header. For example:
    main.c - "int g_global;"
    header.h - "extern int g_global;"

    gg

  3. #18
    Registered User
    Join Date
    Jun 2008
    Posts
    14

    Thread Prog in C language (seg fault)

    Many many thanks to you for providing hints. But I have no idea/clue that how I'll pass this structure to writer thread, and once I am able to pass this structure to writer thread and the writer thread is able to write some data into it, then again how do I pass this structure to reader thread. "setpshared() is supported on AIX also, but I am sorry to say that don't know how to use it.

  4. #19
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,660
    >> how I'll pass this structure to writer thread, ... how do I pass this structure to reader thread.
    The thread functions take a void* parameter, which you give when calling pthread_create. Your code already passes a integer index to the thread functions. You can continue to use that as an index into the DataBlock array.

    >> setpshared() is supported on AIX also, but ... don't know how to use it
    pthread_mutexattr_init
    pthread_mutexattr_setpshared
    pthread_mutex_init
    Check your man pages. There's example code here: http://www.opengroup.org/onlinepubs/...r_destroy.html

    gg

  5. #20
    Registered User
    Join Date
    Jun 2008
    Posts
    14

    Thread Prog in C language (seg fault)

    I have created the structure as follows:

    Code:
    typedef sturct 
    {
            int thread_id;
            int block_id;
            char string[1024];
    }DataBlock;
    
    DataBlock *writeparm;
    But my writer fails, it doesn't return after writing. It stops immediately after writing the data and it doesn't pass control to the reader and stops further execution of the program
    Attached Files Attached Files

  6. #21
    Registered User
    Join Date
    Jun 2008
    Posts
    14

    Thread Prog in C language (seg fault)

    "Codeplug"May I have your kind attention please on this. I don't why my writer function is failing.....

    One more question I would like to ask on this....

    You can continue to use that as an index into the DataBlock array.

    When I tried to pass structure variable (DataBlock *writeparm), then I got the compilation error.....

    I thought of doing it in differenet way, like passing 2 variable to writer function but that also doesn't work. Right now I am passing one variable in the writer function...

    void *writer(void *parm)

    I tried this way void *writer(void *parm, char *file), but this gives me lots of lots of error.....

    Can we pass 2 variables to writer function, if yes then how can we....

  7. #22
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Can you post your "fixed" code. I think you are under-allocated, by the way. But I would need to see the newer code to confirm whether or not you have fixed that problem yet.

  8. #23
    Registered User
    Join Date
    Jun 2008
    Posts
    14

    Thread Prog in C language (seg fault)

    I am trying to divide the shared memory into multiple blocks in such a way that

    Problem 1:
    writer thread1 should write into first block of shared memory and another thread reader thread1 should read and modify the date by referring to a particular block of shared memory

    Attempt which I made:

    I have created a structure as follows and using that structure pointer trying to write some data into shared memory, and with another structure pointer variable I am trying to read the data. But both the reader and writer failing and the reason is not known to me. Please provide me some input on this


    Code:
    typedef struct{
            int   block_id;
            int   thread_id;
            char  string[1024];
    }DataBlock;
    
    DataBlock* writeparm;
    DataBlock* readparm;

    Please see the writer.c file and reader.c file in the attachment.

    Problem 2:

    I am trying pass one more variable to reader and writer functions in reader and writer file but this also fails, as of now I am passing only one variable to these functions as parameters.


    Code:
    void *reader(void *);
    void *writer(void *);
    What I am trying to attempt:

    If I am able to pass two variables to both these function then other variable I can use as a file pointer and hopefully that may sort out the problem, but this also gives me strange kind of error. This is what I am trying to do

    Code:
    void *reader(void *, void *);
    void *writer(void *, void *);
    But when I tried this I couldn't succeed... Please give some on this also.

    P.S-: I have attached the code (shmem.zip), please have a look at it.
    Attached Files Attached Files

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault
    By hka26 in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2007, 01:38 AM
  2. Replies: 5
    Last Post: 11-04-2006, 05:39 PM
  3. seg fault problems
    By nadamson6 in forum C++ Programming
    Replies: 11
    Last Post: 12-27-2005, 02:26 PM
  4. Pointer To Functions = Seg Fault
    By misplaced in forum C++ Programming
    Replies: 3
    Last Post: 04-05-2005, 08:03 AM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM

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