From 13b4869d99511c99c2ea287ba2ec53fdd3e3d4a1 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Tue, 29 Jul 2025 16:08:16 +0200 Subject: [PATCH 1/3] Multi threading downloader simulate [WIP] --- exercises/downloader/Makefile | 27 +++++++++++ exercises/downloader/README.md | 89 ++++++++++++++++++++++++++++++++++ exercises/downloader/main.c | 7 +++ 3 files changed, 123 insertions(+) create mode 100644 exercises/downloader/Makefile create mode 100644 exercises/downloader/README.md create mode 100644 exercises/downloader/main.c 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; +} From 1884b01d15500cdc06225fa58f20579752963800 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Tue, 29 Jul 2025 23:24:16 +0200 Subject: [PATCH 2/3] Implemented fake downloader without log support --- exercises/downloader/main.c | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/exercises/downloader/main.c b/exercises/downloader/main.c index 64111b4..00cfce9 100644 --- a/exercises/downloader/main.c +++ b/exercises/downloader/main.c @@ -1,7 +1,41 @@ +#define _GNU_SOURCE +#include +#include #include #include +#include +#include +#include + +static void *download(void *); int main(int argc, char *argv[]) { - return EXIT_SUCCESS; + if (argc < 2) { + fprintf(stderr, "Usage: %s file1 file2...", argv[0]); + return 1; + } + + const int nfile = argc - 1; + pthread_t workers[nfile]; + for (int i = 0; i < nfile; i++) { + pthread_create(&workers[i], NULL, download, argv[i + 1]); + } + + for (int i = 0; i < nfile; i++) { + pthread_join(workers[i], NULL); + } + printf("All downloads complete.\n"); + + return 0; +} + +static void *download(void *arg) +{ + char *uri = (char *)arg; + int tid = syscall(SYS_gettid); + printf("[Thread %x] Starting download of %s\n", tid, uri); + sleep(rand() % 3 + 1); + printf("[Thread %x] Finished download of %s\n", tid, uri); + pthread_exit(0); } From 91bc8863cb1ab1f7a55111d9e55814be5728a479 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Wed, 30 Jul 2025 15:49:50 +0200 Subject: [PATCH 3/3] Update exercises README.md --- exercises/README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/exercises/README.md b/exercises/README.md index 62150fd..cdfc54a 100644 --- a/exercises/README.md +++ b/exercises/README.md @@ -47,6 +47,20 @@ This is an organized list of all exercises completed so far, including a brief d --- +## ๐Ÿ“ฅ downloader + +**Description**: A multi-threaded simulated file downloader implemented in C using POSIX threads (pthreads). + +- ๐Ÿ“„ [README](downloader/README.md) +- ๐Ÿ“‚ Directory: `downloader/` +- โœ… Features: + - Each thread simulates downloading a file by printing periodic progress updates + - Synchronization using `pthread` APIs + - Demonstrates creation, execution, and joining of threads + - Makefile included with standard targets + +--- + ## ๐Ÿ”ง Tooling & Automation **Shared tools and scripts used across projects**: @@ -56,4 +70,4 @@ This is an organized list of all exercises completed so far, including a brief d - Shell script for automated tests: `run_tests.sh` - Valgrind memory check reports generated automatically -*Last updated: 2025-07-09* +*Last updated: 2025-07-30*