Facebook
From Ozma, 4 Years ago, written in Plain Text.
This paste is a reply to Re: suck mydick from Ozma - view diff
Embed
Download Paste or View Raw
Hits: 242
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <pthread.h>
  5. #include <semaphore.h>
  6. #define n 10
  7. #define threadNumber 3
  8.  
  9. typedef struct pcb
  10. {
  11.  
  12.   int id;
  13.  
  14.   int priority;
  15.   struct pcb *next;
  16.  
  17.   struct pcb *prev;
  18.  
  19. } pcb_t;
  20.  
  21. typedef struct queue
  22. {
  23.  
  24.   pcb_t *front;
  25.  
  26.   pcb_t *rear;
  27.  
  28.   pcb_t *pcb_table[n];
  29.  
  30.   int state;
  31.  
  32. } que_t;
  33.  
  34.  
  35. que_t
  36. init_queue (que_t * qq)
  37. {
  38.  
  39.   qq->front = NULL;
  40.  
  41.   qq->rear = NULL;
  42.  
  43.   return *qq;
  44.  
  45. };
  46.  
  47.  
  48.  
  49. pcb_t
  50. init_pcb (pcb_t * pp)
  51. {
  52.  
  53.   pp->id = rand () % 10 + 1;
  54.  
  55.   pp->priority = rand () % 5 + 1;
  56.  
  57.   return *pp;
  58.  
  59. };
  60.  
  61.  
  62.  
  63. void
  64. print_list (pcb_t * head)
  65. {
  66.  
  67.   pcb_t *current = head;
  68.  
  69.   while (current != NULL)
  70.  
  71.     {
  72.  
  73.       printf ("%d\n", current->id);
  74.  
  75.       current = current->next;
  76.  
  77.     }
  78.  
  79. };
  80.  
  81.  
  82. void
  83. enqueue (pcb_t * pp, que_t * qq)
  84. {
  85.  
  86.   if (qq->front == NULL && qq->rear == NULL)
  87.  
  88.     {
  89.  
  90.       qq->rear = pp;
  91.  
  92.       qq->front = pp;
  93.  
  94.       pp->next = NULL;
  95.  
  96.     }
  97.  
  98.   else
  99.  
  100.     {
  101.  
  102.       qq->rear->next = pp;
  103.  
  104.       qq->rear = pp;
  105.  
  106.       pp->next = NULL;
  107.  
  108.     }
  109.  
  110.  
  111. };
  112.  
  113.  
  114.  
  115. void
  116. dequeue (que_t * qq)
  117. {
  118.  
  119.   if (qq->front == NULL && qq->rear == NULL)
  120.  
  121.     {
  122.  
  123.       printf ("Queue is Empty\n");
  124.  
  125.     }
  126.  
  127.   else
  128.  
  129.     {
  130.  
  131.       pcb_t *temp = qq->front;
  132.  
  133.       qq->front = qq->front->next;
  134.  
  135.       printf ("Removed item is: %d\n", temp->id);
  136.  
  137.     }
  138.  
  139. }
  140.  
  141. int
  142. isFull (que_t * qq)
  143. {
  144.  
  145.   int full = 0;
  146.  
  147.   if (qq->rear->priority == n - 1)
  148.  
  149.     {
  150.  
  151.       full = 1;
  152.  
  153.     }
  154.  
  155.   return full;
  156.  
  157. }
  158.  
  159.  
  160. int
  161. isEmpty (que_t * qq)
  162. {
  163.  
  164.   int empty = 0;
  165.  
  166.   if (qq->front == qq->rear + 1)
  167.  
  168.     {
  169.  
  170.       empty = 1;
  171.  
  172.     }
  173.  
  174.   return empty;
  175.  
  176. }
  177.  
  178.  
  179. void insert(pcb_t *pp, que_t *qq) {
  180.         que_t check;
  181.         check.state = qq->state;
  182.         check.front = qq->front;
  183.         check.rear = qq->front;
  184.  
  185.         if (qq->front == NULL && qq->rear == NULL) {
  186.                 enqueue(pp, qq);
  187.         }
  188.         else if (qq->rear->priority < pp->priority) {
  189.                 enqueue(pp, qq);
  190.         }
  191.         else if (qq->front->priority > pp->priority) {
  192.                 qq->front = pp;
  193.                 pp->next = check.front;
  194.                 printf("%d added to queue.\n", pp->id);
  195.         }
  196.         else if (qq->front->priority < pp->priority) {
  197.                 while (check.front->priority < pp->priority && check.front->next != NULL) {
  198.                         check.front = check.front->next;
  199.                 }
  200.                 while (check.rear->next != check.front) {
  201.  
  202.                         check.rear = check.rear->next;
  203.                 }
  204.                 check.rear->next = pp;
  205.                 if (check.front == NULL) {
  206.                         pp->next = NULL;
  207.                 }
  208.                 else {
  209.                         pp->next = check.front;
  210.                         printf("%d added to queue.\n", pp->id);
  211.                 }
  212.         }
  213.         else {
  214.                 printf("Error!");
  215.         }
  216. }
  217.  
  218.  
  219. void delete(pcb_t *pp, que_t *qq) {
  220.         pcb_t *temp = qq->front;
  221.         if (qq->front == NULL && qq->rear == NULL) {
  222.                 printf("Queue is Empty!");
  223.         }
  224.         else if (pp->id == qq->front->id) {
  225.                 dequeue(qq);
  226.         }
  227.         else if (pp->id == qq->rear->id) {
  228.                 while (temp->next->next != NULL) {
  229.                         temp = temp->next;
  230.                 }
  231.                 temp->next = NULL;
  232.                 qq->rear = temp;
  233.         }
  234.         else {
  235.                 while (pp->id != temp->next->id) {
  236.                         temp = temp->next;
  237.                 }
  238.                 temp->next = temp->next->next;
  239.         }
  240. }
  241.  
  242.  
  243. void
  244. Print (que_t * qq)
  245. {
  246.  
  247.   pcb_t *pp;
  248.  
  249.   printf ("Print: ");
  250.  
  251.   for (pp = qq->front; pp != 0; pp = pp->next)
  252.  
  253.     {
  254.  
  255.       printf ("%d ", pp->id);
  256.  
  257.     }
  258.  
  259.   putchar ('\n');
  260.  
  261. }
  262.  
  263. sem_t sem_fcfs, sem_pb;
  264.  
  265. void fcfsFunction(int id)
  266. {
  267.     sem_wait(&sem_fcfs);
  268.     // Critical Section
  269.     sleep(2);
  270.     // Logic Goes Here
  271.     printf("check_in for fcfs process %i\n",id);
  272.     int i;
  273.     que_t queue;
  274.     queue.state = 1;
  275.     init_queue (&queue);
  276.     pcb_t Array[10];
  277.     for (i=1; i<= 3;i++) {
  278.     pcb_t pp=init_pcb(&pp);
  279.     pp.id=i;
  280.     Array[i]=pp;
  281.     }
  282.     enqueue(&Array[2], &queue);
  283.     enqueue(&Array[1], &queue);
  284.     enqueue(&Array[3], &queue);
  285.     printf("FCFS Queue");
  286.     Print(&queue);
  287.     // End Of Critical Section
  288.     sem_post(&sem_fcfs);
  289. }
  290. void pbFunction(int id)
  291. {
  292.     sem_wait(&sem_pb);
  293.     // Critical Section
  294.     sleep(2);
  295.     // Logic Goes Here
  296.     printf("check_in for pb process %i\n",id);
  297.     int j;
  298.     que_t queue;
  299.     queue.state = 1;
  300.     init_queue (&queue);
  301.     pcb_t Array[10];
  302.     for (j=4; j<=6; j++) {
  303.         pcb_t pp=init_pcb(&pp);
  304.         pp.id=j;
  305.         pp.priority=j;
  306.         Array[j]=pp;
  307.     }
  308.      insert(&Array[5], &queue);
  309.      insert(&Array[4], &queue);
  310.      insert(&Array[6], &queue);
  311.      printf("Priority Based Queue");
  312.     Print(&queue);
  313.     // End Of Critical Section
  314.     sem_post(&sem_pb);
  315. }
  316.  
  317. void *startExecution(void *num)
  318.  
  319. {
  320.  
  321.     int id = *(int *)num;
  322.    
  323.     printf("hello process %i\n",id);
  324.     fcfsFunction(id);
  325.     pbFunction(id);
  326.    
  327.     printf("good bye process %i!\n",id);
  328.  
  329. }
  330.  
  331. int
  332. main ()
  333. {
  334.     int rc,i ;
  335.     pthread_t threads[threadNumber];
  336.     int threads_id[threadNumber];
  337.     sem_init(&sem_pb, 0, threadNumber);
  338.     sem_init(&sem_fcfs, 0, threadNumber);
  339.     // for(i=0 ; i< threadNumber ; i++)
  340.     // {
  341.         threads_id[0]=0;
  342.         rc = pthread_create(&threads[0], NULL,startExecution, (void *) &threads_id[0]);
  343.         rc = pthread_join(threads[0], NULL);
  344.     // }
  345.    
  346.     return 0;
  347. }
  348.