Dispatch_write () and dispatch_read ()

I just play with some GCD functions to write and read data to files. Two of these functions are dispatch_write() and dispatch_read() , which allow you to write and read data to a file descriptor without having to set 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 the help of which I try to write a 4-integer array to a file, and then read it. I previously created data.dat using the touch command, and who has full access to it ( sudo chmod 777 data.dat ).

When I execute this code, it seems that data.dat opens successfully, since the program outputs FD: 3 , which is a valid file descriptor, but dispatch_write does not write anything since I get:

 Written 0 bytes! Error: 9 Read 0 bytes! Error: 9 

Error 9 is the EBADF error code, but again, 3 is a valid file descriptor.

So what am I doing wrong?

+4
source share
1 answer

dispatch_read and dispatch_write are not synchronous calls - all of them are points. In other words, the way you created it, you close the file descriptor immediately after calling dispatch_write . By the time the GCD is sent to record in the background thread, the file descriptor is already closed. The same goes for the read operation. You need to wait until the write operation completes before closing the file.

I reworked your code a bit to use dispatch_semaphore to wait for the write and read 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; } 
+7
source

Source: https://habr.com/ru/post/1494302/


All Articles