Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <time.h>
- #include <pthread.h>
- #define Q_SIZE 1024
- #define E_HEAP 11
- #define ec_thread_creat(s) \
- if((s) != 0){printf("Couldn't create the thread!\n"); exit(err);}
- #define ec_zero(s,m) \
- if((s) != 0){perror(m); exit(s);}
- #define print_info \
- {printf("*** Type q, then press ret to exit!\n"); \
- sleep(3);}
- struct node {
- int info;
- struct node *next;
- };
- static struct node *head = NULL;
- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
- void* con(void* args);
- void* prod(void* args);
- static void cleanup_handler_con(void* arg);
- static void cleanup_handler_prod(void* arg);
- int main() {
- pthread_t tid_con, tid_prod;
- int err, flag;
- char ctrl;
- print_info
- ec_thread_creat(err = pthread_create(&tid_prod, NULL, &prod, NULL));
- printf("*** Producer created!\n");
- ec_thread_creat(err = pthread_create(&tid_con, NULL, &con, NULL));
- printf("*** Consumer created!\n");
- while(flag){
- scanf("%c",&ctrl);
- if(ctrl == 'q') flag = 0;
- else print_info
- }
- ec_zero((pthread_cancel(tid_prod)),"*** Couldn't delete the producer!\n");
- ec_zero((pthread_cancel(tid_con)),"*** Couldn't delete the consumer!\n");
- return 0;
- }
- void* prod(void* args) {
- struct node *p, *i;
- unsigned int seed;
- pthread_cleanup_push(cleanup_handler_prod,NULL);
- while(1){
- printf("--- I'm the producer\n");
- if((p = malloc(sizeof(struct node)))==NULL) pthread_exit((void*) E_HEAP);
- seed = time(0);
- p->info = rand_r(&seed);
- p->next = NULL;
- pthread_mutex_lock(&mutex);
- printf("--- Producer locked\n");
- if(head != NULL){
- i = head;
- while(i->next != NULL) i = i->next;
- i->next = p;
- } else head = p;
- sleep(2);
- printf("--- Wake up consumer!\n");
- pthread_cond_signal(&cond);
- printf("--- Unlocking the producer\n");
- pthread_mutex_unlock(&mutex);
- }
- pthread_cleanup_pop(0);
- return (void*) 0;
- }
- void* con(void* args) {
- struct node * v;
- pthread_cleanup_push(cleanup_handler_con,NULL);
- while(1){
- printf("--- I'm the consumer\n");
- pthread_mutex_lock(&mutex);
- printf("--- Consumer locked\n");
- while(head == NULL){
- printf("--- The consumer's sleeping ...ZzZzZ\n");
- pthread_cond_wait(&cond,&mutex);
- }
- v = head->next;
- printf("Number %d is leaving the queue...\n",head->info);
- free(head);
- head = v;
- printf("--- Unlocking the consumer\n");
- pthread_mutex_unlock(&mutex);
- }
- pthread_cleanup_pop(0);
- return (void*) 0;
- }
- static void cleanup_handler_prod(void* arg) {
- printf("*** Deleting producer...\n");
- pthread_mutex_unlock(&mutex);
- while (head != NULL) sleep(1);
- }
- static void cleanup_handler_con(void* arg) {
- struct node *it;
- printf("*** Deleting consumer...\n");
- if (head != NULL) {
- it = head->next;
- while (it != NULL) {
- free(head);
- head = it;
- it = it->next;
- }
- free(head);
- head = NULL;
- }
- pthread_mutex_unlock(&mutex);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement