Answer the question
In order to leave comments, you need to log in
Is the lock-free bitmap implemented correctly?
There is a bitmap and several streams, in this case two. Each thread receives the number of operations that it needs to perform. All threads receive different transaction numbers. Each of the operations is an array of type int, ending with -1 and containing the numbers of bits that must be set in the bit array, if it was not set before setting the bit, then the stream must output the number of this bit. Question: Is the lock-free bitmap implemented correctly in this code?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdatomic.h>
#include <threads.h>
#define BITS 2109 // Количество бит в битовом массиве
#define MAX_OPS 1000 // Максимальное количество операций
#define MAX_OP_SIZE 100 // Максимальный размер операции
static atomic_uchar bitset[(BITS>>3) + ((BITS&7) != 0)];
static int* ops[MAX_OPS];
static int num_ops;
// Структура для передачи потоку номеров операций, которые он должен выполнить
struct range { int from, to; };
// Функция инициализации массива операций случайными значениями
static inline void gen_ops() {
num_ops = 300 + rand()%(MAX_OPS-300);
for(int i = 0; i < num_ops; i++) {
int op_size = 20 + rand()%(MAX_OP_SIZE-20);
int *op = malloc(sizeof(int)*(op_size+1));
if(!op) {
perror("malloc");
exit(EXIT_FAILURE);
}
for(int j = 0; j < op_size; j++)
op[j] = rand()%BITS;
op[op_size] = -1;
ops[i] = op;
}
}
static inline void free_ops() {
for(int i = 0; i < num_ops; i++)
free(ops[i]);
}
// Функция, выполняющаяся в потоках
static int worker(void *arg) {
struct range *r = (struct range*)arg;
for(int i = r->from; i < r->to; i++) {
int *op = ops[i];
for(;;) {
int val = *op; // Получение номера бита, который необходимо установить
if(val < 0) // Если конец операции, выходим из цикла
break;
int or = 1 << (val&7);
// Атомарно устанавливаем нужный бит и получаем предыдущее значение
unsigned char byte = atomic_fetch_or(&bitset[val >> 3], or);
if((byte & or) == 0) {
// Если бит не был установлен до этого, выводим его номер
printf("%d\n", val);
}
op++;
}
}
return 0;
}
int main() {
srand(time(NULL));
for(int i = 0; i < sizeof(bitset); i++)
atomic_init(&bitset[i], 0);
gen_ops();
thrd_t thr1, thr2;
struct range r1 = {0, num_ops/2};
struct range r2 = {r1.to, num_ops};
thrd_create(&thr1, worker, &r1);
thrd_create(&thr2, worker, &r2);
thrd_join(thr1, NULL);
thrd_join(thr2, NULL);
free_ops();
}
Answer the question
In order to leave comments, you need to log in
First of all, web applications are a type of network applications. Therefore, it is imperative to understand how networks work and know the protocols. Especially HTTP.
Knowledge of HTML and CSS is a must. Knowledge of JavaScript is desirable, but knowledge of the jQuery library is not required. You need to know at least one server-side programming language. It could be PHP, or it could be something else. Naturally, since web programming is also programming, you need to know the basic data structures, algorithms, paradigms and patterns. Since a rare site happens without a database, you need to understand the principles of operation and design of relational databases and know SQL. It is advisable to use Linux confidently, as your site will be working on one of its varieties. It is desirable to be able to configure http servers and application servers, as well as to know the mechanisms of their interaction.
But the most important thing is to be able to search and analyze information .
Web programming is still a field that has a specialization. Your knowledge should be enough for slapping for doshirak, but if you want something more, then decide on a direction and dig there.
You can be a good developer if you know half of it.
But knowing well.
Or you can "know" a list of 50 different abbreviations and not even a terrible web programmer.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question