Skip to content

Commit e5ea92c

Browse files
committed
用于读取read文件和创建线程进行写入read的函数
1 parent 862450e commit e5ea92c

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

read_io.cpp

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//
2+
// Created by 梁俊鹏 on 2019-07-20.
3+
//
4+
5+
#include <iostream>
6+
#include <stdio.h>
7+
#include <string.h>
8+
#include "read_io.h"
9+
#include "entity/read.h"
10+
11+
#define BUFFER_SIZE 152
12+
13+
using namespace std;
14+
15+
//block_queue<string> *ioqueue = new block_queue<string>;
16+
17+
static int thisRank;
18+
static int host_num;
19+
static string path;
20+
static string *fnames;
21+
static int fileAmount = 0;
22+
static int curIndex4Fnames;
23+
static fpos_t curSeek;
24+
static char buffer[BUFFER_SIZE];
25+
static FILE *infile;
26+
27+
int moveToNext() {
28+
fclose(infile);
29+
curSeek = 0ll;
30+
if (curIndex4Fnames + 1 >= fileAmount) {
31+
cout << "All files are read." << endl;
32+
return 0;
33+
}
34+
string fname = path + fnames[++curIndex4Fnames];
35+
infile = fopen(fname.c_str(), "r");
36+
if (!infile) {
37+
cerr << "Unable to open file: " << fname << endl;
38+
return -1;
39+
}
40+
return 1;
41+
}
42+
43+
int readIOInit(int currank, int world_size, string filepath, string *filenames, int fileNum) {
44+
thisRank = currank;
45+
host_num = world_size;
46+
path = filepath;
47+
fnames = filenames;
48+
fileAmount = fileNum;
49+
curIndex4Fnames = -1; // 因为moveToNext需要直接增加
50+
curSeek = 0ll;
51+
if (nullptr == filenames) {
52+
cerr << "No input read file." << endl;
53+
return -1;
54+
}
55+
while (moveToNext() == -1);
56+
if (curIndex4Fnames + 1 >= fileAmount) return -1;
57+
return 0;
58+
}
59+
60+
int getNextRead(string *outread, size_t *readpos) {
61+
if (fileAmount < 1) return -1;
62+
// 先尝试读取,如果不能读取则转向下一个文件
63+
while (fgets(buffer, BUFFER_SIZE, infile) == NULL) {
64+
int flag;
65+
while ((flag = moveToNext()) == -1) ; // 当读到的文件不能打开则马上打开下一个
66+
if (flag == 0) return -1; // 全部读完则返回-1
67+
}
68+
*readpos = curSeek; // 读取前的位置
69+
fgetpos(infile, &curSeek);
70+
71+
int len = strlen(buffer) - 1;
72+
*outread = string(buffer, len);
73+
74+
return 1;
75+
}
76+
77+
string getCurrentFilename() {
78+
return fnames[curIndex4Fnames];
79+
}
80+
81+
pthread_t requestWriteRead(const string &read) {
82+
pthread_t tid;
83+
if (pthread_create(&tid, NULL, writeReadRunner, (void *)&read) == -1) {
84+
cerr << "Error: Cannot create thread when requesting read writing." << endl;
85+
return 0;
86+
}
87+
pthread_detach(tid);
88+
return tid;
89+
}
90+
91+
void *writeReadRunner(void *arg) {
92+
string read = *((string *) arg);
93+
if (createRead(read, nullptr) == 0) {
94+
cerr << "Error: Cannot create file for read \"" << read << "\"" << endl;
95+
return (void *) -1;
96+
}
97+
return (void *)0;
98+
}

read_io.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// Created by 梁俊鹏 on 2019-07-20.
3+
//
4+
5+
#ifndef RANDOMSTRINGASSEMBLY_READ_IO_H
6+
#define RANDOMSTRINGASSEMBLY_READ_IO_H
7+
8+
#include <pthread.h>
9+
#include <string>
10+
11+
using namespace std;
12+
13+
int readIOInit(int currank, int world_size, string filepath, string *filenames, int fileNum);
14+
15+
int getNextRead(string *outread, size_t *readpos);
16+
17+
string getCurrentFilename();
18+
19+
pthread_t requestWriteRead(const string &read);
20+
21+
void *writeReadRunner(void *arg);
22+
23+
#endif //RANDOMSTRINGASSEMBLY_READ_IO_H

0 commit comments

Comments
 (0)