Notes
Notes - notes.io |
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#define BUF_SIZE 3 /* Size of shared buffer */
char buffer[BUF_SIZE][15]; /* shared buffer */
int add = 0; /* place to add next element */
int rem = 0;
/* place to remove next element
*/
int num = 0; /* number elements in buffer */
/* mutex lock for buffer */
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
/* consumer waits on this cond var */
pthread_cond_t c_cons = PTHREAD_COND_INITIALIZER;
/* producer waits on this cond var */
pthread_cond_t c_prod = PTHREAD_COND_INITIALIZER;
void *producer(void *param);
void *consumer(void *param);
int main(int argc, char *argv[]) {
pthread_t tid1, tid2; /* thread identifiers */
int i;
/* create the producer threads*/
if (pthread_create([1]) != 0) {
fprintf(stderr, "Unable to create producer threadn");
exit(1);
}
/* create the consumer threads*/
if (pthread_create([2]) != 0) {
fprintf(stderr, "Unable to create consumer threadn");
exit(1);
}
/* wait for created thread to exit */
pthread_join([3]);
pthread_join([4]);
printf("Parent quitingn");
return 0;
}
/* Produce value(s) */
void *producer(void *param) {
int i;
char mess[15];
for (i = 1; i <= 10; i++) {
/* Insert into buffer */
pthread_mutex_lock([5]);
if (num > BUF_SIZE) exit(1); /* overflow */
while (num == BUF_SIZE) /* block if buffer is full */
pthread_cond_wait([6]);
/* if executing here, buffer not full so add element */
sprintf(mess, "Message %d", i);
strcpy(buffer[add], mess);
add = (add + 1) % BUF_SIZE;
num++;
printf("Producer: writes %sn", mess);
pthread_mutex_unlock([7]);
pthread_cond_signal([8]);
fflush(stdout);
}
printf("producer quitingn");
fflush(stdout);
return 0;
}
/* Consume value(s); Note the consumer never terminates */
void *consumer(void *param) {
int i;
char mess[15];
while (1) {
[9];
if (num < 0) exit(1); /* underflow */
while (num == 0) /* block if buffer empty */
[10];
/* if executing here, buffer not empty so remove element */
strcpy(mess, buffer[rem]);
rem = (rem + 1) % BUF_SIZE;
num--;
printf("Consume: reads %sn", mess);
[11];
[12];
fflush(stdout);
}
return 0;
}
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team