Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions archive/c/c/sleep-sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>

pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER;
int *global_sorted;
int global_index = 0;
int global_total;

typedef struct {
int number;
} ThreadPayload;

void* sortNumber(void* args) {
ThreadPayload* payload = (ThreadPayload*) args;
const int number = payload->number;

sleep(number); // Sleep for number seconds

pthread_mutex_lock(&print_mutex);
global_sorted[global_index++] = number;
pthread_mutex_unlock(&print_mutex);

free(payload);
return NULL;
}

void parseInput(const char *input, int **arr, int *n) {
char *token;
char *inputCopy = strdup(input);
token = strtok(inputCopy, ",");

while (token != NULL) {
(*arr)[(*n)++] = atoi(token);
token = strtok(NULL, ",");
}

free(inputCopy);
}

int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"\n");
return 1;
}

const char *input = argv[1];
if (strlen(input) == 0 || input[0] == ' ') {
printf("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"\n");
return 1;
}

int *arr = malloc(100 * sizeof(int));
int n = 0;

parseInput(input, &arr, &n);

if (n < 2) {
printf("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"\n");
free(arr);
return 1;
}

global_sorted = malloc(n * sizeof(int));
global_total = n;

pthread_t *threads = malloc(n * sizeof(pthread_t));

for (int i = 0; i < n; i++) {
ThreadPayload *payload = malloc(sizeof(ThreadPayload));
payload->number = arr[i];
pthread_create(&threads[i], NULL, sortNumber, (void *) payload);
}

for (int i = 0; i < n; i++) {
pthread_join(threads[i], NULL);
}

for (int i = 0; i < n; i++) {
printf("%d%s", global_sorted[i], (i < n - 1) ? ", " : "");
}
printf("\n");

free(arr);
free(threads);
free(global_sorted);

return 0;
}
2 changes: 1 addition & 1 deletion archive/c/c/testinfo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ folder:
container:
image: "gcc"
tag: "8.3"
build: "gcc -o {{ source.name }} {{ source.name }}{{ source.extension }} -lm"
build: "gcc -o {{ source.name }} {{ source.name }}{{ source.extension }} -pthread -lm"
cmd: "./{{ source.name }}"
Loading