Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <aio.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <signal.h>
- #include <string.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <signal.h>
- #include <stdint.h>
- #include <inttypes.h>
- int in_file, out_file, file_size, cluster_size, m = 1, n = 1, n_of_clusters, n_of_blocks, cur_block, write_counter;
- struct aio_operation {
- struct aiocb aio;
- char *buffer;
- int write_operation;
- void* next_operation;
- int block_no;
- };
- void aio_completion_handler(sigval_t sigval)
- {
- struct aio_operation *aio_op = (struct aio_operation *)sigval.sival_ptr;
- if (aio_op->write_operation)
- {
- printf("Successfully wrote block %i out of %i\n", aio_op->block_no, n_of_blocks);
- //free((char*)aio_op->buffer);
- if (cur_block <= n_of_blocks)
- {
- struct aiocb s;
- struct aio_operation op;
- char *charbuf = (char*)malloc(m * cluster_size * sizeof(char));
- s.aio_fildes = in_file;
- s.aio_offset = cur_block * cluster_size * m;
- s.aio_buf = charbuf;
- s.aio_nbytes = m * cluster_size;
- s.aio_reqprio = 0;
- s.aio_sigevent.sigev_notify = SIGEV_THREAD;
- s.aio_sigevent.sigev_signo = SIGRTMIN;
- s.aio_sigevent.sigev_notify_function = aio_completion_handler;
- s.aio_sigevent.sigev_notify_attributes = NULL;
- s.aio_sigevent.sigev_value.sival_ptr = &op;
- op.aio = s;
- op.buffer = charbuf;
- op.next_operation = NULL;
- op.write_operation = 0;
- op.block_no = cur_block;
- cur_block++;
- aio_read(&s);
- }
- else
- {
- if (write_counter == n_of_blocks)
- {
- close(in_file);
- close(out_file);
- printf("Copying successful\n");
- }
- }
- }
- else
- {
- printf("Successfully read block %i out of %i\n", aio_op->block_no, n_of_blocks);
- struct aiocb s;
- struct aio_operation op;
- s.aio_fildes = out_file;
- s.aio_offset = aio_op->aio.aio_offset;
- s.aio_buf = aio_op->aio.aio_buf;
- s.aio_nbytes = aio_op->aio.aio_nbytes;
- s.aio_reqprio = 0;
- s.aio_sigevent.sigev_notify = SIGEV_THREAD;
- s.aio_sigevent.sigev_signo = SIGRTMIN;
- s.aio_sigevent.sigev_notify_function = aio_completion_handler;
- s.aio_sigevent.sigev_notify_attributes = NULL;
- s.aio_sigevent.sigev_value.sival_ptr = &op;
- op.aio = s;
- op.buffer = (char*) aio_op->aio.aio_buf;
- op.next_operation = NULL;
- op.write_operation = 1;
- op.block_no = aio_op->block_no;
- aio_write(&s);
- write_counter++;
- }
- }
- int main()
- {
- struct stat buf;
- in_file = open("test.txt", O_RDONLY | O_NONBLOCK, 0666);
- if (in_file)
- {
- if (!fstat(in_file, &buf))
- {
- file_size = buf.st_size;
- cluster_size = buf.st_blksize;
- //cluster_size = 1;
- n_of_clusters = file_size / cluster_size;
- if (file_size % cluster_size)
- n_of_clusters++;
- n_of_blocks = n_of_clusters / m;
- if (n_of_clusters % m)
- n_of_blocks++;
- printf("Size is %i, block size is %i, n of clusters is %i, n of blocks is %i\n", file_size, cluster_size, n_of_clusters, n_of_blocks);
- out_file = open("test_copy.txt", O_CREAT | O_WRONLY | O_TRUNC | O_NONBLOCK, 0666);
- if (out_file)
- {
- cur_block = 0;
- write_counter = 0;
- for (int i = 0; i < n && cur_block != n_of_blocks; i++)
- {
- struct aiocb s;
- struct aio_operation op;
- char *charbuf = (char*)malloc(m * cluster_size * sizeof(char));
- s.aio_fildes = in_file;
- s.aio_offset = cur_block * cluster_size * m;
- s.aio_buf = charbuf;
- s.aio_nbytes = m * cluster_size;
- s.aio_reqprio = 0;
- s.aio_sigevent.sigev_notify = SIGEV_THREAD;
- s.aio_sigevent.sigev_signo = SIGRTMIN;
- s.aio_sigevent.sigev_notify_function = aio_completion_handler;
- s.aio_sigevent.sigev_notify_attributes = NULL;
- s.aio_sigevent.sigev_value.sival_ptr = &op;
- op.aio = s;
- op.buffer = charbuf;
- op.next_operation = NULL;
- op.write_operation = 0;
- op.block_no = cur_block;
- cur_block++;
- aio_read(&s);
- }
- }
- }
- }
- getchar();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement