GithubHelp home page GithubHelp logo

cywinski / semaphores-thread-synchronization Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 10 KB

Threads synchronization using semaphores implemented in C.

C 100.00%
semaphores producer-consumer threads

semaphores-thread-synchronization's Introduction

Thread synchronization with use of semaphores

Table of Contents

About

Project for SOI (Operating Systems) course at Warsaw University of Technology. Project solves the Producer-Consumer problem using semaphores. There are three types of Users:

  • VIP Users (Producer)
  • Normal Users (Producer)
  • Reader (Consumer)

Messages sent by VIP Users have higher priority than messages sent by Normal Users.

This solution uses three semaphores:

  • one called sem_full for counting the number of slots that are full in buffer
  • one called sem_empty for counting the number of slots that are empty in buffer
  • one called sem_mutex to make sure the Producer and Consumer do not access the buffer at the same time

Initializing of queue

Queue is initialized in function init_message_queue().

User can set maximal number of messages in queue.

#define MAX_MESSAGES_BUFF

User can also set maximal number of signs in every message.

#define MAX_MESSAGE_SIZE

Usage

To run a test, user needs to uncomment particular test function, which are called from main() function in file semaphores.c.

Compiling semaphores.c:

cc -o semaphores semaphores.c -lpthread
./semaphores

Tests

test_both_users() creates:

  • 2 threads of Normal Users
  • 1 thread of VIP User
  • 1 thread of reader

In this test communication of both types of Users is tested. Many messages sent by Normal Users, despite being sent before VIP messages, are being read after VIP messages, because VIP messages have higher priority. All messages are putting to queue one at a time.

test_normal_users() creates:

  • 2 threads of Normal Users
  • 1 thread of reader

In this test communication of one type of Users is tested. Messages are being read in the order of being putted to queue.

test_vip_users() creates:

  • 2 threads of VIP Users
  • 1 thread of reader

In this test communication of one type of Users is tested. Messages are being read in the order of being putted to queue.

test_empty_buffer_normal() creates:

  • 1 thread of Normal User
  • 1 thread of reader

In this test reader wants to read a message from empty buffer. Reader is blocked until first message is putted to queue.

test_empty_buffer_vip() creates:

  • 1 thread of VIP User
  • 1 thread of reader

In this test reader wants to read a message from empty buffer. Reader is blocked until first message is putted to queue.

test_full_buffer_normal() creates:

  • 1 thread of Normal User
  • 1 thread of reader

In this test Normal User wants to put a message to full buffer. Normal User is blocked until first message is removed from queue.

test_full_buffer_vip() creates:

  • 1 thread of VIP User
  • 1 thread of reader

In this test VIP User wants to put a message to full buffer. VIP User is blocked until first message is removed from queue.

semaphores-thread-synchronization's People

Contributors

cywinski avatar

Watchers

 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.