-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9058875
commit 09a1fe1
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
eBPF_Supermarket/CPU_Subsystem/eBPF_proc_image/test/test_mfutex.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include <stdio.h> | ||
#include <pthread.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <string.h> | ||
|
||
// 定义一个互斥锁 | ||
pthread_mutex_t mutex; | ||
|
||
// 线程的执行函数 | ||
void *thread_func(void *arg) { | ||
int thread_id = *(int *)arg; | ||
|
||
printf("Thread %d: Trying to acquire the mutex\n", thread_id); | ||
|
||
// 尝试获取互斥锁 | ||
pthread_mutex_lock(&mutex); | ||
printf("Thread %d: Mutex acquired\n", thread_id); | ||
|
||
// 模拟线程持有互斥锁的操作 | ||
sleep(3); | ||
|
||
// 释放互斥锁 | ||
printf("Thread %d: Releasing the mutex\n", thread_id); | ||
pthread_mutex_unlock(&mutex); | ||
|
||
return NULL; | ||
} | ||
pid_t get_tgid() { | ||
pid_t pid = getpid(); | ||
char path[256]; | ||
snprintf(path, sizeof(path), "/proc/%d/status", pid); | ||
|
||
FILE *fp = fopen(path, "r"); | ||
if (!fp) { | ||
perror("打开 proc/pid/status 文件失败"); | ||
return -1; | ||
} | ||
|
||
char line[256]; | ||
int tgid = -1,cpuid = -1; | ||
|
||
while (fgets(line, sizeof(line), fp)) { | ||
if (strncmp(line, "Tgid:", 5) == 0) { | ||
sscanf(line, "Tgid: %d", &tgid); | ||
break; | ||
} | ||
} | ||
|
||
fclose(fp); | ||
return tgid; | ||
} | ||
int main() { | ||
pthread_t threads[10]; | ||
int thread_ids[10]; | ||
int i; | ||
pid_t tgid = get_tgid(); | ||
printf("TGID:%d\n",tgid); | ||
int tmp; | ||
scanf("%d",&tmp); | ||
// 初始化互斥锁 | ||
if (pthread_mutex_init(&mutex, NULL) != 0) { | ||
fprintf(stderr, "Failed to initialize mutex\n"); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
// 创建5个线程 | ||
for (i = 0; i < 10; i++) { | ||
thread_ids[i] = i + 1; | ||
if (pthread_create(&threads[i], NULL, thread_func, &thread_ids[i]) != 0) { | ||
fprintf(stderr, "Failed to create thread %d\n", i + 1); | ||
return EXIT_FAILURE; | ||
} | ||
} | ||
|
||
// 等待所有线程结束 | ||
for (i = 0; i < 10; i++) { | ||
if (pthread_join(threads[i], NULL) != 0) { | ||
fprintf(stderr, "Failed to join thread %d\n", i + 1); | ||
return EXIT_FAILURE; | ||
} | ||
} | ||
|
||
// 销毁互斥锁 | ||
pthread_mutex_destroy(&mutex); | ||
|
||
return EXIT_SUCCESS; | ||
} |