Showing posts with label Demonstration. Show all posts
Showing posts with label Demonstration. Show all posts

C Program to Demonstrates use of reader/writer lock

How to write a C Program to Demonstrates use of reader/writer lock in C Programming Language ?


Solution:

  1. /* Demonstrates use of reader/writer lock*/
  2.  
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5.  
  6. struct job {
  7.         struct job *j_next;
  8.         struct job *j_prev;
  9.         pthread_t   j_id;   /* tells which thread handles this job */
  10.         /* ... more stuff here ... */
  11. };
  12.  
  13. struct queue {
  14.         struct job      *q_head;
  15.         struct job      *q_tail;
  16.         pthread_rwlock_t q_lock;
  17. };
  18.  
  19. /*
  20.  * Initialize a queue.
  21.  */
  22. int queue_init(struct queue *qp)
  23. {
  24.         int err;
  25.  
  26.         qp->q_head = NULL;
  27.         qp->q_tail = NULL;
  28.         err = pthread_rwlock_init(&qp->q_lock, NULL);
  29.         if (err != 0)
  30.                 return(err);
  31.  
  32.         /* ... continue initialization ... */
  33.  
  34.         return(0);
  35. }
  36.  
  37. /*
  38.  * Insert a job at the head of the queue.
  39.  */
  40. void job_insert(struct queue *qp, struct job *jp)
  41. {
  42.         pthread_rwlock_wrlock(&qp->q_lock);
  43.         jp->j_next = qp->q_head;
  44.         jp->j_prev = NULL;
  45.         if (qp->q_head != NULL)
  46.                 qp->q_head->j_prev = jp;
  47.         else
  48.                 qp->q_tail = jp;        /* list was empty */
  49.         qp->q_head = jp;
  50.         pthread_rwlock_unlock(&qp->q_lock);
  51. }
  52.  
  53. /*
  54.  * Append a job on the tail of the queue.
  55.  */
  56. void job_append(struct queue *qp, struct job *jp)
  57. {
  58.         pthread_rwlock_wrlock(&qp->q_lock);
  59.         jp->j_next = NULL;
  60.         jp->j_prev = qp->q_tail;
  61.         if (qp->q_tail != NULL)
  62.                 qp->q_tail->j_next = jp;
  63.         else
  64.                 qp->q_head = jp;        /* list was empty */
  65.         qp->q_tail = jp;
  66.         pthread_rwlock_unlock(&qp->q_lock);
  67. }
  68.  
  69. /*
  70.  * Remove the given job from a queue.
  71.  */
  72. void job_remove(struct queue *qp, struct job *jp)
  73. {
  74.         pthread_rwlock_wrlock(&qp->q_lock);
  75.         if (jp == qp->q_head) {
  76.                 qp->q_head = jp->j_next;
  77.                 if (qp->q_tail == jp)
  78.                         qp->q_tail = NULL;
  79.         } else if (jp == qp->q_tail) {
  80.                 qp->q_tail = jp->j_prev;
  81.                 if (qp->q_head == jp)
  82.                         qp->q_head = NULL;
  83.         } else {
  84.                 jp->j_prev->j_next = jp->j_next;
  85.                 jp->j_next->j_prev = jp->j_prev;
  86.         }
  87.         pthread_rwlock_unlock(&qp->q_lock);
  88. }
  89.  
  90. /*
  91.  * Find a job for the given thread ID.
  92.  */
  93. struct job *job_find(struct queue *qp, pthread_t id)
  94. {
  95.         struct job *jp;
  96.  
  97.         if (pthread_rwlock_rdlock(&qp->q_lock) != 0)
  98.                 return(NULL);
  99.  
  100.         for (jp = qp->q_head; jp != NULL; jp = jp->j_next)
  101.                 if (pthread_equal(jp->j_id, id))
  102.                         break;
  103.  
  104.         pthread_rwlock_unlock(&qp->q_lock);
  105.         return(jp);
  106. }

C Program to Demonstrates a linked list for numbers.

How to write a C Program to Demonstrates a linked list for numbers in C Programming Language ?

This Program Demonstrates a linked list for numbers.

