fork download
  1. //Enter you code here.
  2. #include <pthread.h>
  3.  
  4. #include <semaphore.h>
  5.  
  6. #include <stdio.h>
  7.  
  8.  
  9.  
  10.  
  11.  
  12. int countval = 3;
  13.  
  14. int valu = 0;
  15.  
  16. sem_t semvalu;
  17.  
  18. pthread_mutex_t mutxvalu;
  19.  
  20.  
  21.  
  22. int h=0;
  23.  
  24. void *writer(void *wr)
  25.  
  26. {
  27.  
  28. sem_wait(&semvalu);
  29.  
  30. countval = countval * 4 + 1;
  31.  
  32. int c, d;
  33.  
  34.  
  35.  
  36. for (c = 1; c <= 32767; c++)
  37.  
  38. for (d = 1; d <= 32767; d++)
  39.  
  40. {}
  41.  
  42. printf("Writer %d modified countval to %d\n",(*((int *)wr)),countval);
  43.  
  44. printf("No of readers present are %d\n",h);
  45.  
  46. sem_post(&semvalu);
  47.  
  48. }
  49.  
  50.  
  51.  
  52. int g=6;
  53.  
  54. void *reader(void *rr)
  55.  
  56. {
  57.  
  58. // Reader acquire the lock before modifying numreader
  59.  
  60. pthread_mutex_lock(&mutxvalu);
  61.  
  62. valu++;
  63.  
  64. if(valu == 1) {
  65.  
  66. sem_wait(&semvalu); // If this id the first reader, then it will block the writer
  67.  
  68. }
  69.  
  70. pthread_mutex_unlock(&mutxvalu);
  71.  
  72. // Reading Section
  73.  
  74. int q,w;
  75.  
  76.  
  77.  
  78. for (q = 1; q <=32767; q++)
  79.  
  80. for (w = 1; w <= 32767; w++)
  81.  
  82. { }
  83.  
  84.  
  85.  
  86. printf("Reader %d countval is %d\n",*((int *)rr),countval);
  87.  
  88. printf("No of readers present are %d\n",g);
  89.  
  90.  
  91.  
  92. g--;
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. // Reader acquire the lock before modifying numreader
  101.  
  102. pthread_mutex_lock(&mutxvalu);
  103.  
  104. valu--;
  105.  
  106.  
  107.  
  108.  
  109.  
  110. if(valu == 0) {
  111.  
  112. sem_post(&semvalu); // If this is the last reader, it will wake up the writer.
  113.  
  114. }
  115.  
  116. pthread_mutex_unlock(&mutxvalu);
  117.  
  118. }
  119.  
  120.  
  121.  
  122.  
  123.  
  124. int main()
  125.  
  126. {
  127.  
  128.  
  129.  
  130. pthread_t read[6],write[6];
  131.  
  132. pthread_mutex_init(&mutxvalu, NULL);
  133.  
  134. sem_init(&semvalu,0,1);
  135.  
  136.  
  137.  
  138. int a[6] = {1,2,3,4,5,6}; //Just used for numbering the producer and consumer
  139.  
  140.  
  141.  
  142. for(int k = 1; k <=6 ; k++) {
  143.  
  144. pthread_create(&read[k-1], NULL, (void *)reader, (void *)&a[k-1]);
  145.  
  146. }
  147.  
  148.  
  149.  
  150. for(int k = 1; k <= 6; k++) {
  151.  
  152. pthread_create(&write[k-1], NULL, (void *)writer, (void *)&a[k-1]);
  153.  
  154. }
  155.  
  156.  
  157.  
  158.  
  159.  
  160. for(int k = 1; k <= 6; k++) {
  161.  
  162. pthread_join(read[k-1], NULL);
  163.  
  164. }
  165.  
  166.  
  167.  
  168. for(int k = 1; k <= 6; k++) {
  169.  
  170. pthread_join(write[k-1], NULL);
  171.  
  172. }
  173.  
  174. pthread_mutex_destroy(&mutxvalu);
  175.  
  176. sem_destroy(&semvalu);
  177.  
  178.  
  179.  
  180. return 0;
  181.  
  182. }
  183.  
  184.  
  185. //Please indent properly.
  186.  
  187. <?php
  188. //program to find the common elements of the two array
  189. //here we have to array A and B from which w have to find the common element
  190. //first we sort then using merge sort and after then for traversing through
  191. //the array in one iteration we can find the comman elements the given array
  192. //this is an inspace algorithm meansno extra space is needed
  193.  
  194. //best case time complexity=O(nlogn)
  195. //O(nlogn)-> for sorting
  196. //O(n)-> for while loop to find comman element
  197.  
  198. //average case time complexity=O(nlogn)
  199. //O(nlogn)-> for sorting
  200. //O(n)-> for while loop to find comman element
  201.  
  202. //worst case time complexity =O(nlogn)
  203. //O(nlogn)-> for sorting
  204. //O(n)-> for while loop to find comman element
  205.  
  206.  
  207.  
  208. $commonArray=array();
  209. $A=array(3,4,5,6,7,8,9,10,36,58,27,48);
  210. $B=array(3,10,4,5,6,8,12,24,37,27,50);
  211. sort($A);
  212. sort($B);
  213. $size1=sizeof($A);
  214. $size2=sizeof($B);
  215. $counter1=0;
  216. $counter2=0;
  217. while(($counter1< $size1) && ($counter2)<($size2))//traversing through the array
  218. {
  219.  
  220. if ($A[$counter1] == $B[$counter2])
  221. {
  222. array_push($commonArray,$A[$counter1]); //to enter comman element in the output array
  223. $counter1=$counter1+1;
  224. $counter2=$counter2+1;
  225. }
  226. else if ($A[$counter1] < $B[$counter2])
  227. {
  228. $counter1=$counter1+1; }
  229.  
  230. else
  231. {
  232. $counter2=$counter2+1;
  233. }
  234. }
  235.  
  236. print_r($commonArray);//to print the output array
  237. ?>
  238.  
  239.  
