Facebook
From ZZZ, 4 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 171
  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 Allocated 1
  8. #define UnAllocated 0
  9. #define READY 1
  10. #define BLOCKED -1
  11. #define ERR_NO_SPACE 999
  12. #define N_SEM 10
  13. #define RANGE_ERR 666
  14.  
  15. typedef struct pcb
  16. {
  17.   sem_t sema;
  18.   int id;
  19.   int priority;
  20.   struct pcb *next;
  21.   struct pcb *prev;
  22.   int state;
  23.   int alloc;
  24.   int type;
  25.  
  26. } pcb_t;
  27.  
  28. typedef struct queue
  29. {
  30.  
  31.   pcb_t *front;
  32.  
  33.   pcb_t *rear;
  34.  
  35.   pcb_t *pcb_table[n];
  36.  
  37.   int state;
  38.  
  39. } que_t;
  40.  
  41. /*    New Added   */
  42. typedef struct {
  43.     int val;
  44.     int alloc;
  45.     que_t qa;
  46. } sema_t;
  47. /* /////////////// */
  48.  
  49.  
  50. que_t
  51. init_queue (que_t * qq)
  52. {
  53.  
  54.   qq->front = NULL;
  55.  
  56.   qq->rear = NULL;
  57.  
  58.   return *qq;
  59.  
  60. };
  61.  
  62.  
  63.  
  64. pcb_t
  65. init_pcb (pcb_t * pp)
  66. {
  67.  
  68.   pp->id = rand () % 10 + 1;
  69.  
  70.   pp->priority = rand () % 5 + 1;
  71.  
  72.   return *pp;
  73.  
  74. };
  75.  
  76.  
  77.  
  78. void
  79. print_list (pcb_t * head)
  80. {
  81.  
  82.   pcb_t *current = head;
  83.  
  84.   while (current != NULL)
  85.  
  86.     {
  87.  
  88.       printf ("%d\n", current->id);
  89.  
  90.       current = current->next;
  91.  
  92.     }
  93.  
  94. };
  95.  
  96.  
  97. void
  98. enqueue (pcb_t * pp, que_t * qq)
  99. {
  100.  
  101.   if (qq->front == NULL && qq->rear == NULL)
  102.  
  103.     {
  104.  
  105.       qq->rear = pp;
  106.  
  107.       qq->front = pp;
  108.  
  109.       pp->next = NULL;
  110.  
  111.     }
  112.  
  113.   else
  114.  
  115.     {
  116.  
  117.       qq->rear->next = pp;
  118.  
  119.       qq->rear = pp;
  120.  
  121.       pp->next = NULL;
  122.  
  123.     }
  124.  
  125.  
  126. };
  127.  
  128.  
  129.  
  130. pcb_t* dequeue (que_t * qq)
  131. {
  132.  
  133.   if (qq->front == NULL && qq->rear == NULL)
  134.  
  135.     {
  136.  
  137.       printf ("Queue is Empty\n");
  138.  
  139.     }
  140.  
  141.   else
  142.  
  143.     {
  144.  
  145.       pcb_t *temp = qq->front;
  146.  
  147.       qq->front = qq->front->next;
  148.  
  149.       printf ("Removed item is: %d\n", temp->id);
  150.       return &temp;
  151.     }
  152.  
  153. }
  154.  
  155. int
  156. isFull (que_t * qq)
  157. {
  158.  
  159.   int full = 0;
  160.  
  161.   if (qq->rear->priority == n - 1)
  162.  
  163.     {
  164.  
  165.       full = 1;
  166.  
  167.     }
  168.  
  169.   return full;
  170.  
  171. }
  172.  
  173.  
  174. int
  175. isEmpty (que_t * qq)
  176. {
  177.  
  178.   int empty = 0;
  179.  
  180.   if (qq->front == qq->rear + 1)
  181.  
  182.     {
  183.  
  184.       empty = 1;
  185.  
  186.     }
  187.  
  188.   return empty;
  189.  
  190. }
  191.  
  192.  
  193. void insert(pcb_t *pp, que_t *qq) {
  194.         que_t check;
  195.         check.state = qq->state;
  196.         check.front = qq->front;
  197.         check.rear = qq->front;
  198.  
  199.         if (qq->front == NULL && qq->rear == NULL) {
  200.                 enqueue(pp, qq);
  201.         }
  202.         else if (qq->rear->priority < pp->priority) {
  203.                 enqueue(pp, qq);
  204.         }
  205.         else if (qq->front->priority > pp->priority) {
  206.                 qq->front = pp;
  207.                 pp->next = check.front;
  208.                 printf("%d added to queue.\n", pp->id);
  209.         }
  210.         else if (qq->front->priority < pp->priority) {
  211.                 while (check.front->priority < pp->priority && check.front->next != NULL) {
  212.                         check.front = check.front->next;
  213.                 }
  214.                 while (check.rear->next != check.front) {
  215.  
  216.                         check.rear = check.rear->next;
  217.                 }
  218.                 check.rear->next = pp;
  219.                 if (check.front == NULL) {
  220.                         pp->next = NULL;
  221.                 }
  222.                 else {
  223.                         pp->next = check.front;
  224.                         printf("%d added to queue.\n", pp->id);
  225.                 }
  226.         }
  227.         else {
  228.                 printf("Error!");
  229.         }
  230. }
  231.  
  232.  
  233. void delete(pcb_t *pp, que_t *qq) {
  234.         pcb_t *temp = qq->front;
  235.         if (qq->front == NULL && qq->rear == NULL) {
  236.                 printf("Queue is Empty!\n");
  237.         }
  238.         else if (pp->id == qq->front->id) {
  239.                 dequeue(qq);
  240.         }
  241.         else if (pp->id == qq->rear->id) {
  242.                 while (temp->next->next != NULL) {
  243.                         temp = temp->next;
  244.                 }
  245.                 temp->next = NULL;
  246.                 qq->rear = temp;
  247.         }
  248.         else {
  249.                 while (pp->id != temp->next->id) {
  250.                         temp = temp->next;
  251.                 }
  252.                 temp->next = temp->next->next;
  253.         }
  254. }
  255.  
  256.  
  257. void
  258. Print (que_t * qq)
  259. {
  260.  
  261.   pcb_t *pp;
  262.  
  263.   printf ("Print: ");
  264.  
  265.   for (pp = qq->front; pp != 0; pp = pp->next)
  266.  
  267.     {
  268.  
  269.       printf ("%d ", pp->id);
  270.  
  271.     }
  272.  
  273.   putchar ('\n');
  274.  
  275. }
  276.  
  277. pcb_t pcb_table[n];
  278. que_t fcfsQueue;
  279. que_t pbQueue;
  280. int pid;
  281.  
  282. void fcfsFunction(int i)
  283. {
  284.     pcb_t Array[10];
  285.     pcb_t pp=init_pcb(&pp);
  286.     pp.id=i;
  287.     pcb_table[i]=pp;
  288.     enqueue(&pcb_table[i], &fcfsQueue);
  289.     printf("    ~~#~~FCFS Queue");
  290.     Print(&fcfsQueue);
  291. }
  292. void pbFunction(int i)
  293. {
  294.     int jj;
  295.     pcb_t Array[10];
  296.     pcb_t pp=init_pcb(&pp);
  297.     pp.id=i;
  298.     pp.priority=pcb_table[i].priority;
  299.     pcb_table[i]=pp;
  300.     insert(&pcb_table[i], &pbQueue);
  301.     printf("    ~~#~~Priority Based Queue\n");
  302.     Print(&pbQueue);
  303. }
  304.  
  305. void *scheduler()
  306. {
  307.     sem_wait(&pcb_table[pid].sema);
  308.         sleep(2);
  309.     if(pcb_table[pid].type == 0 && pcb_table[pid].state == READY) {
  310.         fcfsFunction(pid);
  311.     } else if (pcb_table[pid].type == 1 && pcb_table[pid].state == READY) {
  312.         pbFunction(pid);
  313.     }
  314.     else if (pcb_table[pid].state == BLOCKED) {
  315.         printf("    Sorry seems BLOCKED ... PID = %d!\n",pid);
  316.     }
  317.     sem_post(&pcb_table[pid].sema);
  318. }
  319.  
  320. void block(int pid) {
  321.     pcb_table[pid].state = BLOCKED;
  322.     printf("#    Memory Address %d is BLOCKED\n",pid);
  323. }
  324.  
  325. void unblock(int pid) {
  326.     pcb_table[pid].state = READY;
  327.     printf("#    Memory Address %d is READY\n",pid);
  328. }
  329.  
  330. void deletePid(int pid) {
  331.     pcb_t* pt;
  332.     pt=&pcb_table[pid];
  333.     pt->state = NULL;
  334.     pt->alloc = UnAllocated;
  335.     if (pt->type == 0) {
  336.         delete(pt,&fcfsQueue);
  337.         printf("FCFSSSSSSSS\n");
  338.     } else if(pt->type == 1) {
  339.         delete(pt,&pbQueue);
  340.         printf("PRIORITY BASEDDDDD\n");
  341.     }
  342.     printf("#    Memory Address %d is DELETED!\n",pid);
  343. }
  344.  
  345. int make_proc(int address , int type , int prio) {
  346.     printf("process number %i\n",address);
  347.     int i; pcb_t* pt;
  348.     for(i=0; i<=n; i++) {
  349.         pt=&pcb_table[i];
  350.         if(pt->alloc = UnAllocated) {
  351.             break;
  352.         }
  353.         else if(i==n) {
  354.             return ERR_NO_SPACE;
  355.         }
  356.         else if (pt->priority || pt->state == BLOCKED) {
  357.             continue;
  358.         }
  359.         pid = i;
  360.         printf("    Memory location %d seems free...implementing there\n",i);
  361.         break;
  362.     }
  363.     pt=&(pcb_table[pid]);
  364.     pt->type = type;
  365.     pt->priority = prio;
  366.     pthread_t p;
  367.     if (!pt->state) {
  368.         pt->state = READY;
  369.     }
  370.     sem_init(&pt->sema,NULL,1);
  371.     pt->alloc = Allocated;
  372.     pthread_create(&p,NULL,scheduler,NULL);
  373.     pthread_join(p, NULL);
  374.     printf("bye process number %i!\n",address);
  375.     printf("//////////////////////////////////////\n");
  376. }
  377. /*        NEW CODE      */
  378. sema_t sem_table[N_SEM];
  379.  
  380. sema_wait(int s) {
  381.     sema_t* sp;
  382.     if( s<0 || s>N_SEM -1) {
  383.         return (RANGE_ERR);
  384.         printf("\nNO SPACE M8\n");
  385.     }
  386.     sp = &(sem_table[s]);
  387.     sp->val--;
  388.     if(sp->val < 0) {
  389.         enqueue(&pcb_table[pid],&sp->qa);
  390.         Print(&sp->qa);
  391.         block(pid);
  392.     }
  393. }
  394.  
  395. sema_signal(int s) {
  396.     sema_t *sp; pcb_t* pp;
  397.     if( s<0 || s>N_SEM -1) {
  398.         return (RANGE_ERR);
  399.         printf("\nNO SPACE M8\n");
  400.     }
  401.     sp=&(sem_table[s]);
  402.     sp->val++;
  403.     if(sp->val<=0) {
  404.         pp = dequeue(&sp->qa);
  405.         unblock(&pp->id);
  406.     }
  407. }
  408.  
  409. alloc_sem() {
  410.     int i;
  411.     for(i=0;i<N_SEM;i++) {
  412.         if(sem_table[i].alloc = UnAllocated) {
  413.             printf("\nitem %d is already Allocated!\n",i);
  414.             continue;
  415.         }
  416.         printf("\nSetting its alloc / val / qa.front for %d\n",i);
  417.         sem_table[i].alloc = Allocated;
  418.         sem_table[i].val = 0;
  419.         sem_table[i].qa.front = NULL;
  420.     }
  421.     return (ERR_NO_SPACE);
  422. }
  423.  
  424. /* /////////////////////  */
  425. int
  426. main ()
  427. {
  428.     alloc_sem();
  429.     int i =0;
  430.     for(i=0;i<N_SEM;i++){
  431.     sema_wait(i);
  432.     sema_signal(i);
  433.     }
  434.     // make_proc (PROCESS_ID , TYPE_OF_PROCESS[PRIORITY BASED / FCFS] , PRIORITY ) //
  435.     // HERE ALL OF THEM ARE FCFS
  436.     // block(0);
  437.     // make_proc(0,0,3);
  438.     // make_proc(3,0,2);
  439.     // make_proc(2,0,1);
  440.     // unblock(0);
  441.     // deletePid(1);
  442.     // make_proc(5,0,0);
  443.     // make_proc(1,0,6);
  444.     // make_proc(6,0,5);
  445.    
  446.     return 0;
  447. }
  448.