标签:char pen pid read type stat creat yield while
每次出错的都是:
{fn_ofd_r, fn_posix_w, fn_dummy, "OFD read lock vs POSIX write lock"},
static int thread_cnt;
static int fail_flag = 0;
static volatile int loop_flag = 1;
static const int max_thread_cnt = 32;
static const char fname[] = "tst_ofd_posix_locks"; //文件名
static const long write_size = 4096;
static pthread_barrier_t barrier;
struct param {
long offset;
long length;
long cnt;
};
thread_cnt = tst_ncpus_conf() * 3; //四核 4*3
if (tst_fill_file(fname, 1, write_size, thread_cnt + 1))
写4096 (4+1)次 写入fname 写入1
if (pthread_barrier_init(&barrier, NULL, thread_cnt*3) != 0)
初始化屏障结构函数,设计屏障等待的最大线程数目(4*3)
for (i = 0; i < thread_cnt; i++) {
p0[i].offset = i * write_size;
p0[i].length = write_size;
p0[i].cnt = i + 2;
p1[i].offset = i * write_size + write_size / 4;
p1[i].length = write_size;
p1[i].cnt = i + 2;
p2[i].offset = i * write_size + write_size / 2;
p2[i].length = write_size;
p2[i].cnt = i + 2;
}
P0[0]={0,4096,2}
P1[0]={1024,4096,2}
P2[0]={2048,4096,2}
P0[1]={4096,4096,3}
P1[1]={5120,4096,3}
P2[1]={6144,4096,3}
P0[2]={8192,4096,4}
P1[2]={9216,4096,4}
P2[2]={10240,4096,4}
P0[3]={12288,4096,5}
P1[3]={13312,4096,5}
P2[3]={14336,4096,5}
fail_flag = 0;
loop_flag = 1;
for (i = 0; i < thread_cnt; i++) {
SAFE_PTHREAD_CREATE(id0 + i, NULL, f0, (void *)&p0[i]);
SAFE_PTHREAD_CREATE(id1 + i, NULL, f1, (void *)&p1[i]);
SAFE_PTHREAD_CREATE(id2 + i, NULL, f2, (void *)&p2[i]);
}
static struct tcase {
void *(*fn0)(void *);
void *(*fn1)(void *);
void *(*fn2)(void *);
const char *desc;
} tcases[] = {
{fn_ofd_r, fn_ofd_w, fn_dummy, "OFD read lock vs OFD write lock"},
{fn_ofd_w, fn_posix_w, fn_dummy, "OFD write lock vs POSIX write lock"},
{fn_ofd_r, fn_posix_w, fn_dummy, "OFD read lock vs POSIX write lock"},
{fn_ofd_w, fn_posix_r, fn_dummy, "OFD write lock vs POSIX read lock"},
{fn_ofd_w, fn_ofd_w, fn_dummy, "OFD write lock vs OFD write lock"},
{fn_ofd_r, fn_ofd_w, fn_posix_w, "OFD r/w lock vs POSIX write lock"},
{fn_ofd_r, fn_ofd_w, fn_posix_r, "OFD r/w lock vs POSIX read lock"},
};
建立以下线程
fn_ofd_r p00
fn_ofd_w p10
fn_dummy p20
fn_ofd_r p01
fn_ofd_w p11
fn_dummy p21
fn_ofd_r p02
fn_ofd_w p12
fn_dummy p22
fn_ofd_r p03
fn_ofd_w p13
fn_dummy p23
/* OFD read lock reading data*/
static void *fn_ofd_r(void *arg)
{
struct param *pa = arg;
//pa P0[0]={0,4096,2} offset length cnt
unsigned char buf[pa->length];//4096
int i;
int fd = SAFE_OPEN(fname, O_RDWR);
struct flock64 lck = {
.l_whence = SEEK_SET,
.l_start = pa->offset,
.l_len = pa->length,
.l_pid = 0,
};
while (loop_flag) {
memset(buf, 0, pa->length);
lck.l_type = F_RDLCK;
SAFE_FCNTL(fd, F_OFD_SETLKW, &lck);
/* rlock acquired */
SAFE_LSEEK(fd, pa->offset, SEEK_SET);
SAFE_READ(1, fd, buf, pa->length);
/* Verifying data read */
for (i = 0; i < pa->length; i++) {
if (buf[i] < 1 || buf[i] > 254) {
tst_res(TFAIL, "Unexpected data "
"offset %ld value %d",
pa->offset + i, buf[i]);
fail_flag = 1;
break;
}
int j = (i / (pa->length/4)) * pa->length/4;
if (buf[i] != buf[j]) {
tst_res(TFAIL, "Unexpected data "
"offset %ld value %d",
pa->offset + i, buf[i]);
fail_flag = 1;
break;
}
}
lck.l_type = F_UNLCK;
SAFE_FCNTL(fd, F_OFD_SETLK, &lck);
sched_yield();
}
pthread_barrier_wait(&barrier);
SAFE_CLOSE(fd);
return NULL;
}
标签:char pen pid read type stat creat yield while
原文地址:https://www.cnblogs.com/idyllcheung/p/9884890.html