Coder Social home page Coder Social logo

sem_t-exercise's Introduction

Practice with <semaphore.h>

This exercise is an opportunity to see how semaphores applied in a C program. queue.h and queue.c are provided in their entirety. Your goal is to complete the code in template.c. answer.c is the answer key with one possible implementation, but you are encouraged not to look at the solution until you've made an attempt on your own. template.c contains comments to guide you and a file header with helpful documentation pages you may want to look at.

The program is an interesting contrived scenario in which you want to cap the number of threads running by splitting the data they need to operate on and guarding it with a semaphore. For more information, see the file header in template.c.

Bonus conceptual question: The usual way to limit the number of active threads is with a thread pool, which contains some number of threads that may be woken to perform a task. Critically, they are not destroyed and recreated in between tasks; they are simply put back to sleep and woken up as needed to eliminate the overhead of creating and destroying threads. How might a semaphore be applied to implement a thread pool?

View possible answer

We could implement a thread pool with a similar strategy to that used in this program. Instead of calling pthread_create() whenever the semaphore value is greater than 0, we could send a signal to an already-created thread that is in the TASK_INTERRUPTIBLE state. When it finishes its task, we would increment the semaphore and then return the thread to the TASK_INTERRUPTIBLE state.

sem_t-exercise's People

Contributors

hkcountryman avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.