$re = '/\d+(.*)/m';
$str = '/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
6 #include <stdio.h>
7 #include <sys/types.h>
8 #include <string.h>
9 #include <errno.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <sys/mman.h>
13 #include <semaphore.h>
14 #include <sys/signal.h>
15 #include <signal.h>
16 #include <dirent.h>
17 #include <sys/stat.h>
18 #include <sys/wait.h>
19 #include <pthread.h>
20
21 #define BUFSIZE 10
22 #define TCOUNT 10
23 #define ITEMS 10000
24
25
26 struct item {
27 int value; // produced value
28 int source; // index of producer
29 };
30
31 struct buffer {
32 int count;
33 int in;
34 int out;
35 struct item data[BUFSIZE];
36 pthread_mutex_t mutex;
37 pthread_cond_t xp; // producer will wait for a slot using this
38 pthread_cond_t xc; // consumer fill wait for an item using this
39 };
40
41
42 struct buffer *buf; // shared bounded buffer
43
44 static void *
45 producer_thread(void *arg)
46 {
47
48 int index; // thread index
49 int i = 0;
50
51 index = (int) arg;
52
53 for (i = 0; i < ITEMS; ++i) {
54
55 // value of i is the item produced
56
57 pthread_mutex_lock(&buf->mutex);
58 while (buf->count == BUFSIZE)
59 pthread_cond_wait(&buf->xp, &buf->mutex);
60 // sleeps in this loop as long as buffer is full
61
7.1. POSIX NAMED SEMAPHORES 117
62
63 // put the item into buffer
64 buf->data[buf->in].value = i;
65 buf->data[buf->in].source = index;
66 buf->in = (buf->in + 1) % BUFSIZE;
67 buf->count++;
68
69 if (buf->count == 1)
70 pthread_cond_signal(&buf->xc);
71
72 pthread_mutex_unlock(&buf->mutex);
73
74 }
75
76 pthread_exit (NULL);
77 }
78
79
80
81 static void *
82 consumer_thread(void *arg)
83 {
84 int n = 0;
85 int x;
86 int s;
87
88 while (1) {
89 pthread_mutex_lock(&buf->mutex);
90
91 while (buf->count == 0)
92 pthread_cond_wait(&buf->xc, &buf->mutex);
93 // sleeps in this loop as long as buffer is empty
94
95
96
97 // retrive an item from buffer
98 x = buf->data[buf->out].value;
99 s = buf->data[buf->out].source;
100 buf->out = (buf->out + 1) % BUFSIZE;
101 buf->count--;
102
103 if (buf->count == (BUFSIZE - 1)) {
104 // wake up all possible workers;
105 // otherwise some workers will always sleep
106 pthread_cond_broadcast (&buf->xp);
107 }
108
109 pthread_mutex_unlock(&buf->mutex);
110
111 n++;
112
113 if (n == (TCOUNT * ITEMS))
114 break;
115 }
116
117 printf ("retrieved %d items\\n", n);
118 CHAPTER 7. SYNCHRONIZATION
118
119 printf ("consumer finished successfully\\n");
120
121 pthread_exit (NULL);
122 }
123
124
125 int
126 main(int argc, char **argv)
127 {
128
129 pthread_t pids[TCOUNT]; // producer tids
130 pthread_t ctid; // consumer tid
131 int i;
132 int ret;
133
134
135 buf = (struct buffer *) malloc(sizeof (struct buffer));
136 buf->count = 0;
137 buf->in = 0;
138 buf->out = 0;
139 pthread_mutex_init(&buf->mutex, NULL);
140 pthread_cond_init(&buf->xp, NULL);
141 pthread_cond_init(&buf->xc, NULL);
142
143
144 // create producer threads
145 for (i = 0; i < TCOUNT; ++i) {
146 ret = pthread_create(&pids[i], NULL, producer_thread,
147 (void *) i);
148 if (ret < 0) {
149 perror("thread create failed\\n");
150 exit(1);
151 }
152 }
153
154
155 // create consumer thread
156 ret = pthread_create(&ctid, NULL, consumer_thread, (void *) NULL);
157 if (ret < 0) {
158 perror("thread create failed\\n");
159 exit(1);
160 }
161
162
163 // wait for the producer threads to terminate
164 for (i = 0; i < TCOUNT; ++i)
165 pthread_join(pids[i], NULL);
166
167 // wait for the consumer thread to terminate
168 pthread_join(ctid, NULL);
169
170 free(buf);
171
172 pthread_mutex_destroy(&buf->mutex);
173 pthread_cond_destroy(&buf->xp);
7.2. PETERSON’S SOLUTION TO CRITICAL REGION PROBLEM 119
174 pthread_cond_destroy(&buf->xc);
175
176 printf("program ending...bye..\\n");
177 exit (0);
178 }';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php