Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdlib.h>
  3. #include "list.c"
  4. typedef int my_type;
  5. typedef char my_type2;
  6.  
  7. struct args{
  8. struct hash_table *arr;
  9. int *liczby;
  10. };
  11.  
  12. int hash(void* element){
  13. my_type* tmp;
  14. tmp = (my_type *)element;
  15. return (*tmp) % 19;
  16. }
  17.  
  18. void getF(void* element){
  19. my_type* tmp;
  20. tmp = (my_type*) element;
  21. printf("%d ", *(tmp));
  22. }
  23.  
  24. int hash_ch(void* element){
  25. my_type2* tmp;
  26. tmp = (my_type2 *)element;
  27. return (*tmp) % 19;
  28. }
  29.  
  30. void getF_ch(void* element){
  31. my_type2* tmp;
  32. tmp = (my_type2*) element;
  33. printf("%d ", *(tmp));
  34. }
  35.  
  36. void* f_watku(void *arg){
  37.  
  38. struct args* a = (struct args *)arg;
  39. int *liczby = a->liczby;
  40. int i;
  41. for(i=0; i<10; i++){
  42. liczby[i]= i;
  43. add(a->arr, &liczby[i]);
  44. }
  45. }
  46.  
  47. void* f_watku2(void *arg){
  48. struct args* a = (struct args *)arg;
  49. int *liczby = a->liczby;
  50. int i;
  51. for(i=1; i<10; i++){
  52. delete(a->arr, &liczby[i]);
  53. }
  54. }
  55.  
  56. void* f_watku3(void *arg){
  57. struct args* a = (struct args *)arg;
  58. int *liczby = a->liczby;
  59. int i;
  60. for(i=1; i<10; i++){
  61. delete(a->arr, &liczby[i]);
  62. }
  63. }
  64.  
  65. int main(){
  66. struct hash_table* arr = create_table(hash, getF);
  67.  
  68. pthread_t w1;
  69. pthread_t w2;
  70. pthread_t w3;
  71. pthread_t w4, w5, w6;
  72.  
  73. struct args a1, a2, a3, a4;
  74. a1.arr = a2.arr = a3.arr = a4.arr = arr;
  75. a1.liczby = (int *)malloc(sizeof(int) * 10);
  76. a2.liczby = (int *)malloc(sizeof(int) * 10);
  77. a3.liczby = (int *)malloc(sizeof(int) * 10);
  78. pthread_create(&w1, NULL, f_watku, &a1);
  79. pthread_create(&w2, NULL, f_watku, &a2);
  80. pthread_create(&w5, NULL, f_watku, &a3);
  81. pthread_join(w1, NULL);
  82. pthread_join(w2, NULL);
  83. pthread_join(w5, NULL);
  84. print_table(arr);
  85. //pthread_create(&w3, NULL, f_watku2, &a1);
  86. pthread_create(&w4, NULL, f_watku2, &a2);
  87. pthread_create(&w6, NULL, f_watku2, &a3);
  88.  
  89. //pthread_join(w3, NULL);
  90. pthread_join(w4, NULL);
  91. pthread_join(w6, NULL);
  92. print_table(arr);
  93. /*
  94. int a = 20;
  95. int b = 19;
  96. int c = 38;
  97.  
  98. int *a = (int *)malloc(sizeof(int));
  99. *a = 5;
  100. int *b = (int *)malloc(sizeof(int));
  101. *b = 25;
  102. int *c = (int *)malloc(sizeof(int));
  103. *c = 15;
  104. int *d = (int *)malloc(sizeof(int));
  105. *d = 35;
  106. add(arr, a);
  107. add(arr, b);
  108. add(arr, c);
  109. add(arr, d);
  110. print_table(arr);
  111. delete(arr, b);
  112. print_table(arr);
  113. delete(arr, c);
  114. print_table(arr);
  115. delete(arr, d);
  116. print_table(arr);
  117. delete(arr, a);
  118. print_table(arr);*/
  119. return 0;
  120. }
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128. #include <stdio.h>
  129. #include <malloc.h>
  130. #include <pthread.h>
  131.  
  132. struct list_elem {
  133. void* number;
  134. struct list_elem *next;
  135. };
  136.  
  137. struct arr_elem {
  138. struct arr_elem *next;
  139. struct list_elem *cont;
  140. int key;
  141. };
  142.  
  143. struct hash_table {
  144. pthread_mutex_t p_m;
  145. struct arr_elem *first;
  146. int(*hash)(void*);
  147. void(*getF)(void*);
  148. };
  149.  
  150. void add(struct hash_table* arr, void* element)
  151. {
  152. pthread_mutex_lock(&arr->p_m);
  153. struct arr_elem *tmp = arr->first;
  154. while (tmp->next != NULL && tmp->next->key != arr->hash(element)) {
  155. tmp = tmp->next;
  156. }
  157. if (tmp->next == NULL) {
  158. tmp->next = (struct arr_elem *)malloc(sizeof(struct arr_elem));
  159. tmp->next->key = arr->hash(element);
  160. tmp->next->next = NULL;
  161. tmp->next->cont = (struct list_elem *)malloc(sizeof(struct list_elem));
  162. tmp->next->cont->number = element;
  163. tmp->next->cont->next = NULL;
  164. }
  165. else {
  166. struct list_elem *tmpl = tmp->next->cont;
  167. while (tmpl->next != NULL && tmpl->next->number != element) {
  168. tmpl = tmpl->next;
  169. }
  170. if (tmpl->next == NULL) {
  171. tmpl->next = (struct list_elem *)malloc(sizeof(struct list_elem));
  172. tmpl->next->number = element;
  173. tmpl->next->next = NULL;
  174. }
  175. }
  176. pthread_mutex_unlock(&arr->p_m);
  177. }
  178.  
  179. void delete(struct hash_table* arr, void* element){
  180. pthread_mutex_lock(&arr->p_m);
  181.  
  182. struct arr_elem *tmp = arr->first;
  183. while (tmp != NULL && tmp->key != arr->hash(element)) {
  184. tmp = tmp->next;
  185. }
  186. if(tmp == NULL){
  187. pthread_mutex_unlock(&arr->p_m);
  188. return;
  189. }
  190. else{
  191. struct list_elem *tmpl = tmp->cont, *tmpl2;
  192. if(tmpl->next == NULL){
  193. free(tmpl);
  194. tmp->cont = NULL;
  195. //tmp23 = tmp->next;
  196. //free(tmp->next);
  197. //tmp->next = tmp23;
  198.  
  199. pthread_mutex_unlock(&arr->p_m);
  200. return;
  201. }
  202. while (tmpl->next != NULL && tmpl->next->number != element) {
  203.  
  204. tmpl = tmpl->next;
  205. }
  206. if(tmpl->next != NULL){
  207. tmpl2 = tmpl->next->next;
  208. free(tmpl->next);
  209. tmpl->next = tmpl2;
  210. }
  211. }
  212. pthread_mutex_unlock(&arr->p_m);
  213. }
  214.  
  215. void clear_table(struct hash_table* arr) {
  216. struct arr_elem *tmp = arr->first->next;
  217. struct arr_elem *tmp2;
  218. struct list_elem *contTmp, *contTmp2;
  219. while (tmp != NULL) {
  220. tmp2 = tmp->next;
  221. contTmp = tmp->cont;
  222. while (contTmp != NULL) {
  223. contTmp2 = contTmp->next;
  224. free(contTmp);
  225. contTmp = contTmp2;
  226. }
  227. free(tmp);
  228. tmp = tmp2;
  229. }
  230. free(arr->first);
  231. }
  232.  
  233. void get(struct hash_table* arr, int key) {
  234. struct arr_elem *tmp = arr->first;
  235. while (tmp != NULL && tmp->key != key) {
  236. tmp = tmp->next;
  237. }
  238. if (tmp == NULL) {
  239. printf("no data with key %d\n", key);
  240. }
  241. else {
  242. struct list_elem *tmpl = tmp->cont;
  243. printf("key %d :", key);
  244. while (tmpl != NULL) {
  245. arr->getF(tmpl->number);
  246. tmpl = tmpl->next;
  247. }
  248. printf("\n");
  249. }
  250. }
  251.  
  252. void print_table(struct hash_table* arr) {
  253. struct arr_elem *tmp = arr->first->next;
  254. while (tmp != NULL) {
  255. printf("key: %d, values: ", tmp->key);
  256. struct list_elem *tmpl = tmp->cont;
  257. while (tmpl != NULL) {
  258. arr->getF(tmpl->number);
  259. tmpl = tmpl->next;
  260. }
  261. printf("\n");
  262. tmp = tmp->next;
  263. }
  264. }
  265.  
  266. struct hash_table* create_table(int(*hashF)(void*),void(*getF)(void*)) {
  267. struct hash_table* arr = (struct hash_table*)malloc(sizeof(struct hash_table));
  268. //arr->p_m = PTHREAD_MUTEX_INITIALIZER;
  269. pthread_mutex_init(&arr->p_m, NULL);
  270. arr->hash = hashF;
  271. arr->getF = getF;
  272. arr->first = (struct arr_elem *)malloc(sizeof(struct arr_elem));
  273. arr->first->key = -1;
  274. arr->first->next = NULL;
  275. arr->first->cont = NULL;
  276. return arr;
  277. }
  278.  
  279. gcc -c -Wall -fPIC -D_GNU_SOURCE list.c
  280. gcc list.o -shared -o lib_list.so
  281. gcc -c main.c -o list_main.o
  282. gcc list_main.o -L. -pthread -l_list -o test_listdyn.out
  283. export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:."
  284. ./test_listdyn.out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement