Thread: File input and pthreads

  1. #16
    Registered User
    Join Date
    Nov 2013
    Posts
    27
    Quote Originally Posted by tabstop View Post
    Your two threads are independent of each other, so why would you expect them to alternate? Also each thread is locking once and then unlocking eleven times, which doesn't seem appropriate.

    If you want calc to depend on the reader thread, then presumably you would have the reader thread put the data somewhere and the calc thread use that data, rather than also reading from the file separately.
    That was my original question is how to store the data??

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Seán A Haon View Post
    That was my original question is how to store the data??
    And you got a bunch of answers at the start of the thread: some variable that is visible to both functions (passed-in pointers would work so far as I can see).

  3. #18
    Registered User
    Join Date
    Nov 2013
    Posts
    27
    Quote Originally Posted by Codeplug View Post
    >> I'v had a look at that but can't seem to compile it to see end results
    gcc -Wall -pthread -D_XOPEN_SOURCE=700 -std=c99 main.c

    Post the compiler errors/warnings if you get any. You need to understand the code in the link I posted in order to complete your assignment.

    gg
    This is what I'm getting..

    c: In function ‘consumer’:
    c:16:16: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
    int myid = (int)p;

  4. #19
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The standard does not guarantee a round trip int->void*->int conversion. It just happens to work on most desktop systems and so I used it as shortcut for passing a int by-value to the thread function.

    Once you understand the rest of the code and why it works, you should be able to complete your assignment. Let us know if you need any of it explained.

    gg

  5. #20
    Registered User
    Join Date
    Nov 2013
    Posts
    27
    Cheers man. I can kind of follow it but can't really get my head around applying it to my assignment. I have until 18.00 tomorrow UTC time so late night tonight haha

  6. #21
    Registered User
    Join Date
    Nov 2013
    Posts
    27
    I've just been told I can only use signals in this assignment so back to square one if anyone has any advice??

  7. #22
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    What kind of "signals"? As in <signal.h>, sigaction, etc? If so, that doesn't make sense if using pthreads. Are you supposed to be using fork() instead of pthreads?

    gg

  8. #23
    Registered User
    Join Date
    Nov 2013
    Posts
    27
    I'm supposed to use signal handler and sigset etc.. Here's the requirements..

    You are to write a C program that prints the sum of each pair of integers in a text file. (The file whose contents are to be printed is supplied as a command line parameter.) Your program must function as follows:

    • One thread (the reader) reads a pair of integers from the input file and prints them to the screen.
    • Another thread (the calculator) then sums the numbers just loaded by the reader and prints the result to the screen.
    • The reader blocks while waiting for the calculator to print the latest result.
    • The calculator blocks while waiting for the reader to submit a pair of integers for addition.
    • Before reading the next pair of integers from the file, the reader sleeps for a random number of microseconds (up to 10000).
    • After summing the latest two integers loaded by the reader, the calculator sleeps for a random number of microseconds (up to 10000).
    • A main thread coordinates the actions of the reader and calculator threads.
    • Unix signals are used for inter-thread communication and to avoid busy-waiting.
    • The main thread cancels the other threads once the last pair of integers has been summed.
    • Each cancelled thread prints out a farewell message as it exits.


    I only seen in the FAQs that I can only use signals and not conditional cars or mutexes .. Glad to say I'm completely lost now..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Naming an output file based on an input file name
    By mbxs3 in forum C Programming
    Replies: 4
    Last Post: 09-07-2013, 06:30 PM
  2. Replies: 9
    Last Post: 12-05-2012, 01:11 PM
  3. Replies: 1
    Last Post: 05-13-2012, 10:00 AM
  4. Replies: 2
    Last Post: 03-05-2012, 10:35 AM
  5. Help Loading DLL File/pthreads for Win-32
    By maththeorylvr in forum Windows Programming
    Replies: 0
    Last Post: 10-25-2005, 08:49 PM

Tags for this Thread