0

I basically have to create a simplified device controller for an OS. I need to create synchronous and asynchronous functions to input/output a word of data to a queue and when the "device" is ready, it is signalled & function returns. The synchronous functions block themselves until the device removes the word from queue & signals function (output) and until request is satisfied (input).

I'm not sure how to handle the synchronous functions where they need to block until request is handled. How can I implement the blocking aspect? Would using a pthread lock do what I need?

Thanks.

Caulay
  • 43
  • 7
  • Blocking operations are handled by the kernel scheduler. The generic userspace end of a blocking I/O-type operation is a condition variable or a semaphore, but several standard I/O mechanisms like `read` and `write` also support blocking directly. – Kerrek SB Apr 05 '14 at 23:05

1 Answers1

1

If you are at the user space: Yes, use a pthread mutex. On the consumer, you will pthread_mutex_lock if no data available. On the producer, you will pthread_mutex_unlock after new data has been committed to the queue.

Felipe Lavratti
  • 2,887
  • 16
  • 34