加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
file_test.c 1.41 KB
一键复制 编辑 原始数据 按行查看 历史
linuxmail 提交于 2020-11-04 23:43 . resolv
/*
* ================================
* eli960@qq.com
* http://linxumail.cn/
* 2018-07-10
* ================================
*/
#include "coroutine.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <fcntl.h>
static int co_count = 0;
static void *file_do(void *arg)
{
char *fn = "a.txt";
int fd = open(fn, O_RDWR|O_CREAT|O_TRUNC, 0777);
if (fd == -1) {
fprintf(stderr, "can not open %s(%m)\n", fn);
return 0;
}
for (int i=0;i < 100; i++) {
if (write(fd, "0123456789", 8) != 8) {
fprintf(stderr, "write error\n");
}
if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
fprintf(stderr, "lseek error (%m)");
}
sleep(1);
}
close(fd);
co_count--;
if (co_count == 0) {
fprintf(stderr, "zcoroutine_base_stop_notify\n");
zcoroutine_base_stop_notify(0);
}
return 0;
}
int main(int argc, char **argv)
{
int i;
zcoroutine_base_init();
zvar_coroutine_block_pthread_count_limit = 3;
zvar_coroutine_fileio_use_block_pthread = 1;
co_count = 10;
for (i=0;i<co_count;i++) {
zcoroutine_go(file_do, 0, 0);
}
printf("exit after 100s\n");
printf("file io running in worker pthread\n");
printf("strace -p pthrad_id\n");
zcoroutine_base_run(0);
zcoroutine_base_fini();
sleep(1);
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化