问题描述
我只是玩一些GCD功能来写入和读取数据到文件。其中两个函数是 dispatch_write()和 dispatch_read(),它允许将数据写入和读取到文件描述符,而不必设置新的 dispatch_io_t 通道。所以,我有以下代码:#import< dispatch / dispatch.h>
#import< stdio.h>
#import< unistd.h>
int main(){
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
int intbuffer [] = {1,2,3,4};
dispatch_data_t data = dispatch_data_create(intbuffer,4 * sizeof(int),queue,NULL);
//写入
dispatch_fd_t fd = open(data.dat,O_RDWR);
printf(FD:%d \\\
,fd);
$ b dispatch_write(fd,data,queue,^(dispatch_data_t d,int e){
printf(Written%zu bytes!\\\
,dispatch_data_get_size(d));
printf(\tError:%d \\\
,e);
});
close(fd);
//读取
fd = open(data.dat,O_RDWR);
dispatch_read(fd,4 * sizeof(int),queue,^(dispatch_data_t d,int e){
printf(Read%zu bytes!\\\
,dispatch_data_get_size(d ));
printf(\tError:%d\\\
,e);
});
close(fd);
//退出确认
getchar();
返回0;
$ / code>
我试图将4个整数数组写入文件之后再读回来。我之前用 touch 命令创建了 data.dat ,任何人都可以完全访问它( sudo chmod 777 data.dat )。
当我执行这段代码时,好像是 data.dat 打开成功,因为程序输出 FD:3 ,这是一个有效的文件描述符,但是 dispatch_write 不写任何东西,因为我得到:
写入0个字节!
错误:9
读取0个字节!
错误:9
错误9是 EBADF 错误,但是,3是一个有效的文件描述符。
那么,我做错了什么?
dispatch_read 和 dispatch_write 电话 - 这是他们的整个观点。换句话说,就是你在这里设置的方式,在调用 dispatch_write 之后立即执行 close 文件描述符。在GCD执行写入后台线程时,文件描述符已经关闭。读取操作相同。您必须等到写操作完成后再关闭文件。
我重新编写了一段代码,使用 dispatch_semaphore
code>等待写入和读取完成后再关闭文件:int main(){
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
int intbuffer [] = {1,2,3,4};
dispatch_data_t data = dispatch_data_create(intbuffer,4 * sizeof(int),queue,NULL);
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
//写入
dispatch_fd_t fd = open(/ tmp / data.dat,O_RDWR | O_CREAT | O_TRUNC,S_IRWXU | S_IRWXG | S_IRWXO);
printf(FD:%d \\\
,fd);
$ b $ dispatch_write(fd,data,queue,^(dispatch_data_t d,int e){
printf(Written%zu bytes!\\\
,dispatch_data_get_size(data) - (d? dispatch_data_get_size(d):0));
printf(\tError:%d\\\
,e);
dispatch_semaphore_signal(sem);
});
dispatch_semaphore_wait(sem,DISPATCH_TIME_FOREVER);
close(fd);
//读取
fd = open(/ tmp / data.dat,O_RDWR);
dispatch_read(fd,4 * sizeof(int),queue,^(dispatch_data_t d,int e){
printf(Read%zu bytes!\\\
,dispatch_data_get_size(d ));
printf(\tError:%d\\\
,e);
dispatch_semaphore_signal(sem);
});
dispatch_semaphore_wait(sem,DISPATCH_TIME_FOREVER);
close(fd);
//退出确认
getchar();
返回0;
}
I'm just playing with some GCD functions for writing and reading data to files. Two of these functions are dispatch_write() and dispatch_read(), which allow one to write and read data to a file descriptor without having to setup a new dispatch_io_t channel.
So, I have the following code:
#import <dispatch/dispatch.h> #import <stdio.h> #import <unistd.h> int main() { dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); int intbuffer[] = { 1, 2, 3, 4 }; dispatch_data_t data = dispatch_data_create(intbuffer, 4 * sizeof(int), queue, NULL); // Write dispatch_fd_t fd = open("data.dat", O_RDWR); printf("FD: %d\n", fd); dispatch_write(fd, data, queue,^(dispatch_data_t d, int e) { printf("Written %zu bytes!\n", dispatch_data_get_size(d)); printf("\tError: %d\n", e); }); close(fd); // Read fd = open("data.dat", O_RDWR); dispatch_read(fd, 4 * sizeof(int), queue, ^(dispatch_data_t d, int e) { printf("Read %zu bytes!\n", dispatch_data_get_size(d)); printf("\tError: %d\n", e); }); close(fd); // Exit confirmation getchar(); return 0; }
with which I'm attempting to write a 4-integer array to a file and, after that, to read it back. I previously created data.dat with the touch command and anyone has full access to it (sudo chmod 777 data.dat).
When I execute this code, it seems data.dat gets open successfully, since the program prints out FD: 3, which is a valid file descriptor, but dispatch_write doesn't write anything to it, since I get:
Written 0 bytes! Error: 9 Read 0 bytes! Error: 9
Error 9 is the code for a EBADF error, but, again, 3 is a valid file descriptor.
So, what am I doing wrong?
dispatch_read and dispatch_write are not synchronous calls -- that's their whole point. In other words, the way you have it set up here, you close the file descriptor right after calling dispatch_write. By the time GCD goes to perform the write on a background thread, the file descriptor is already closed. Same for the read operation. You have to wait until the write operation is finished before you close the file.
I reworked your code a little bit to use a dispatch_semaphore to wait for the writes and reads to complete before closing the file:
int main() { dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); int intbuffer[] = { 1, 2, 3, 4 }; dispatch_data_t data = dispatch_data_create(intbuffer, 4 * sizeof(int), queue, NULL); dispatch_semaphore_t sem = dispatch_semaphore_create(0); // Write dispatch_fd_t fd = open("/tmp/data.dat", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO); printf("FD: %d\n", fd); dispatch_write(fd, data, queue,^(dispatch_data_t d, int e) { printf("Written %zu bytes!\n", dispatch_data_get_size(data) - (d ? dispatch_data_get_size(d) : 0)); printf("\tError: %d\n", e); dispatch_semaphore_signal(sem); }); dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); close(fd); // Read fd = open("/tmp/data.dat", O_RDWR); dispatch_read(fd, 4 * sizeof(int), queue, ^(dispatch_data_t d, int e) { printf("Read %zu bytes!\n", dispatch_data_get_size(d)); printf("\tError: %d\n", e); dispatch_semaphore_signal(sem); }); dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); close(fd); // Exit confirmation getchar(); return 0; }
这篇关于dispatch_write()和dispatch_read()用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!