Skip to content

Commit

Permalink
lab3 05-多处理器编程实验代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Iron-Buster committed Mar 13, 2024
1 parent de1fdde commit 39f6871
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 2 deletions.
20 changes: 20 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "E:\\cpp\\mingw64\\bin\\gcc.exe",
"cStandard": "c17",
"cppStandard": "gnu++14",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
Empty file added .vscode/extensions.json
Empty file.
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"files.associations": {
"dirent.h": "c",
"unordered_map": "cpp"
}
"unordered_map": "cpp",
"cstdio": "c",
"thread.h": "c"
},
"C_Cpp.errorSquiggles": "disabled"
}
19 changes: 19 additions & 0 deletions 2024/lab3/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "thread.h"

void T_a() {
while (1) {
printf("a");
}
}

void T_b() {
while (1) {
printf("b");
}
}

// pthead库不是标准linux库 -> 执行gcc thread.c -lpthread
int main() {
create(T_a);
create(T_b);
}
16 changes: 16 additions & 0 deletions 2024/lab3/memory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "thread.h"


// 多个线程之间是共享内存
int n;

void T_hello(int id) {
int i = n++;
printf("%d\n", i);
}

int main() {
for (int i = 0; i < 10; i++) {
create(T_hello);
}
}
42 changes: 42 additions & 0 deletions 2024/lab3/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "thread.h"


#define N 4

char * volatile low[N];
char * volatile high[N];


void update_range(int T, char *ptr) {
if (ptr < low[T]) {
low[T] = ptr;
}
if (ptr > high[T]) {
high[T] = ptr;
}
}


void probe(int T, int n) {
char scratch[64];
update_range(T, scratch);

printf("Stack(T%d) >= %ld KB\n", T, (high[T] - low[T]) / 1024);

probe(T, n + 1);
}

void T_probe(int T) {
T -= 1;
low[T] = (char *)-1;
high[T] = (char *)0;
probe(T, 0);
}

int main() {
setbuf(stdout, NULL);

for (int i = 0; i < N; i++) {
create(T_probe);
}
}
68 changes: 68 additions & 0 deletions 2024/lab3/thread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <pthread.h>


#define LENGTH(arr) (sizeof(arr) / sizeof(arr[0]))

enum {
T_FREE = 0, // This slot is not used yet.
T_LIVE, // This thread is running.
T_DEAD, // This thread has terminated.
};

struct thread {
int id; // Thread number: 1, 2, ...
int status; // Thread status: FREE/LIVE/DEAD
pthread_t thread; // Thread struct
void (*entry)(int); // Entry point
};

static struct thread threads_[4096];
static int n_ = 0;

static inline
void *wrapper_(void *arg) {
struct thread *t = (struct thread *)arg;
t->entry(t->id);
return NULL;
}

// Create a thread that calls function fn.
static inline
void create(void *fn) {
assert(n_ < LENGTH(threads_));

threads_[n_] = (struct thread) {
.id = n_ + 1,
.status = T_LIVE,
.entry = fn,
};
pthread_create(
&(threads_[n_].thread),
NULL,
wrapper_,
&threads_[n_]
);
n_++;
};

static inline
void join() {
for (int i = 0; i < LENGTH(threads_); i++) {
struct thread *t = &threads_[i];
if (t->status == T_LIVE) {
pthread_join(t->thread, NULL);
t->status = T_DEAD;
}
}
}

// Join all threads when main() returns;
__attribute__((destructor))
static void cleanup() {
join();
}

0 comments on commit 39f6871

Please sign in to comment.