Eric Hodel (drbrain) wrote,
Eric Hodel
drbrain

  • Mood:

Pthreads

Pthreads are cool, as part of a homework problem I was to implement a monitor that would allow processes each with a unique priority to access a file concurrently so long as the sum of the priorities was less than n.

So I wrote it, tried again, rewrote it, etc. I probably had a solution in there somewhere, but didn't know it because it always ran the threads sequentially (due to some weirdness I'll explain later). Eventually I poked around on google and got a pthread monitor-like dummy implementation. I slapped that in, added the pieces I needed to protect the priority counter and ran it.

Unfortunately there is something odd about how the program reacted to being piped to a file or more. Whenever I did this it executed the threads sequentially, and more than one thread never executed in the monitor at once. When I used the console scrollback, I could see multiple threads executing in the monitor at once.

In all, my solution is fairly elegant. I pass in a function pointer that does the work in the monitor and gets called at the right time. This protects the programmer from their own mistakes and allows them to make a single call when attempting to do work.

Tomorrow I'll have to post a link to my solution.

Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments