wangzhengquan
2020-05-25 88eda4c0c73d09dee323d8e0b6a0c9287464beff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "test.h"
 
 
using namespace std;
 
SQueue<struct Item> *queue;
 
 
void sigint_handler(int sig) {
  cerr << "sigint_handler" << endl;
  destroy();
  exit(0);
 
}
 
int main(int argc, char *argv[])
  void *shmp;
  string line;
  signal(SIGINT,  sigint_handler);
  /* Create set containing two semaphores; initialize so that
     writer has first access to shared memory. */
  int start = 0;
  int end = -1; 
 
  if (argc == 3) {
    start = atoi(argv[1]);
    end = atoi(argv[2]);
  }
 
  int first = mm_init(sizeof(SQueue<struct Item>), &shmp);
  
  if (first == 1)
    queue = new(shmp) SQueue<struct Item>(1);
  else
    queue = (SQueue<struct Item> *) shmp;
 
  /* Transfer blocks of data from stdin to shared memory */
  struct Item item;
  struct timespec timeout = {5, 0};
  int i = start;
  item.pic = i;
  item.info = i;
  while((end == -1 || (i < end) ) && (queue->enqueue_timeout(item, &timeout)) ) {
    item.pic = i;
    item.info = i;
 
    // cout << "入队:" << item.pic << ", " << item.info << endl;
    cout <<  item.pic << endl;
 
    i++;
    
  }
   
  cerr << "productor quit" << endl;
  exit(EXIT_SUCCESS);
}