2 //=============================================================================
6 * Checkout $ACE_ROOT/examples/Reactor/Proactor/test_aiocb_ace.cpp,
7 * which is the ACE'ified version of this program.
10 * % CC -g -o test_aiocb -lrt test_aiocb.cpp
13 * @author Alexander Babu Arulanthu <alex@cs.wustl.edu>
15 //=============================================================================
18 //FUZZ: disable check_for_lack_ACE_OS
19 //FUZZ: disable check_for_improper_main_declaration
23 #include <sys/types.h>
36 /// Default constructor.
39 /// Initting the output file and the buffer.
42 /// Doing the testing stuff.
48 /// Output file descriptor.
51 /// For writing to the file.
52 struct aiocb
*aiocb_write_
;
54 /// Reading stuff from the file.
55 struct aiocb
*aiocb_read_
;
57 /// The buffer to be written to the out_fd.
60 /// The buffer to be read back from the file.
65 : aiocb_write_ (new struct aiocb
),
66 aiocb_read_ (new struct aiocb
),
72 Test_Aio::~Test_Aio ()
77 delete [] buffer_read_
;
80 // Init the output file and init the buffer.
84 // Open the output file.
85 this->out_fd_
= open ("test_aio.log", O_RDWR
| O_CREAT
| O_TRUNC
, 0666);
86 if (this->out_fd_
== 0)
88 cout
<< "Error : Opening file" << endl
;
93 this->buffer_write_
= strdup ("Welcome to the world of AIO... AIO Rules !!!");
94 cout
<< "The buffer : " << this->buffer_write_
<< endl
;
95 this->buffer_read_
= new char [strlen (this->buffer_write_
) + 1];
99 // Set the necessary things for the AIO stuff.
100 // Write the buffer asynchly.hmm Disable signals.
101 // Go on aio_suspend. Wait for completion.
102 // Print out the result.
106 // = Write to the file.
109 this->aiocb_write_
->aio_fildes
= this->out_fd_
;
110 this->aiocb_write_
->aio_offset
= 0;
111 this->aiocb_write_
->aio_buf
= this->buffer_write_
;
112 this->aiocb_write_
->aio_nbytes
= strlen (this->buffer_write_
);
113 this->aiocb_write_
->aio_reqprio
= 0;
114 this->aiocb_write_
->aio_sigevent
.sigev_notify
= SIGEV_NONE
;
115 //this->this->aiocb_.aio_sigevent.sigev_signo = SIGRTMAX;
116 this->aiocb_write_
->aio_sigevent
.sigev_value
.sival_ptr
=
117 (void *) this->aiocb_write_
;
119 // Fire off the aio write.
120 if (aio_write (this->aiocb_write_
) != 0)
122 perror ("aio_write");
126 // = Read from that file.
129 this->aiocb_read_
->aio_fildes
= this->out_fd_
;
130 this->aiocb_read_
->aio_offset
= 0;
131 this->aiocb_read_
->aio_buf
= this->buffer_read_
;
132 this->aiocb_read_
->aio_nbytes
= strlen (this->buffer_write_
);
133 this->aiocb_read_
->aio_reqprio
= 0;
134 this->aiocb_read_
->aio_sigevent
.sigev_notify
= SIGEV_NONE
;
135 //this->this->aiocb_.aio_sigevent.sigev_signo = SIGRTMAX;
136 this->aiocb_read_
->aio_sigevent
.sigev_value
.sival_ptr
=
137 (void *) this->aiocb_read_
;
139 // Fire off the aio write. If it doesnt get queued, carry on to get
140 // the completion for the first one.
141 if (aio_read (this->aiocb_read_
) < 0)
144 // Wait for the completion on aio_suspend.
145 struct aiocb
*list_aiocb
[2];
146 list_aiocb
[0] = this->aiocb_write_
;
147 list_aiocb
[1] = this->aiocb_read_
;
149 // Do suspend till all the aiocbs in the list are done.
154 return_val
= aio_suspend (list_aiocb
,
157 cerr
<< "Return value :" << return_val
<< endl
;
159 // Analyze return and error values.
160 if (list_aiocb
[0] != 0)
162 if (aio_error (list_aiocb
[0]) != EINPROGRESS
)
164 if (aio_return (list_aiocb
[0]) == -1)
166 perror ("aio_return");
171 // Successful. Store the pointer somewhere and make the
172 // entry NULL in the list.
173 this->aiocb_write_
= list_aiocb
[0];
178 cout
<< "AIO write in progress" << endl
;
181 if (list_aiocb
[1] != 0)
183 if (aio_error (list_aiocb
[1]) != EINPROGRESS
)
185 int read_return
= aio_return (list_aiocb
[1]);
186 if (read_return
== -1)
188 perror ("aio_return");
193 // Successful. Store the pointer somewhere and make the
194 // entry NULL in the list.
195 this->aiocb_read_
= list_aiocb
[1];
197 this->buffer_read_
[read_return
] = '\0';
201 cout
<< "AIO read in progress" << endl
;
205 if ((list_aiocb
[0] == 0) && (list_aiocb
[1] == 0))
209 cout
<< "Both the AIO operations done." << endl
;
210 cout
<< "The buffer is :" << this->buffer_read_
<< endl
;
216 main (int argc
, char **argv
)
220 if (test_aio
.init () != 0)
222 printf ("AIOCB test failed:\n"
223 "ACE_POSIX_AIOCB_PROACTOR may not work in this platform\n");
227 if (test_aio
.do_aio () != 0)
229 printf ("AIOCB test failed:\n"
230 "ACE_POSIX_AIOCB_PROACTOR may not work in this platform\n");
233 printf ("AIOCB test successful:\n"
234 "ACE_POSIX_AIOCB_PROACTOR should work in this platform\n");