Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c8636d2

Browse files
authored
Create print_seq
1 parent f295685 commit c8636d2

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

‎print_seq

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
output 1 - 100
3+
two threads, one prints odd one prints even in order
4+
*/
5+
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <pthread.h>
9+
10+
#define START_NUMBER 1
11+
#define END_NUMBER 100
12+
13+
pthread_mutex_t mtx;
14+
pthread_cond_t cond;
15+
16+
int seq = START_NUMBER;
17+
18+
int thread_run;
19+
20+
typedef struct range_s {
21+
int start;
22+
int end;
23+
} range_t;
24+
25+
int odd_print_one(const int start, const int end) {
26+
27+
pthread_mutex_lock(&mtx);
28+
29+
// wait for cond to be singaled by using mtx and cond
30+
while (thread_run == 1) {
31+
pthread_cond_wait(&cond, &mtx);
32+
}
33+
if (seq > end) {
34+
pthread_mutex_unlock(&mtx);
35+
return -1;
36+
}
37+
printf("%d ", seq);
38+
seq ++;
39+
thread_run = 2;
40+
// siganl cond variable;
41+
pthread_cond_signal(&cond);
42+
pthread_mutex_unlock(&mtx);
43+
44+
return 0;
45+
}
46+
47+
int even_print_one(const int start, const int end) {
48+
49+
pthread_mutex_lock(&mtx);
50+
51+
// wait for cond to be singaled by using mtx and cond
52+
while (thread_run == 1) {
53+
pthread_cond_wait(&cond, &mtx);
54+
}
55+
if (seq > end) {
56+
pthread_mutex_unlock(&mtx);
57+
return -1;
58+
}
59+
printf("%d ", seq);
60+
seq ++;
61+
thread_run = 1;
62+
// siganl cond variable
63+
pthread_cond_signal(&cond);
64+
pthread_mutex_unlock(&mtx);
65+
66+
return 0;
67+
}
68+
69+
void *odd_printer(void *args) {
70+
int rc;
71+
const int start = ((range_t *)args)->start;
72+
const int end = ((range_t *)args)->end;
73+
do {
74+
rc = odd_print_one(start, end);
75+
} while (rc == 0);
76+
77+
return NULL;
78+
}
79+
80+
void *even_printer(void *args) {
81+
int rc;
82+
const int start = ((range_t *)args)->start;
83+
const int end = ((range_t *)args)->end;
84+
do {
85+
rc = even_print_one(start, end);
86+
} while(rc == 0);
87+
88+
return NULL;
89+
}
90+
91+
int main() {
92+
pthread_t odd_thread, even_thread;
93+
int rc;
94+
void *ret;
95+
96+
const range_t range = {START_NUMBER, END_NUMBER};
97+
98+
pthread_mutex_init(&mtx, NULL);
99+
pthread_cond_init(&cond, NULL);
100+
101+
thread_run = 2;
102+
103+
rc = pthread_create(&odd_thread, NULL, odd_printer, (void *)&range);
104+
if (rc != 0) {
105+
goto cu1;
106+
}
107+
rc = pthread_create(&even_thread, NULL, even_printer, (void *)&range);
108+
if (rc != 0) {
109+
pthread_kill(odd_thread, 0);
110+
goto cu0;
111+
}
112+
113+
// clean up
114+
pthread_join(even_thread, &ret);
115+
116+
cu0:
117+
pthread_join(odd_thread, &ret);
118+
cu1:
119+
printf("\n");
120+
pthread_mutex_destroy(&mtx);
121+
pthread_cond_destroy(&cond);
122+
123+
return rc;
124+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /