stuff
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -7,11 +7,13 @@
|
||||
* Consusmers wait for at least one filled spot before reading
|
||||
*/
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -60,8 +62,8 @@ unsigned char buffer_remove(circular_buffer *b)
|
||||
}
|
||||
|
||||
// Semaphores for controlling access
|
||||
sem_t fullCount;
|
||||
sem_t emptyCount;
|
||||
sem_t *fullCount;
|
||||
sem_t *emptyCount;
|
||||
|
||||
// Start flag
|
||||
volatile int start = 0;
|
||||
@@ -76,7 +78,7 @@ void *producer()
|
||||
for (int i = 0; i < ELEMENTS; i++)
|
||||
{
|
||||
// Wait for a free spot in the buffer
|
||||
sem_wait(&emptyCount);
|
||||
sem_wait(emptyCount);
|
||||
|
||||
// Add an element to the buffer
|
||||
unsigned char value = (i % 100) + 1; // Make sure the value isn't zero
|
||||
@@ -84,7 +86,7 @@ void *producer()
|
||||
printf("Producer added: %d\n", value);
|
||||
|
||||
// Signal the consumer that there something to consume
|
||||
sem_post(&fullCount);
|
||||
sem_post(fullCount);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
@@ -100,13 +102,13 @@ void *consumer()
|
||||
for (int i = 0; i < ELEMENTS; i++)
|
||||
{
|
||||
// Wait for element to consume
|
||||
sem_wait(&fullCount);
|
||||
sem_wait(fullCount);
|
||||
|
||||
// Remove an element from the buffer
|
||||
printf("Consumer removed: %d\n", buffer_remove(&buffer));
|
||||
|
||||
// Signal the producer that there is a free spot
|
||||
sem_post(&emptyCount);
|
||||
sem_post(emptyCount);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
@@ -115,8 +117,10 @@ void *consumer()
|
||||
// Main process
|
||||
int main()
|
||||
{
|
||||
sem_init(&fullCount, 0, 0);
|
||||
sem_init(&emptyCount, 0, BUFFER_SIZE);
|
||||
// sem_init(&fullCount, 0, 0);
|
||||
fullCount = sem_open("sem1", O_CREAT, 0777, 0);
|
||||
// sem_init(&emptyCount, 0, BUFFER_SIZE);
|
||||
emptyCount = sem_open("sem2", O_CREAT, 0777, BUFFER_SIZE);
|
||||
buffer_init(&buffer);
|
||||
|
||||
pthread_t prod;
|
||||
@@ -137,8 +141,10 @@ int main()
|
||||
pthread_join(prod, NULL);
|
||||
pthread_join(cons, NULL);
|
||||
|
||||
sem_destroy(&fullCount);
|
||||
sem_destroy(&emptyCount);
|
||||
sem_unlink("sem1");
|
||||
// sem_destroy(&fullCount);
|
||||
sem_unlink("sem2");
|
||||
// sem_destroy(&emptyCount);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,8 +1,8 @@
|
||||
s /*
|
||||
* race.c - Shows the race condition that can happen
|
||||
* then threads try to modify multiple global
|
||||
* values at the same time
|
||||
*/
|
||||
/*
|
||||
* race.c - Shows the race condition that can happen
|
||||
* then threads try to modify multiple global
|
||||
* values at the same time
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
@@ -10,8 +10,8 @@ s /*
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// start flag
|
||||
volatile int start = 0;
|
||||
// start flag
|
||||
volatile int start = 0;
|
||||
|
||||
// shared global
|
||||
static int counter1 = 0;
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,7 +1,7 @@
|
||||
s /*
|
||||
* sem_list2.c - Uses a semaphore as a way to control access
|
||||
* to a critical section for a singly linked list
|
||||
*/
|
||||
/*
|
||||
* sem_list2.c - Uses a semaphore as a way to control access
|
||||
* to a critical section for a singly linked list
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
@@ -10,7 +10,7 @@ s /*
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
typedef struct node
|
||||
typedef struct node
|
||||
{
|
||||
int val;
|
||||
struct node *next;
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>English</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>com.apple.xcode.dsym.sem_list2</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>dSYM</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
</dict>
|
||||
</plist>
|
||||
Binary file not shown.
Reference in New Issue
Block a user