-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdouble_buffer_pingpong.c
More file actions
139 lines (112 loc) · 3.58 KB
/
double_buffer_pingpong.c
File metadata and controls
139 lines (112 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// double_buffer_pingpong.c
// Implementación real de doble búfer (ping-pong) con hilos
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <sys/time.h>
#define BUFFER_SIZE 262144 // 256 KB
#define NUM_BUFFERS 2
typedef struct {
char *data;
ssize_t size;
int full;
} buffer_t;
typedef struct {
int fd_in, fd_out;
buffer_t buffers[NUM_BUFFERS];
int read_index;
int write_index;
int done_reading;
pthread_mutex_t lock;
pthread_cond_t can_read;
pthread_cond_t can_write;
} shared_data;
void *reader_thread(void *arg) {
shared_data *sd = (shared_data *)arg;
while (1) {
pthread_mutex_lock(&sd->lock);
while (sd->buffers[sd->read_index].full) {
pthread_cond_wait(&sd->can_read, &sd->lock);
}
ssize_t bytes = read(sd->fd_in, sd->buffers[sd->read_index].data, BUFFER_SIZE);
if (bytes <= 0) {
sd->done_reading = 1;
pthread_cond_signal(&sd->can_write);
pthread_mutex_unlock(&sd->lock);
break;
}
// Simulación de cómputo pesado
for (volatile int i = 0; i < 100000000; i++);
sd->buffers[sd->read_index].size = bytes;
sd->buffers[sd->read_index].full = 1;
sd->read_index = (sd->read_index + 1) % NUM_BUFFERS;
pthread_cond_signal(&sd->can_write);
pthread_mutex_unlock(&sd->lock);
}
return NULL;
}
void *writer_thread(void *arg) {
shared_data *sd = (shared_data *)arg;
while (1) {
pthread_mutex_lock(&sd->lock);
while (!sd->buffers[sd->write_index].full && !sd->done_reading) {
pthread_cond_wait(&sd->can_write, &sd->lock);
}
if (!sd->buffers[sd->write_index].full && sd->done_reading) {
pthread_mutex_unlock(&sd->lock);
break;
}
write(sd->fd_out, sd->buffers[sd->write_index].data, sd->buffers[sd->write_index].size);
sd->buffers[sd->write_index].full = 0;
sd->write_index = (sd->write_index + 1) % NUM_BUFFERS;
pthread_cond_signal(&sd->can_read);
pthread_mutex_unlock(&sd->lock);
}
return NULL;
}
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Uso: %s <origen> <destino>\n", argv[0]);
return 1;
}
int fd_in = open(argv[1], O_RDONLY);
int fd_out = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd_in < 0 || fd_out < 0) {
perror("Error al abrir archivos");
return 1;
}
shared_data sd = {
.fd_in = fd_in,
.fd_out = fd_out,
.read_index = 0,
.write_index = 0,
.done_reading = 0,
.lock = PTHREAD_MUTEX_INITIALIZER,
.can_read = PTHREAD_COND_INITIALIZER,
.can_write = PTHREAD_COND_INITIALIZER
};
for (int i = 0; i < NUM_BUFFERS; i++) {
sd.buffers[i].data = malloc(BUFFER_SIZE);
sd.buffers[i].size = 0;
sd.buffers[i].full = 0;
}
struct timeval start, end;
gettimeofday(&start, NULL);
pthread_t reader, writer;
pthread_create(&reader, NULL, reader_thread, &sd);
pthread_create(&writer, NULL, writer_thread, &sd);
pthread_join(reader, NULL);
pthread_join(writer, NULL);
gettimeofday(&end, NULL);
double elapsed = (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0;
printf("Tiempo total (ping-pong double buffer): %.4f segundos\n", elapsed);
close(fd_in);
close(fd_out);
for (int i = 0; i < NUM_BUFFERS; i++) {
free(sd.buffers[i].data);
}
return 0;
}