NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <stdio.h>
#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);

/* Produce value(s) */

void *producer(void *param) {
int i;
char mess[15];
for (i = 1; i <= 10; i++) {
/* TODO: Insert into buffer */
pthread_mutex_lock(&m);
if (num > BUF_SIZE) exit(1); /* overflow */
while (num == BUF_SIZE)
/* TODO: block if buffer is full */
pthread_cond_wait(&c_prod, &m);
/* 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);

// TODO
pthread_mutex_unlock(&m);
pthread_cond_signal(&c_cons);
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) {
// TODO: lock mutex
pthread_mutex_lock(&m);
if (num < 0) exit(1); /* underflow */
while (num == 0) /* TODO: block if buffer empty */
pthread_cond_wait(&c_cons, &m);
/* if executing here, buffer not empty so remove element */
strcpy(mess, buffer[rem]);
rem = (rem + 1) % BUF_SIZE;
num--;
printf("Consume: reads %sn", mess);

// TODO: unlock mutex
pthread_mutex_unlock(&m);

// TODO: send message to producer condition is running.
pthread_cond_signal(&c_prod);
fflush(stdout);
}
return 0;
}

int main(int argc, char *argv[]) {
pthread_t tid1, tid2; /* thread identifiers */
int i;
/* TODO: create the producer thread */
if (pthread_create(&tid1, NULL, producer, NULL) !=0) {
fprintf(stderr, "Unable to create producer threadn");
exit(1);
}
/* TODO: create the consumer thread */
if (pthread_create(&tid2, NULL, consumer, NULL) !=0) {
fprintf(stderr, "Unable to create consumer threadn");
exit(1);
}
/* TODO: wait for created threads to exit */
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
printf("Parent exitingn");
return 0;
}
     
 
what is notes.io
 

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

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.