Skip to content

Instantly share code, notes, and snippets.

@sgillen
Last active May 3, 2019 08:24
Show Gist options
  • Save sgillen/34974c562bef7984900894356e220cad to your computer and use it in GitHub Desktop.
Save sgillen/34974c562bef7984900894356e220cad to your computer and use it in GitHub Desktop.
CS 170 project 3 example code.
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <queue>
#define BUFFER_SIZE 4
static std::queue<int> buffer;
static sem_t full;
static sem_t empty;
static sem_t mutex_sem; //using as a mutex
void* producer(void*);
void* consumer(void*);
void print_queue(std::queue<int> q)
{
while (!q.empty())
{
printf("%i ", q.front());
q.pop();
}
printf("\n");
}
int main(void){
pthread_t tid1; pthread_t tid2;
sem_init(&mutex_sem, 0, 1);
sem_init(&full, 0, 0);
sem_init(&empty, 0, BUFFER_SIZE);
pthread_create(&tid1, NULL, &consumer, NULL);
pthread_create(&tid2, NULL, &producer, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
sem_destroy(&mutex_sem);
sem_destroy(&full);
sem_destroy(&empty);
}
void* producer(void* arg){
int produced_item = 0;
for(int i = 0; i < 10; i++){
usleep(rand() %300); // produce the item;
produced_item = rand() %100;
sem_wait(&empty);
sem_wait(&mutex_sem);
buffer.push(produced_item);
printf("pushed new item: buffer is now: ");
print_queue(buffer);
sem_post(&mutex_sem);
sem_post(&full);
}
}
void* consumer(void* arg){
int consumed_item = 0;
for(int i = 0; i < 10; i++){
sem_wait(&full);
sem_wait(&mutex_sem);
consumed_item = buffer.front();
buffer.pop();
printf("popped new item: buffer is now: ");
print_queue(buffer);
sem_post(&mutex_sem);
sem_post(&empty);
usleep(rand() %500); //consume the item
}
}
#include <pthread.h>
#include <stdio.h>
void* _thread_join1(void* arg){
for(int i = 0 ; i < 3; i++){
printf("In thread 1\n");
}
*(int*)arg = 1;
return (void*)arg;
}
void* _thread_join2(void* arg){
for(int i = 0 ; i < 3; i++){
printf("In thread 2\n");
}
*(int*)arg = 2;
pthread_exit((void*)arg);
}
int main(void){
pthread_t tid1 = 0; pthread_t tid2 = 0;
int a1; int a2;
int* pa1 = &a1;
int* pa2 = &a2;
printf("main thread: creating thread 1\n");
pthread_create(&tid1, NULL, &_thread_join1, &a1);
printf("main thread: joining\n");
pthread_join(tid1, (void**)&pa1);
printf("main thread: joined with thread 1, return value was %i\n", *pa1);
printf("main thread: creating thread 2\n");
pthread_create(&tid2, NULL, &_thread_join2, &a1);
printf("main thread: joining\n");
pthread_join(tid2, (void**)&pa2);
printf("main thread: joined with thread 2, return value was %i\n", *pa2);
}
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void lock(void);
void unlock(void);
void* _thread_1(void* arg){
lock();
for(int i = 0 ; i < 3; i++){
printf("In thread 1\n");
sleep(1);
}
//unlock();
while(1);
return arg;
}
void* _thread_2(void* arg){
lock();
for(int i = 0 ; i < 3; i++){
printf("In thread 2\n");
sleep(1);
}
unlock();
return arg;
}
int main(void){
pthread_t tid1 = 0; pthread_t tid2 = 0;
printf("main thread: creating thread 1\n");
pthread_create(&tid1, NULL, &_thread_1, NULL);
pthread_create(&tid2, NULL, &_thread_2, NULL);
pthread_join(tid2, NULL);
pthread_join(tid1, NULL);
}
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <semaphore.h>
sem_t sem;
void* _thread_1(void* arg){
printf("thread1 waiting on sem\n");
sem_wait(&sem);
for(int i = 0 ; i < 3; i++){
printf("In thread 1\n");
sleep(1);
}
return arg;
}
void* _thread_2(void* arg){
printf("thread2 waiting on sem\n");
sem_wait(&sem);
for(int i = 0 ; i < 3; i++){
printf("In thread 2\n");
sleep(1);
}
return arg;
}
int main(void){
pthread_t tid1 = 0; pthread_t tid2 = 0;
sem_init(&sem, 0, 2);
sem_wait(&sem); // waiting twice her is useless except as a sanity check
sem_wait(&sem);
printf("main thread: creating threads\n");
pthread_create(&tid1, NULL, &_thread_1, NULL);
pthread_create(&tid2, NULL, &_thread_2, NULL);
sleep(1);
printf("main thread: posting sems\n");
sem_post(&sem);
sem_post(&sem);
pthread_join(tid2, NULL);
pthread_join(tid1, NULL);
sem_destroy(&sem);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment