-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_progress.c
More file actions
122 lines (107 loc) · 2.99 KB
/
new_progress.c
File metadata and controls
122 lines (107 loc) · 2.99 KB
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
115
116
117
118
119
120
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<time.h>
#include<pthread.h>
#include<unistd.h>
#include<math.h>
#define BILLION 1000000000L
#define NUM_THREAD 3
void *countA(void *p);
void *countB(void *p);
void *countC(void *p);
pthread_t thr[NUM_THREAD];
struct timespec s1, e1,s2,e2,s3,e3;
double accum;
void Thread_A(){
int s,rc;
struct sched_param prio;
int policy=SCHED_OTHER;
rc = pthread_setschedparam(thr[0], policy, &prio);
printf("A->%d\n",rc);
if( clock_gettime( CLOCK_REALTIME, &s1) == -1 ) {
fprintf(stderr, "clock gettime" );
exit( EXIT_FAILURE );
}
pthread_create(&thr[0],NULL,countA,NULL);
}
void Thread_B(){
int s,rc;
struct sched_param prio;
int policy=SCHED_FIFO;
rc = pthread_setschedparam(thr[1], policy, &prio);
printf("B->%d\n",rc);
if( clock_gettime( CLOCK_REALTIME, &s2) == -1 ) {
fprintf(stderr, "clock gettime" );
exit( EXIT_FAILURE );
}
pthread_create(&thr[1],NULL,countB,NULL);
}
void Thread_C(){
int s,rc;
struct sched_param prio;
int policy=SCHED_RR;
rc = pthread_setschedparam(thr[2], policy, &prio);
printf("C->%d\n",rc);
if( clock_gettime( CLOCK_REALTIME, &s3) == -1 ) {
fprintf(stderr, "clock gettime" );
exit( EXIT_FAILURE );
}
pthread_create(&thr[2],NULL,countC,NULL);
}
//......................................................................
int main(int argc , char* argv[]){
double t1,t2,t3;
Thread_A();
pthread_join(thr[0],NULL);
if( clock_gettime( CLOCK_REALTIME, &e1) == -1 ) {
fprintf(stderr,"clock gettime" );
exit( EXIT_FAILURE );}
Thread_B();
pthread_join(thr[1],NULL);
if( clock_gettime( CLOCK_REALTIME, &e2) == -1 ) {
fprintf(stderr,"clock gettime" );
exit( EXIT_FAILURE );}
Thread_C();
pthread_join(thr[2],NULL);
if( clock_gettime( CLOCK_REALTIME, &e3) == -1 ) {
fprintf(stderr,"clock gettime" );
exit( EXIT_FAILURE );}
t1 = ( e1.tv_sec - s1.tv_sec ) + ( e1.tv_nsec - s1.tv_nsec )/ BILLION;
t2 = ( e2.tv_sec - s2.tv_sec ) + ( e2.tv_nsec - s2.tv_nsec )/ BILLION;
t3 = ( e3.tv_sec - s3.tv_sec ) + ( e3.tv_nsec - s3.tv_nsec )/ BILLION;
printf("\n");
int i[3]={t1,t2,t3};
for(int j=0;j<3;j++){
printf("%d ",i[j]);
for(int k=i[j];k>0;k--){
printf("*");
}
printf("\n");
}
}
//......................................................................
void *countA(void *p){
long double c=0;
printf("\nExecuting A\n");
for(long double i=0;i<pow(2,32);i++){
c+=1;
}
printf("\ndone A\n");
}
void *countB(void *p){
long double c=0;
printf("\nExecuting B\n");
for(long double i=0;i<pow(2,32);i++){
c+=1;
}
printf("\ndone B\n");
}
void *countC(void *p){
long double c=0;
printf("\nExecuting C\n");
for(long double i=0;i<pow(2,32);i++){
c+=1;
}
printf("\ndone c\n");
}