Solution:

  1. /*Demonstrates a linked list for numbers.*/
  2.  
  3.  
  4.  
  5. #include "list-0.h"    // contain struct for the linked list
  6. #include <stdio.h>
  7. #include <cs50.h>    // special library for using getint to get numper from user insead of scanf
  8. #include <stdlib.h>    // for malloc and free functions
  9. #include <unistd.h>    // for sleep function
  10.  
  11. //node to determine the beginning of the list.
  12. node* first = NULL;  
  13.  
  14. // prototypes
  15. void delete(void);
  16. void insert(void);
  17. void search(void);
  18. void traverse(void);
  19.  
  20.  
  21. int main(void)
  22. {
  23.     int c;  //the numper of menue
  24.     do
  25.     {
  26.         printf("\nmenu\n"
  27.                "1-delete\n"
  28.                "2-insert\n"
  29.                "3-search\n"
  30.                "4-traverse\n"
  31.                "0-quit\n");
  32.                
  33.        // get the command
  34.        printf("command: ");
  35.        c = GetInt();
  36.        
  37.        // execute the command
  38.        switch(c)
  39.        {
  40.            case 1: delete(); break;
  41.            case 2: insert(); break;
  42.            case 3: search(); break;
  43.            case 4: traverse(); break;
  44.        }
  45.        
  46.        
  47.    }
  48.    while( c != 0);
  49.    
  50.    // clear the hole list before exiting
  51.    node* ptr = first;
  52.    while ( ptr != NULL)
  53.    {
  54.        node* predptr = ptr;
  55.        ptr = ptr->next;
  56.        free(predptr);
  57.    }
  58.    return 0;
  59. }
  60.  
  61. // searching a numper in the list
  62. void search(void)
  63. {
  64.     printf("the numper please: ");
  65.     int n = GetInt();
  66.    
  67.     // insitate a node for the search at the beginnig
  68.     node* ptr = first;
  69.    
  70.     //continue searching until reach the end of  the list
  71.     while ( ptr != NULL)
  72.     {
  73.        
  74.         if( ptr->== n)
  75.         {
  76.             printf("founded\n");
  77.             break;
  78.         }
  79.        
  80.         else
  81.             // update the pounter
  82.             ptr = ptr-> next;    
  83.     }
  84.    
  85.     //if reached to the end of list
  86.     if (ptr == NULL)
  87.         printf("not founded\n");
  88. }
  89.  
  90. void delete(void)
  91. {
  92.     printf("the numper please: ");
  93.     int n = GetInt();
  94.    
  95.     // insitate a pointer at the beginning of the list
  96.     node* ptr = first;
  97.    
  98.     //insitate to rememper the previous node in the list before deleting
  99.     node* predptr = NULL;
  100.     while( ptr != NULL)
  101.     {
  102.         if( ptr-> n == n)
  103.         {
  104.            
  105.             // deleting the first node
  106.             if( ptr == first)
  107.             {
  108.                 first = ptr-> next;
  109.                 free(ptr);
  110.             }
  111.            
  112.             //deleting from the middle or tail
  113.             else
  114.             {
  115.               predptr->next = ptr->next;
  116.               free(ptr);
  117.              
  118.             }
  119.            
  120.             break;
  121.         }
  122.        
  123.         // update the pointers to the next of each
  124.         else
  125.         {
  126.             predptr = ptr;          
  127.             ptr = ptr-> next;
  128.         }
  129.     }
  130.    
  131.     // make the traverse process to print the list out
  132.     traverse();
  133. }
  134.  
  135. void traverse(void)
  136. {
  137.     node* ptr = first;
  138.    
  139.     //print all the list numbers
  140.     printf("the list is: ");
  141.     while ( ptr!= NULL)
  142.     {
  143.         printf("%i ", ptr->n);
  144.         ptr = ptr->next;
  145.     }
  146.    
  147.    
  148.     // flush standard output since we haven't outputted any newlines yet
  149.     fflush(stdout);
  150.  
  151.     // pause before continuing
  152.     sleep(1);
  153.     printf("\n\n");
  154. }
  155.  
  156. // the hardest process :)..
  157. void insert(void)
  158. {
  159.     // allocate memory for new insert strcuct
  160.     node* insert = malloc(sizeof(node));
  161.     if(insert == NULL)
  162.     {
  163.         printf("cann't allocated..!!\n");
  164.         return;
  165.     }
  166.    
  167.     // fill the node
  168.     printf("\nthe numper please: ");
  169.     insert->= GetInt();
  170.     insert->next = NULL;
  171.    
  172.     // insert at the beginning if the list empty
  173.     if( first == NULL)
  174.         first = insert;
  175.        
  176.     // if it is lower than the first numper
  177.     else if ( insert->< first->)
  178.     {
  179.         insert->next = first;
  180.         first = insert;
  181.     }
  182.    
  183.     else
  184.     {
  185.         // to terverse the list for comparing the numpers
  186.         node* ptr = first;
  187.        
  188.         while (true)
  189.         {
  190.             // insert in in the last if it is the beggist numper
  191.             // must be before the down comparing or will generate "segemntaion fault"
  192.             if ( ptr->next == NULL)
  193.             {
  194.                 ptr->next = insert;
  195.                 break;
  196.             }
  197.            
  198.             // attention please : ptr->next->n
  199.             //at 'c' compare the next numper at 'e' to the inserting
  200.             else if( insert-> n < ptr->next->)
  201.             {
  202.                 insert-> next = ptr->next;
  203.                 ptr->next = insert;
  204.                 break;
  205.             }        
  206.            
  207.             // always update the ptr to reverse the list until we insert the item
  208.             ptr = ptr->next;  
  209.         }
  210.     }
  211.     // make the traverse process to print the list out
  212.     traverse();  
  213.    
  214. }