multithreading buffer help

This is a discussion on multithreading buffer help within the C Programming forums, part of the General Programming Boards category; I'm trying to implement a pipelined multithreaded version of the grep command for a class project. The pipeline will have ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    6

    multithreading buffer help

    I'm trying to implement a pipelined multithreaded version of the grep command for a class project. The pipeline will have 5 stages and I need 4 buffers to hold filenames that are used between each stage. The buffersize is one of the arguments passed from the terminal when the command is called (i.e. ./pipelinegrep <buffsize> ...etc)

    I'm completely lost as to how to create the buffers so that they can be used by different stages of a pipeline but not define the size of the buffer until the main method where I can use the arguments passed to the function call.

    My initial thought was to pass the two buffers that a thread would use as the 4th argument for pthread_create, however this is only one argument so I don't know how to pass it two buffers.

    I've got to finish this by tonight so any help would be much appreciated let me know if I wasn't clear enough on anything or if any other information would help.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,468
    You can malloc memory once you know the size, or if your compiler supports it, use the variable-length array (VLA) feature of C99. Global variables will be available to all threads, just remember to use a mutex or something to control access between threads.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    I'm not sure if I follow what you're saying... do I have to declare the buffer as:
    char buff;
    or
    char buff[];

    This line would be outside of any method. Then in the main method I have the variable buffsize which is defined according to the arguments passed to the program when it is run from the terminal. How do I use buffsize to set the size of buff?

    I know very little about c itself. I'm much more familiar with java as that was what I was taught in high school unfortunately. My program has to be able to run on one of the servers at my school so I'm not sure if it supports C99 but I assume so. Again I don't know enough about c to be able to tell you for sure.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,468
    Code:
    char *buff;  // global variable, to be used with malloc later
    ...
    buff = malloc(buffsize * sizeof(*buff));  // in main, allocate buffsize bytes
    Note, since *buff is a char, which is defined to have a sizeof of 1, you can technically leave it off and just do malloc(buffsize), but I wanted to show it to you if you ever use malloc for an array of something other than chars, like ints or structs or whatever. That's safer than doing sizeof(char) since if you ever change buff's type, the malloc will still allocate the right size.

    VLAs don't make sense with global variables in your case, so disregard that option. Few compilers support the entire C99 set of features, but most major compilers support many of the features. VLAs are pretty simple to implement, so your school's compiler probably does, but don't worry about it. It would behoove you to find out what actual compiler and version you use at school however, and what features/capabilities it has. Ask your teacher.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    sweet it compiles thanks for the help you're a life saver now i just have to figure out how to use readdir and opendir...

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,468
    You're welcome. Check the man pages ("man readdir" and "man opendir"), and google for examples, there's tons out there I'm sure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading (flag stopping a thread, ring buffer) volatile
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 05-19-2009, 07:27 AM
  2. Multithreading
    By ksarkar in forum Windows Programming
    Replies: 6
    Last Post: 06-03-2005, 03:59 PM
  3. fgets(buffer,sizeof(buffer),stdin);
    By linuxdude in forum C Programming
    Replies: 2
    Last Post: 10-28-2003, 09:41 AM
  4. Multithreading
    By JaWiB in forum Game Programming
    Replies: 7
    Last Post: 08-24-2003, 09:28 PM
  5. Multithreading
    By Lord CyKill in forum C++ Programming
    Replies: 4
    Last Post: 06-30-2003, 11:14 AM

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