Facebook
From fran ek, 1 Month ago, written in Ruby.
Embed
Download Paste or View Raw
Hits: 129
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <signal.h>
  6. #include "pqueue.h"
  7.  
  8. char *filename = "queue.dat";
  9.  
  10. int itemId = 0;
  11.  
  12. typedef struct item item;
  13. struct item {
  14.  int id;
  15. };
  16.  
  17.  
  18. item *
  19. produce() {
  20.  //int time = rand() % 5 + 1;
  21.  int time = 1;
  22.  item *it = (item *)malloc(sizeof(item));
  23.  
  24.  sleep(time);
  25.  printf("Produced.");
  26.  it->id = itemId;
  27.  itemId += 1;
  28.  return it;
  29. }
  30.  
  31. void
  32. consume(item *it) {
  33.  int time = rand() % 5 + 1;
  34.  sleep(time);
  35.  free(it);
  36. }
  37.  
  38.  
  39. void
  40. producer(pid_t childPid, pqueue *qu) {
  41.  
  42.  for(int i = 0; i<5;i++)
  43. {
  44.  item* item_new = produce();
  45.  printf("%d",item_new->id);
  46.  qinsert(&qu;, item_new, item_new->id);
  47.  qserialize(qu,sizeof(item_new),filename);
  48. }
  49. }
  50.  
  51. void
  52. consumer(pqueue *qu) {
  53. for(int i = 0; i>k);
  54.  sleep(1);
  55. }
  56. }
  57.  
  58. int
  59. main(int argc, char **argv) {
  60.  pid_t pid;
  61.  pqueue *qu = NULL;
  62.  printf("essa");
  63.  /* watch -n 1 ps -l --forest */
  64.  
  65.  /* create empty queue */
  66.  qserialize(qu, sizeof(item), filename);
  67.  
  68.  //int pid ;
  69.  printf (" Proces uruchomiony % d \n " , getpid () ) ;
  70.  pid = fork() ;
  71.  if ( pid == -1)
  72.  {
  73.   perror (" Blad utworzenia procesu potomnego !") ;
  74.   return 1;
  75.  }
  76.  if ( pid == 0)
  77.  {
  78.   printf (" Zglasza sie proces potomny % d \n " , getpid () ) ;
  79.   producer(getpid(), qu);
  80.   printf("Skonczylem");
  81.  }
  82.  else
  83.  {
  84.   printf (" Zlasza sie proces macierzysty % d . Potomek % d \n " ,
  85.   getpid () , pid ) ;
  86.  }
  87.  
  88.  consumer(qu);
  89.  
  90.  // producer(1,qu);
  91.  // pqueue* item_new = qpop(&qu;);
  92.  // printf("%d/n",item_new->k);
  93.  
  94.  return 0;
  95. }
  96.  
  97.