Skip to content
Snippets Groups Projects
Commit 1d0a30ee authored by Barbu Alexandru Daniel's avatar Barbu Alexandru Daniel
Browse files

Fixing output file appending indefinitely #2.


Signed-off-by: default avatarBarbu Alexandru Daniel <barbualex.daniel2004@gmail.com>
parent 770711a1
No related branches found
No related tags found
1 merge request!5Solutions
Pipeline #92783 passed
{
"files.associations": {
"consumer.h": "c",
"ring_buffer.h": "c",
"packet.h": "c"
}
}
\ No newline at end of file
No preview for this file type
......@@ -5,6 +5,8 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include "ring_buffer.h"
#include "consumer.h"
......@@ -62,6 +64,8 @@ int main(int argc, char **argv)
thread_ids = calloc(num_consumers, sizeof(pthread_t));
DIE(thread_ids == NULL, "calloc pthread_t");
int out_fd_preliminar = open(argv[2], O_RDWR|O_CREAT|O_TRUNC, 0666);
close(out_fd_preliminar);
/* create consumer threads */
threads = create_consumers(thread_ids, num_consumers, &ring_buffer, argv[2]);
......@@ -71,7 +75,7 @@ int main(int argc, char **argv)
/* TODO: wait for child threads to finish execution*/
for (int i = 0; i < num_consumers; i++)
{
printf("joining thread: %d\n",i);
// printf("joining thread: %d\n",i);
pthread_join(thread_ids[i], NULL);
}
......
......@@ -51,7 +51,7 @@ ssize_t ring_buffer_enqueue(so_ring_buffer_t *ring, void *data, size_t size)
sem_wait(&ring->rb_empty);
pthread_mutex_lock(&ring->mutex);
printf("inside critical section of enqueue\n");
// printf("inside critical section of enqueue\n");
memcpy(ring->data + ring->write_pos * PKT_SIZE, data, size);
if (!memcmp(ring->data + ring->write_pos, data, size))
all_good = 0;
......@@ -87,11 +87,11 @@ ssize_t ring_buffer_dequeue(so_ring_buffer_t *ring, void *data, size_t size)
sem_wait(&ring->rb_full);
pthread_mutex_lock(&ring->mutex);
printf("inside critical section of dequeue\n");
// printf("inside critical section of dequeue\n");
if (ring->len == 0)
{
pthread_mutex_unlock(&ring->mutex);
printf("exiting weird case\n");
// printf("exiting weird case\n");
return -1;
}
......@@ -108,7 +108,7 @@ ssize_t ring_buffer_dequeue(so_ring_buffer_t *ring, void *data, size_t size)
sem_post(&ring->rb_empty);
pthread_mutex_unlock(&ring->mutex);
printf("exiting dequeue\n");
// printf("exiting dequeue\n");
return all_good;
}
......@@ -145,6 +145,6 @@ void ring_buffer_stop(so_ring_buffer_t *ring)
pthread_mutex_lock(&ring->mutex);
ring->producer_is_alive = 0;
sem_post(&ring->rb_full);
printf("producer stopped\n");
// printf("producer stopped\n");
pthread_mutex_unlock(&ring->mutex);
}
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment