我在c++中创建了一个非常简单的函数,它使用aio-write。在参数中,我得到了创建文件的路径及其大小。要创建新文件,我使用int open(const char *pathname, int flags, mode_t mode)。在
然后我使用:g++ -Wall -g -Werror aio_calls.cpp -shared -o aio_calls.so -fPIC -lrt将其编译为共享对象。在
在Python2.7.5上,一切都很完美,但在Python3.4上,我只得到路径的第一个字符。有什么线索可以让它工作,这样它就可以走完这条路了吗?在
以下是功能代码:#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "aio_calls.h"
#define DLLEXPORT extern "C"
using namespace std;
DLLEXPORT int awrite(const char *path, int size)
{
// create the file
cout << path << endl;
int file = open(path, O_WRONLY | O_CREAT, 0644);
if (file == -1)
return errno;
// create the buffer
char* buffer = new char[size];
// create the control block structure
aiocb cb;
memset(buffer, 'a', size);
memset(&cb, 0, sizeof(aiocb));
cb.aio_nbytes = size;
cb.aio_fildes = file;
cb.aio_offset = 0;
cb.aio_buf = buffer;
// write!
if (aio_write(&cb) == -1)
{
close(file);
return errno;
}
// wait until the request has finished
while(aio_error(&cb) == EINPROGRESS);
// return final status for aio request
int ret = aio_return(&cb);
if (ret == -1)
return errno;
// now clean up
delete[] buffer;
close(file);
return 0;
}
如你所见,我在函数的开头写了cout。以下是python 2上发生的情况:
^{pr2}$
Python3上的情况如下:Python 3.4.5 (default, May 29 2017, 15:17:55)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import cdll
>>> m=cdll.LoadLibrary('/home/administrator/Documents/aio_calls.so')
>>> m.awrite('aa.txt', 40)
a
0