-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab-5-4.c
61 lines (47 loc) · 1.43 KB
/
lab-5-4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdlib.h>
#include <sys/mman.h>
#include <native/task.h>
#include <rtdk.h>
#include <pthread.h>
volatile long int shared_resource = 0L;
#define ITERATIONS 100000
#define TASKS 10
#define DELAY 10000L
void task_body(void *cookie) {
RT_TASK *current_task;
RT_TASK_INFO current_task_info;
current_task = rt_task_self();
//получаем информацию о таске
rt_task_inquire(current_task, ¤t_task_info);
rt_printf("Task name: %s started with priority %d\n",
current_task_info.name,
current_task_info.cprio
);
for (int i = 0; i < ITERATIONS; i++) {
__sync_fetch_and_add(&shared_resource, 1);
rt_task_sleep(DELAY);
}
// pthread_mutex_unlock(&my_gasu_mutex);
rt_printf("Task name: %s is shutting down\n", current_task_info.name);
}
int main(int argc, char **argv) {
char task_name[TASKS][16];
RT_TASK task[TASKS];
rt_print_auto_init(1);
mlockall(MCL_CURRENT|MCL_FUTURE);
for(int i = 0; i < TASKS; i++) {
snprintf(task_name[i], 16, "Lab5Task-%d", i);
if (rt_task_create(&task[i], task_name[i], 0, 50 - (i*2), T_JOINABLE) != 0) {
exit(-1);
}
}
for (int i = 0; i < TASKS; i++) {
rt_printf("Starting task %s\n", task_name[i]);
rt_task_start(&task[i], &task_body, NULL);
}
rt_printf("All tasks started\n");
for(int i = 0; i < TASKS; i++) {
rt_task_join(&task[i]);
}
rt_printf("All tasks stopped, shared_resource = %d\n", shared_resource);
}