-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
114 lines (84 loc) · 1.61 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include"utils.h"
#define QSIZE (20)
struct queue
{
int a[QSIZE];
int size;
int front;
int rear;
} que={0};
void queue_push(int x)
{
if(que.size<QSIZE)
{
int rear=(que.rear+1)%QSIZE;
que.a[que.rear]=x;
que.size++;
que.rear=rear;
}
}
int queue_pop()
{
int r=-1;
if(que.size>=0)
{
r=que.a[que.front];
int front=(que.front+1)%QSIZE;
que.front=front;
que.size--;
}
return r;
}
#define STK_LEN 2048*4
xlen_t Task1_STK[STK_LEN];
xlen_t Task2_STK[STK_LEN];
xlen_t Task3_STK[STK_LEN];
xlen_t Task4_STK[STK_LEN];
xlen_t Task5_STK[STK_LEN];
tid_t t1,t2,t3,t4,t5;
sem full={0};
sem empty={10};
sem mutex={1};
int index=0;
void producer()
{
int cnt=0;
for(;;)
{
sem_wait(&empty);
sem_wait(&mutex);
queue_push(cnt);
printf("produce %d...\n",cnt);
cnt++;
sem_signal(&mutex);
sem_signal(&full);
task_sleep(100000);
}
}
void customer()
{
for(;;)
{
sem_wait(&full);
sem_wait(&mutex);
printf("custom %d...\n",queue_pop());
sem_signal(&mutex);
sem_signal(&empty);
task_sleep(1000000);
}
}
#include<encoding.h>
int main()
{
t1=task_creat(producer,(task_attr_t){&Task1_STK[STK_LEN-1],1, .task_name="task1"});
t2=task_creat(customer,(task_attr_t){&Task2_STK[STK_LEN-1],2, .task_name="task2"});
//t3=task_creat(task,(task_attr_t){&Task3_STK[STK_LEN-1],3, .task_name="task3"});
//t4=task_creat(task,(task_attr_t){&Task4_STK[STK_LEN-1],4, .task_name="task4"});
//t5=task_creat(timer,(task_attr_t){&Task5_STK[STK_LEN-1],5,.task_name="timer"});
//enable_global_int();
//enable_time_int();
//task_sleep(500000);
printf("\n%ld\n",rdtime());
exit(0);
for(;;){}
}