Coder Social home page Coder Social logo

guykabiri / gcd-calculator-in-c-using-threads Goto Github PK

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

An Exercise as a part of the `Operation Systems` course. Calculate the gcd of pairs of numbers using threads and mutex lock in C.

Makefile 1.77% C 98.23%
c gcd-calculator gcd thread threads mutex-lock mutex lock

gcd-calculator-in-c-using-threads's Introduction

GCD Calculator in C using Threads

A small Unix / Linux assignment in the 'Operation Systems' course which calculates the GCD of pairs of numbers. There are 2 versions, one that using a process for each pair, and one that uses a constant number of processes and communicates with the main process using pipes.

V1

V1 reads pairs of numbers from STDIN, but can get a file name in the command line argument to read from it. All the pairs are saved in a shared memory array so each thread can access it. Each thread gets specific indexes in that array to read and write to so no need for any lock. The main thread joins all of the threads and then prints the pairs and their calculated gcd.

Usage

Run ./v1 in the command line in the v1 folder, or ./v1/v1 in the main folder, to user STDIN as an input source. Press ctrl + d to stop.

Optional:

Run ./v1 file_name in the command line in v1 folder, or ./v1/v1 file_name in the main folder. Where file_name is the file to read from, it should contain pairs of numbers separated with spaces or tabs, each pair in a separate row.

V2

V2 reads pairs of numbers from STDIN, but can get a file name in the command line argument to read from it. All the pairs are saved in a shared memory array so each thread can access it. Each thread takes one pair from the array and calculates it's gcd, a mutex lock is used to synchronize uses of the array. Each thread prints the pair and the calculated gcd when done.

Usage

Run ./v2 in the command line in the v2 folder, or ./v2/v2 in the main folder, to user STDIN as an input source. Press ctrl + d to stop.

Optional:

Run ./v2 file_name in the command line in v2 folder, or ./v2/v2 file_name in the main folder. Where file_name is the file to read from, it should contain pairs of numbers separated with spaces or tabs, each pair in a separate row.

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.