diff --git a/exercises/downloader/Makefile b/exercises/downloader/Makefile new file mode 100644 index 0000000..01acf23 --- /dev/null +++ b/exercises/downloader/Makefile @@ -0,0 +1,27 @@ +# Makefile for Multi-threaded File Downloader + +CC := gcc +CFLAGS := -Wall -Wextra -pedantic -std=c11 -pthread -g +TARGET := downloader +SRC := main.c +OBJS := $(SRC:.c=.o) + +.PHONY: all clean test valgrind + +all: $(TARGET) + +$(TARGET): $(OBJS) + $(CC) $(CFLAGS) -o $@ $^ + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +test: $(TARGET) + ./downloader test1.txt test2.txt test3.txt + +valgrind: $(TARGET) + valgrind --leak-check=full ./downloader test1.txt test2.txt + +clean: + rm -f $(TARGET) $(OBJS) + diff --git a/exercises/downloader/README.md b/exercises/downloader/README.md new file mode 100644 index 0000000..4ffae9c --- /dev/null +++ b/exercises/downloader/README.md @@ -0,0 +1,89 @@ +# ๐Ÿงต Multi-threaded File Downloader (Simulated) + +This project simulates a multi-threaded file downloader in C using POSIX threads (`pthreads`). The goal is to practice creating and managing threads, synchronizing access to shared resources, and simulating parallel file downloads. + +--- + +## ๐Ÿ“‹ Description + +Each file provided via command-line arguments is "downloaded" by a separate thread. The download is simulated by printing log messages and sleeping for a random duration. All threads run concurrently and the main thread waits for them to complete. + +This project is inspired by Chapter 7 ("Threads") of *Linux System Programming* by Robert Love. + +--- + +## ๐Ÿš€ Usage + +```bash +$ ./downloader file1.txt file2.txt file3.txt +```` + +Each file will be "downloaded" in parallel by a separate thread. The program prints informative messages about the start and completion of each download. + +--- + +## ๐Ÿ› ๏ธ Features + +* One thread per file +* Simulated download using `sleep()` +* Optional logging to file (future enhancement) +* Thread-safe output via `pthread_mutex_t` +* Clean thread termination via `pthread_join` + +--- + +## ๐Ÿงช Example Output + +``` +[Thread 0x7fdc9a10]: Starting download of file1.txt +[Thread 0x7fdc9210]: Starting download of file2.txt +[Thread 0x7fdc9210]: Finished download of file2.txt +[Thread 0x7fdc9a10]: Finished download of file1.txt +All downloads complete. +``` + +--- + +## ๐Ÿ“‚ Project Structure + +``` +multidownload/ +โ”œโ”€โ”€ main.c # Main program +โ”œโ”€โ”€ Makefile # Build script +โ””โ”€โ”€ README.md # Project documentation +``` + +--- + +## ๐Ÿงต Concepts Practiced + +* Thread creation with `pthread_create` +* Thread joining with `pthread_join` +* Synchronization with `pthread_mutex_t` +* Struct-based thread argument passing +* Output race condition prevention + +--- + +## ๐Ÿ—๏ธ Build + +```bash +$ make +``` + +To clean the build: + +```bash +$ make clean +``` + +--- + +## ๐Ÿ“– References + +* *Linux System Programming* by Robert Love โ€” Chapter 7: Threads +* POSIX Threads (man 7 pthread) + +--- + +*Last updated: 2025-07-28* diff --git a/exercises/downloader/main.c b/exercises/downloader/main.c new file mode 100644 index 0000000..64111b4 --- /dev/null +++ b/exercises/downloader/main.c @@ -0,0 +1,7 @@ +#include +#include + +int main(int argc, char *argv[]) +{ + return EXIT_SUCCESS; +}