Success #stdin #stdout 0.01s 24496KB
stdin
Standard input is empty
stdout
//Enter you code here.
#include <pthread.h>

#include <semaphore.h>

#include <stdio.h>





int countval = 3;

int valu = 0;

sem_t semvalu;

pthread_mutex_t mutxvalu;



int h=0;

 void *writer(void *wr)

{

    sem_wait(&semvalu);

    countval = countval * 4 + 1;

   int c, d;

   

   for (c = 1; c <= 32767; c++)

       for (d = 1; d <= 32767; d++)

       {}

    printf("Writer %d modified countval to %d\n",(*((int *)wr)),countval);

    printf("No of readers present are %d\n",h);   

 sem_post(&semvalu);

}



int g=6;

void *reader(void *rr)

{

    // Reader acquire the lock before modifying numreader

    pthread_mutex_lock(&mutxvalu);

    valu++;

    if(valu == 1) {

        sem_wait(&semvalu); // If this id the first reader, then it will block the writer

    }

    pthread_mutex_unlock(&mutxvalu);

    // Reading Section

   int q,w;

   

   for (q = 1; q <=32767; q++)

       for (w = 1; w <= 32767; w++)

        { }



    printf("Reader %d countval is %d\n",*((int *)rr),countval);

    printf("No of readers present are %d\n",g);



    g--;

  

   

   

 // Reader acquire the lock before modifying numreader

    pthread_mutex_lock(&mutxvalu);

    valu--;





 if(valu == 0) {

        sem_post(&semvalu); // If this is the last reader, it will wake up the writer.

    }

    pthread_mutex_unlock(&mutxvalu);

}





int main()

{



    pthread_t read[6],write[6];

    pthread_mutex_init(&mutxvalu, NULL);

    sem_init(&semvalu,0,1);



    int a[6] = {1,2,3,4,5,6}; //Just used for numbering the producer and consumer



    for(int k = 1; k <=6  ; k++) {

        pthread_create(&read[k-1], NULL, (void *)reader, (void *)&a[k-1]);

    }

  

    for(int k = 1; k <= 6; k++) {

        pthread_create(&write[k-1], NULL, (void *)writer, (void *)&a[k-1]);

    }



   

    for(int k = 1; k <= 6; k++) {

        pthread_join(read[k-1], NULL);

    }



    for(int k = 1; k <= 6; k++) {

        pthread_join(write[k-1], NULL);

  }

    pthread_mutex_destroy(&mutxvalu);

    sem_destroy(&semvalu);



    return 0;

}


//Please indent properly.

Array
(
    [0] => 3
    [1] => 4
    [2] => 5
    [3] => 6
    [4] => 8
    [5] => 10
    [6] => 27
)