Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pthread Sorting Simulator

A C command-line program that simulates a simple two-thread sorting algorithm using POSIX threads. The program reads integers from a file, coordinates two worker threads with mutexes and condition variables, and prints the sorted array with per-thread and total swap counts.

Overview

The simulator uses two threads:

  • T1 checks adjacent pairs starting at even indexes: A[0]/A[1], A[2]/A[3], etc.
  • T2 checks adjacent pairs starting at odd indexes: A[1]/A[2], A[3]/A[4], etc.

The threads alternate using pthread_cond_wait() and pthread_cond_signal(). Sorting stops when both threads complete consecutive passes without making a swap.

Features

  • C implementation using POSIX threads.
  • Thread synchronization with mutexes and condition variables.
  • Shared swap counter protected by a mutex.
  • Input validation for integer-only files.
  • Supports up to 200 integers.
  • Includes sample input and output files.
  • Simple Makefile for build and run commands.

Proof and Validation Evidence

Evidence Where to inspect it What it proves
Sample output docs/sample-output.txt The repository includes a captured run result for review.
Synchronization notes docs/thread-synchronization.md Mutex and condition-variable behavior is explained outside the code.
Build automation Makefile The C program has a repeatable compile/run path.
Example input data/example-input.txt Reviewers can reproduce the documented run with sample data.
Sorting source src/sort.c The pthread synchronization implementation is inspectable in source.

Project Structure

pthread-sorting-simulator/
|-- include/
|   `-- sort.h                                # Shared sort/synchronization declarations
|-- src/
|   |-- main.c                                # Program entry point and file input flow
|   `-- sort.c                                # pthread sorting and shared-state logic
|-- data/
|   |-- ToSort
|   |-- example-input.txt
|   `-- small-input.txt
|-- docs/
|   |-- sample-output.txt
|   `-- thread-synchronization.md
|-- Makefile
`-- README.md

Requirements

  • GCC or Clang
  • POSIX thread support
  • macOS, Linux, or WSL on Windows

Build

make

This creates the executable:

sss

You can also compile manually:

gcc -Wall -Wextra -std=c11 -Iinclude src/main.c src/sort.c -pthread -o sss

Run

./sss data/example-input.txt

Or use:

make run

Example Input

5 3 11 2 1 4 5 1 10 11 21 17 25 16 6

Example Output

Thread ID1: total number of swaps = 16
Thread ID2: total number of swaps = 13

Sorted Array A = (1, 1, 2, 3, 4, 5, 5, 6, 10, 11, 11, 16, 17, 21, 25)
Total number of swaps to sort array A = 29.

Notes

This project was originally developed as an Operating Systems coursework project and later cleaned, documented, and prepared for portfolio presentation.

About

C pthread-based sorting simulation using two coordinated worker threads, mutexes, condition variables, and shared swap tracking.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages