Changeset 18
- Timestamp:
- 09/06/07 13:04:36
(1 year ago)
- Author:
- tmiller
- Message:
Adapt tests and examples to the new API
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r9 |
r18 |
|
| 20 | 20 | OBJS=mq_creator mq_destroyer mq_sender mq_reader mq_bin_sender mq_bin_reader shm_creator shm_destroyer shm_sender shm_reader |
|---|
| 21 | 21 | LDFLAGS=-L../src -lsipc -Wl,-rpath,$(SHLIBDIR) |
|---|
| | 22 | CFLAGS+=-g3 -I../include |
|---|
| 22 | 23 | SUBDIRS=policy |
|---|
| 23 | 24 | |
|---|
| r4 |
r18 |
|
| 3 | 3 | Creating an IPC Resource |
|---|
| 4 | 4 | ======================== |
|---|
| 5 | | Libsipc was designed with the intention of minimizing backchannels between reader and |
|---|
| 6 | | writer processes. Therefore, creation and destruction of IPC resources must be controlled |
|---|
| 7 | | by applications outside of the communicating processes. |
|---|
| 8 | | The shm_creator and mq_creator targets in this directory are examples of such helper applications; they simply call sipc_create(3). |
|---|
| 9 | | Likewise, the shm_destroyer and mq_destroyer targets in this directory are also helper applications; they call sipc_destroy_resource(3). |
|---|
| | 5 | Libsipc was designed with the intention of minimizing backchannels |
|---|
| | 6 | between reader and writer processes. Therefore, creation and destruction |
|---|
| | 7 | of IPC resources must be controlled by applications outside of the |
|---|
| | 8 | communicating processes. The shm_creator and mq_creator targets in this |
|---|
| | 9 | directory are examples of such helper applications; they simply call |
|---|
| | 10 | sipc_open(3) with the SIPC_CREATOR flag. Likewise, the shm_destroyer |
|---|
| | 11 | and mq_destroyer targets in this directory are also helper applications; |
|---|
| | 12 | they call sipc_unlink(3). |
|---|
| 10 | 13 | |
|---|
| 11 | 14 | Connecting |
|---|
| 12 | 15 | ========== |
|---|
| 13 | | Connecting a process to an IPC resource created through libsipc involves calling sipc_init(3). |
|---|
| 14 | | This function will return an IPC handle, which contains an internal buffer and state information relevant |
|---|
| 15 | | to the communications process. A call to sipc_init(3) will allocate the IPC handle's data member. |
|---|
| 16 | | After the handle has been initialized, a call to calling sipc_connect(3) will actually connect the handle to the IPC resource: |
|---|
| | 16 | Connecting a process to an IPC resource created through libsipc involves |
|---|
| | 17 | calling sipc_open(3) with the SIPC_SENDER or SIPC_RECEIVER flag. This |
|---|
| | 18 | function will return an IPC handle, which contains an internal buffer |
|---|
| | 19 | and state information relevant to the communications process. When the |
|---|
| | 20 | SIPC_SENDER or SIPC_RECEIVER flag is specified, a call to sipc_open(3) |
|---|
| | 21 | will allocate the IPC handle's data member and connect the handle to |
|---|
| | 22 | the IPC resource: |
|---|
| 17 | 23 | |
|---|
| 18 | | ipc = sipc_init("ipc_key", size, ipc_type); |
|---|
| 19 | | sipc_connect(ipc); |
|---|
| | 24 | ipc = sipc_open("ipc_key", role, ipc_type, size); |
|---|
| 20 | 25 | |
|---|
| 21 | | Note the distinction between an IPC handle and an IPC resource: an IPC handle has an underlying IPC mechanism, |
|---|
| 22 | | such as message queues or shared memory, as specified by the ipc_type parameter to sipc_init(3). |
|---|
| 23 | | This underlying mechanism and its associated system resources shall be known as an "IPC resource." Processes communicating |
|---|
| 24 | | using libsipc do not directly access IPC resources, rather they operate entirely on IPC handles. |
|---|
| | 26 | where "role" is either SIPC_SENDER or SIPC_RECEIVER. Note the distinction |
|---|
| | 27 | between an IPC handle and an IPC resource: an IPC handle has an underlying |
|---|
| | 28 | IPC mechanism, such as message queues or shared memory, as specified by |
|---|
| | 29 | the ipc_type parameter to sipc_open(3). This underlying mechanism and its |
|---|
| | 30 | associated system resources shall be known as an "IPC resource." Processes |
|---|
| | 31 | communicating using libsipc do not directly access IPC resources, rather |
|---|
| | 32 | they operate entirely on IPC handles. |
|---|
| 25 | 33 | |
|---|
| 26 | 34 | Sending Data |
|---|
| 27 | 35 | ============ |
|---|
| 28 | | To send data to a connected IPC handle, the data must first be copied into the handle's internal data member. |
|---|
| 29 | | To do this, a pointer to the handle's internal data member can be obtained via sipc_get_data_ptr(3): |
|---|
| | 36 | To send data to a connected IPC handle, the data must first be copied |
|---|
| | 37 | into the handle's internal data member. To do this, a pointer to the |
|---|
| | 38 | handle's internal data member can be obtained via sipc_get_data_ptr(3): |
|---|
| 30 | 39 | |
|---|
| 31 | 40 | char my_data[] = "Hello, world."; |
|---|
| … | … | |
| 36 | 45 | Receiving Data |
|---|
| 37 | 46 | ============== |
|---|
| 38 | | To receive data from a connected IPC handle, you must call sipc_recv_data(3), passing it a buffer |
|---|
| 39 | | for data storage: |
|---|
| | 47 | To receive data from a connected IPC handle, you must call |
|---|
| | 48 | sipc_recv_data(3), passing it a buffer for data storage: |
|---|
| 40 | 49 | |
|---|
| 41 | 50 | char *data = sipc_get_data_ptr(ipc); |
|---|
| … | … | |
| 44 | 53 | sipc_shm_recv_done(ipc); /* Only needed when using shm */ |
|---|
| 45 | 54 | |
|---|
| 46 | | When using shared memory, a call to sipc_shm_recv_done(3) is needed to unblock the sending process, |
|---|
| 47 | | allowing it to send more data. |
|---|
| | 55 | When using shared memory, a call to sipc_shm_recv_done(3) is needed to |
|---|
| | 56 | unblock the sending process, allowing it to send more data. |
|---|
| 48 | 57 | |
|---|
| 49 | 58 | Cleaning Up |
|---|
| 50 | 59 | =========== |
|---|
| 51 | | Disconnecting an IPC handle from an IPC resource can be done with sipc_disconnect(3). |
|---|
| 52 | | To eliminate backchannels between sending and receiving processes, freeing IPC resources acquired |
|---|
| 53 | | during the communications process should be done in a separate process (see "Creating an IPC Resource"). |
|---|
| 54 | | This destroyer process should call sipc_destroy_resource(3) to free IPC resources. Note that |
|---|
| 55 | | the destroyer process should be run only when all communicating processes have disconnected from |
|---|
| 56 | | their respective IPC resource. |
|---|
| | 60 | Closing an IPC handle from an IPC resource can be done with sipc_close(3). |
|---|
| | 61 | To eliminate backchannels between sending and receiving processes, |
|---|
| | 62 | freeing IPC resources acquired during the communications process |
|---|
| | 63 | should be done in a separate process (see "Creating an IPC Resource"). |
|---|
| | 64 | This destroyer process should call sipc_unlink(3) to free IPC resources. |
|---|
| | 65 | Note that the destroyer process should be run only when all communicating |
|---|
| | 66 | processes have closed their respective IPC handles. |
|---|
| 57 | 67 | |
|---|
| 58 | 68 | Handling Errors |
|---|
| 59 | 69 | =============== |
|---|
| 60 | | When using libsipc, various errors may occur in the communication process. |
|---|
| 61 | | The sipc_error(3) function has been provided to handle the displaying of error messages |
|---|
| 62 | | when such conditions arise: |
|---|
| | 70 | When using libsipc, various errors may occur in the communication process. |
|---|
| | 71 | The sipc_error(3) function has been provided to handle the displaying |
|---|
| | 72 | of error messages when such conditions arise: |
|---|
| 63 | 73 | |
|---|
| 64 | 74 | if (sipc_send_data(ipc) < 0) |
|---|
| 65 | 75 | sipc_error(ipc, "Error sending data\n"); |
|---|
| 66 | | |
|---|
| 67 | | |
|---|
| r9 |
r18 |
|
| 56 | 56 | } |
|---|
| 57 | 57 | |
|---|
| 58 | | /* Initialize the IPC handle */ |
|---|
| 59 | | sipc_t *ipc = sipc_init(SIPC_KEY, SIPC_SYSV_MQUEUES, DATA_LEN, 0); |
|---|
| | 58 | /* Create a new IPC handle for receiving */ |
|---|
| | 59 | sipc_t *ipc = sipc_open(SIPC_KEY, SIPC_RECEIVER, SIPC_SYSV_MQUEUES, DATA_LEN); |
|---|
| 60 | 60 | if (!ipc) { |
|---|
| 61 | 61 | fprintf(stderr, "Error: unable to create IPC resource\n"); |
|---|
| 62 | 62 | return 1; |
|---|
| 63 | 63 | } |
|---|
| 64 | | |
|---|
| 65 | | /* Connect to the IPC resource */ |
|---|
| 66 | | if (sipc_connect(ipc)) { |
|---|
| 67 | | fprintf(stderr, "Error: unable to connect IPC resource\n"); |
|---|
| 68 | | return 1; |
|---|
| 69 | | } |
|---|
| 70 | | |
|---|
| 71 | | |
|---|
| 72 | 64 | |
|---|
| 73 | 65 | /* First receive the file */ |
|---|
| … | … | |
| 84 | 76 | |
|---|
| 85 | 77 | /* Cleanup */ |
|---|
| 86 | | sipc_disconnect(ipc); |
|---|
| 87 | | sipc_destroy_handle(ipc); |
|---|
| | 78 | sipc_close(ipc); |
|---|
| 88 | 79 | return 0; |
|---|
| 89 | 80 | } |
|---|
| r9 |
r18 |
|
| 64 | 64 | |
|---|
| 65 | 65 | /* Initialize and connect IPC handle */ |
|---|
| 66 | | ipc = sipc_init(SIPC_KEY, SIPC_SYSV_MQUEUES, IPC_LEN, 1); |
|---|
| | 66 | ipc = sipc_open(SIPC_KEY, SIPC_SENDER, SIPC_SYSV_MQUEUES, IPC_LEN); |
|---|
| 67 | 67 | if (!ipc) { |
|---|
| 68 | 68 | fprintf(stderr, "Unable to create IPC resource\n"); |
|---|
| 69 | 69 | goto out; |
|---|
| 70 | 70 | } |
|---|
| 71 | | if (sipc_connect(ipc)) { |
|---|
| 72 | | fprintf(stderr, "Unable to connect IPC resource\n"); |
|---|
| 73 | | goto out; |
|---|
| 74 | | } |
|---|
| 75 | 71 | |
|---|
| 76 | 72 | /* Get pointer to the handle's internal buffer */ |
|---|
| … | … | |
| 120 | 116 | out: |
|---|
| 121 | 117 | /* Cleanup */ |
|---|
| 122 | | sipc_destroy_handle(ipc); |
|---|
| | 118 | sipc_close(ipc); |
|---|
| 123 | 119 | return retv; |
|---|
| 124 | 120 | } |
|---|
| r4 |
r18 |
|
| 33 | 33 | int main() |
|---|
| 34 | 34 | { |
|---|
| 35 | | if (sipc_create(SIPC_KEY, SIPC_SYSV_MQUEUES) < 0) { |
|---|
| | 35 | sipc_t *sipc; |
|---|
| | 36 | |
|---|
| | 37 | /* Do not need to specify a max size when creating an mqueue. */ |
|---|
| | 38 | sipc = sipc_open(SIPC_KEY, SIPC_CREATOR, SIPC_SYSV_MQUEUES, 0); |
|---|
| | 39 | if (sipc == NULL) { |
|---|
| 36 | 40 | fprintf(stderr, "Unable to create message queue.\n"); |
|---|
| 37 | 41 | return -1; |
|---|
| 38 | 42 | } |
|---|
| | 43 | sipc_close(sipc); |
|---|
| 39 | 44 | |
|---|
| 40 | 45 | return 0; |
|---|
| r4 |
r18 |
|
| 36 | 36 | int main() |
|---|
| 37 | 37 | { |
|---|
| 38 | | sipc_destroy_resource(SIPC_KEY, SIPC_SYSV_MQUEUES); |
|---|
| | 38 | sipc_unlink(SIPC_KEY, SIPC_SYSV_MQUEUES); |
|---|
| 39 | 39 | return 0; |
|---|
| 40 | 40 | } |
|---|
| r4 |
r18 |
|
| 47 | 47 | |
|---|
| 48 | 48 | /* Initialize the IPC handle */ |
|---|
| 49 | | sipc_t *ipc = sipc_init(SIPC_KEY, SIPC_SYSV_MQUEUES, DATA_LEN, 0); |
|---|
| | 49 | sipc_t *ipc = sipc_open(SIPC_KEY, SIPC_RECEIVER, SIPC_SYSV_MQUEUES, DATA_LEN); |
|---|
| 50 | 50 | if (!ipc) { |
|---|
| 51 | 51 | fprintf(stderr, "Error: unable to create IPC resource\n"); |
|---|
| … | … | |
| 53 | 53 | } |
|---|
| 54 | 54 | |
|---|
| 55 | | /* Connect to the IPC resource */ |
|---|
| 56 | | if (sipc_connect(ipc)) { |
|---|
| 57 | | fprintf(stderr, "Error: unable to connect IPC resource\n"); |
|---|
| 58 | | return 1; |
|---|
| 59 | | } |
|---|
| 60 | | |
|---|
| 61 | 55 | /* Receive data from shared memory until the end of transmission |
|---|
| 62 | 56 | * marker has been received. */ |
|---|
| … | … | |
| 71 | 65 | |
|---|
| 72 | 66 | /* Cleanup */ |
|---|
| 73 | | sipc_disconnect(ipc); |
|---|
| 74 | | sipc_destroy_handle(ipc); |
|---|
| | 67 | sipc_close(ipc); |
|---|
| 75 | 68 | return 0; |
|---|
| 76 | 69 | } |
|---|
| r4 |
r18 |
|
| 58 | 58 | |
|---|
| 59 | 59 | /* Initialize and connect IPC handle */ |
|---|
| 60 | | ipc = sipc_init(SIPC_KEY, SIPC_SYSV_MQUEUES, IPC_LEN, 1); |
|---|
| | 60 | ipc = sipc_open(SIPC_KEY, SIPC_SENDER, SIPC_SYSV_MQUEUES, IPC_LEN); |
|---|
| 61 | 61 | if (!ipc) { |
|---|
| 62 | 62 | fprintf(stderr, "Unable to create IPC resource\n"); |
|---|
| 63 | 63 | goto out; |
|---|
| 64 | 64 | } |
|---|
| 65 | | if (sipc_connect(ipc)) { |
|---|
| 66 | | fprintf(stderr, "Unable to connect IPC resource\n"); |
|---|
| 67 | | goto out; |
|---|
| 68 | | } |
|---|
| 69 | 65 | |
|---|
| 70 | 66 | /* Get pointer to the handle's internal buffer */ |
|---|
| … | … | |
| 100 | 96 | out: |
|---|
| 101 | 97 | /* Cleanup */ |
|---|
| 102 | | sipc_destroy_handle(ipc); |
|---|
| | 98 | sipc_close(ipc); |
|---|
| 103 | 99 | return retv; |
|---|
| 104 | 100 | } |
|---|
| r4 |
r18 |
|
| 33 | 33 | * by both the sender and receiver. The file must already exist */ |
|---|
| 34 | 34 | #define SIPC_KEY "sipc_shm_test" |
|---|
| | 35 | |
|---|
| | 36 | /* Amount of data to allocate inside the IPC handle */ |
|---|
| | 37 | #define READ_LEN 4096 |
|---|
| 35 | 38 | |
|---|
| 36 | 39 | int main() |
|---|
| 37 | 40 | { |
|---|
| 38 | | if (sipc_create(SIPC_KEY, SIPC_SYSV_SHM) < 0) { |
|---|
| | 41 | sipc_t *sipc; |
|---|
| | 42 | |
|---|
| | 43 | sipc = sipc_open(SIPC_KEY, SIPC_CREATOR, SIPC_SYSV_SHM, READ_LEN); |
|---|
| | 44 | if (sipc == NULL) { |
|---|
| 39 | 45 | fprintf(stderr, "Unable to create message queue.\n"); |
|---|
| 40 | 46 | return -1; |
|---|
| 41 | 47 | } |
|---|
| | 48 | sipc_close(sipc); |
|---|
| 42 | 49 | |
|---|
| 43 | 50 | return 0; |
|---|
| r4 |
r18 |
|
| 36 | 36 | int main() |
|---|
| 37 | 37 | { |
|---|
| 38 | | sipc_destroy_resource(SIPC_KEY, SIPC_SYSV_SHM); |
|---|
| | 38 | sipc_unlink(SIPC_KEY, SIPC_SYSV_SHM); |
|---|
| 39 | 39 | return 0; |
|---|
| 40 | 40 | } |
|---|
| r4 |
r18 |
|
| 47 | 47 | |
|---|
| 48 | 48 | /* Initialize the IPC handle */ |
|---|
| 49 | | sipc_t *ipc = sipc_init(SIPC_KEY, SIPC_SYSV_SHM, READ_LEN, 0); |
|---|
| | 49 | sipc_t *ipc = sipc_open(SIPC_KEY, SIPC_RECEIVER, SIPC_SYSV_SHM, READ_LEN); |
|---|
| 50 | 50 | if (!ipc) { |
|---|
| 51 | 51 | fprintf(stderr, "Could not create IPC resource\n"); |
|---|
| … | … | |
| 53 | 53 | } |
|---|
| 54 | 54 | |
|---|
| 55 | | /* Connect shm */ |
|---|
| 56 | | if (sipc_connect(ipc) < 0) { |
|---|
| 57 | | sipc_error(ipc, "Unable to connect to IPC resource\n"); |
|---|
| 58 | | return -1; |
|---|
| 59 | | } |
|---|
| 60 | | |
|---|
| 61 | 55 | /* Receive data from shared memory until an end of transmission |
|---|
| 62 | 56 | * marker has been received */ |
|---|
| … | … | |
| 70 | 64 | } |
|---|
| 71 | 65 | |
|---|
| 72 | | sipc_disconnect(ipc); |
|---|
| 73 | | sipc_destroy_handle(ipc); |
|---|
| | 66 | sipc_close(ipc); |
|---|
| 74 | 67 | return 0; |
|---|
| 75 | 68 | } |
|---|
| r4 |
r18 |
|
| 52 | 52 | |
|---|
| 53 | 53 | /* Initialize the IPC handle */ |
|---|
| 54 | | sipc_t *ipc = sipc_init(SIPC_KEY, SIPC_SYSV_SHM, READ_LEN, 1); |
|---|
| | 54 | sipc_t *ipc = sipc_open(SIPC_KEY, SIPC_SENDER, SIPC_SYSV_SHM, READ_LEN); |
|---|
| 55 | 55 | if (!ipc) |
|---|
| 56 | 56 | goto err; |
|---|
| 57 | | |
|---|
| 58 | | /* Connect shm */ |
|---|
| 59 | | if (sipc_connect(ipc) < 0) |
|---|
| 60 | | goto err; |
|---|
| 61 | 57 | |
|---|
| 62 | 58 | /* Get a pointer to data */ |
|---|
| … | … | |
| 88 | 84 | if (ifile) |
|---|
| 89 | 85 | fclose(ifile); |
|---|
| 90 | | sipc_disconnect(ipc); |
|---|
| 91 | | sipc_destroy_handle(ipc); |
|---|
| | 86 | sipc_close(ipc); |
|---|
| 92 | 87 | return 0; |
|---|
| 93 | 88 | } |
|---|
| r4 |
r18 |
|
| 23 | 23 | #include <sipc/sipc.h> |
|---|
| 24 | 24 | |
|---|
| | 25 | #define DATA_LEN 8192 |
|---|
| | 26 | |
|---|
| 25 | 27 | int main(int argc, char *argv[]) |
|---|
| 26 | 28 | { |
|---|
| | 29 | sipc_t *sipc; |
|---|
| | 30 | |
|---|
| 27 | 31 | if (argc != 3) { |
|---|
| 28 | | fprintf(stderr, "Usage: ipc_creator <ipc type> <key>\n"); |
|---|
| | 32 | fprintf(stderr, "Usage: ipc_creator <key> <ipc type>\n"); |
|---|
| 29 | 33 | return -1; |
|---|
| 30 | 34 | } |
|---|
| 31 | 35 | |
|---|
| 32 | | if (sipc_create(argv[1], atoi(argv[2])) < 0) { |
|---|
| 33 | | fprintf(stderr, "Unable to create message queue.\n"); |
|---|
| | 36 | sipc = sipc_open(argv[1], SIPC_CREATOR, atoi(argv[2]), DATA_LEN); |
|---|
| | 37 | if (sipc == NULL) { |
|---|
| | 38 | fprintf(stderr, "Unable to create ipc.\n"); |
|---|
| 34 | 39 | return -1; |
|---|
| 35 | 40 | } |
|---|
| r4 |
r18 |
|
| 30 | 30 | } |
|---|
| 31 | 31 | |
|---|
| 32 | | sipc_destroy_resource(argv[1], atoi(argv[2])); |
|---|
| | 32 | sipc_unlink(argv[1], atoi(argv[2])); |
|---|
| 33 | 33 | return 0; |
|---|
| 34 | 34 | } |
|---|
| r4 |
r18 |
|
| 72 | 72 | wait(&status); |
|---|
| 73 | 73 | |
|---|
| 74 | | /* Initialize IPCs */ |
|---|
| 75 | | if ((writer_ipc = sipc_init(SIPC_MQ_KEY, SIPC_SYSV_MQUEUES, DATA_LEN, 1)) == NULL) { |
|---|
| | 74 | /* Open IPCs */ |
|---|
| | 75 | writer_ipc = |
|---|
| | 76 | sipc_open(SIPC_MQ_KEY, SIPC_SENDER, SIPC_SYSV_MQUEUES, DATA_LEN); |
|---|
| | 77 | if (writer_ipc == NULL) { |
|---|
| 76 | 78 | fprintf(stderr, "Unable to initialize IPC resource\n"); |
|---|
| 77 | 79 | return -1; |
|---|
| 78 | 80 | } |
|---|
| 79 | | if ((reader_ipc = sipc_init(SIPC_MQ_KEY, SIPC_SYSV_MQUEUES, DATA_LEN, 0)) == NULL) { |
|---|
| | 81 | reader_ipc = |
|---|
| | 82 | sipc_open(SIPC_MQ_KEY, SIPC_RECEIVER, SIPC_SYSV_MQUEUES, DATA_LEN); |
|---|
| | 83 | if (reader_ipc == NULL) { |
|---|
| 80 | 84 | fprintf(stderr, "Error initializing IPC resource\n"); |
|---|
| 81 | | return -1; |
|---|
| 82 | | } |
|---|
| 83 | | |
|---|
| 84 | | /* Connect IPCs */ |
|---|
| 85 | | if (sipc_connect(writer_ipc) < 0) { |
|---|
| 86 | | sipc_error(writer_ipc, "Unable to connect IPC resource\n"); |
|---|
| 87 | | return -1; |
|---|
| 88 | | } |
|---|
| 89 | | if (sipc_connect(reader_ipc) < 0) { |
|---|
| 90 | | sipc_error(reader_ipc, "Unable to connect IPC resource\n"); |
|---|
| 91 | 85 | return -1; |
|---|
| 92 | 86 | } |
|---|
| … | … | |
| 127 | 121 | |
|---|
| 128 | 122 | while (!sipc_recv_data(reader_ipc, &data, &len)) { |
|---|
| 129 | | if (is_end_xmit(data)) |
|---|
| | 123 | if (data != NULL && is_end_xmit(data)) |
|---|
| 130 | 124 | break; |
|---|
| 131 | 125 | } |
|---|
| 132 | 126 | |
|---|
| 133 | 127 | /* Cleanup handle here */ |
|---|
| 134 | | sipc_destroy_handle(reader_ipc); |
|---|
| | 128 | sipc_close(reader_ipc); |
|---|
| 135 | 129 | } |
|---|
| 136 | 130 | |
|---|
| … | … | |
| 158 | 152 | |
|---|
| 159 | 153 | /* Send data, then end of xmit */ |
|---|
| 160 | | CU_ASSERT(sipc_send_data(writer_ipc) == 0); |
|---|
| | 154 | CU_ASSERT(sipc_send_data(writer_ipc, DATA_LEN) == 0); |
|---|
| 161 | 155 | CU_ASSERT(send_end_xmit(writer_ipc) == 0); |
|---|
| 162 | 156 | fclose(ifile); |
|---|
| 163 | 157 | |
|---|
| 164 | 158 | /* Cleanup handle here */ |
|---|
| 165 | | sipc_destroy_handle(writer_ipc); |
|---|
| | 159 | sipc_close(writer_ipc); |
|---|
| 166 | 160 | } |
|---|
| 167 | 161 | |
|---|
| … | … | |
| 208 | 202 | bzero(data, DATA_LEN); |
|---|
| 209 | 203 | strncpy(data, XMIT_END, DATA_LEN-1); |
|---|
| 210 | | if (sipc_send_data(ipc) < 0) { |
|---|
| | 204 | if (sipc_send_data(ipc, DATA_LEN) < 0) { |
|---|
| 211 | 205 | sipc_error(ipc, "Unable to send end of transmission marker\n"); |
|---|
| 212 | 206 | return -1; |
|---|
| r4 |
r18 |
|
| 75 | 75 | wait(&status); |
|---|
| 76 | 76 | |
|---|
| 77 | | /* Initialize IPCs */ |
|---|
| 78 | | writer_ipc = sipc_init(SIPC_SHM_KEY, SIPC_SYSV_SHM, DATA_LEN, 1); |
|---|
| 79 | | if (!writer_ipc) { |
|---|
| | 77 | /* Open IPCs */ |
|---|
| | 78 | writer_ipc = |
|---|
| | 79 | sipc_open(SIPC_SHM_KEY, SIPC_SENDER, SIPC_SYSV_SHM, DATA_LEN); |
|---|
| | 80 | if (writer_ipc == NULL) { |
|---|
| 80 | 81 | fprintf(stderr, "Could not initialize IPC resource\n"); |
|---|
| 81 | 82 | return -1; |
|---|
| 82 | 83 | } |
|---|
| 83 | | reader_ipc = sipc_init(SIPC_SHM_KEY, SIPC_SYSV_SHM, DATA_LEN, 0); |
|---|
| 84 | | if (!reader_ipc) { |
|---|
| | 84 | reader_ipc = |
|---|
| | 85 | sipc_open(SIPC_SHM_KEY, SIPC_RECEIVER, SIPC_SYSV_SHM, DATA_LEN); |
|---|
| | 86 | if (reader_ipc == NULL) { |
|---|
| 85 | 87 | fprintf(stderr, "Could not initialize IPC resource\n"); |
|---|
| 86 | 88 | return -1; |
|---|
| 87 | 89 | } |
|---|
| 88 | | |
|---|
| 89 | | /* Connect IPCs */ |
|---|
| 90 | | if (sipc_connect(writer_ipc) < 0) { |
|---|
| 91 | | sipc_error(writer_ipc, "Unable to connect writer IPC\n"); |
|---|
| 92 | | return -1; |
|---|
| 93 | | } |
|---|
| 94 | | if (sipc_connect(reader_ipc) < 0) { |
|---|
| 95 | | sipc_error(writer_ipc, "Unable to connect reader IPC\n"); |
|---|
| 96 | | return -1; |
|---|
| 97 | | } |
|---|
| 98 | 90 | |
|---|
| 99 | 91 | return 0; |
|---|
| … | … | |
| 175 | 167 | if ((rbytes = fread(shm_data, sizeof(char), DATA_LEN-1, ifile)) < 0) { |
|---|
| 176 | 168 | sipc_error(writer_ipc, "fread: %s\n", strerror(errno)); |
|---|
| | 169 | fclose(ifile); |
|---|
| 177 | 170 | return; |
|---|
| 178 | 171 | } |
|---|
| | 172 | fclose(ifile); |
|---|
| 179 | 173 | |
|---|
| 180 | 174 | /* Send the data */ |
|---|
| 181 | | if (sipc_send_data(writer_ipc) < 0) { |
|---|
| | 175 | if (sipc_send_data(writer_ipc, DATA_LEN) < 0) { |
|---|
| 182 | 176 | sipc_error(writer_ipc, "Unable to send data\n"); |
|---|
| 183 | 177 | return; |
|---|
| … | … | |
| 186 | 180 | CU_PASS("Successfully sent data via SYSV shared memory"); |
|---|
| 187 | 181 | |
|---|
| 188 | | sipc_disconnect(writer_ipc); |
|---|
| 189 | | sipc_destroy_handle(reader_ipc); |
|---|
| | 182 | sipc_close(writer_ipc); |
|---|
| 190 | 183 | } |
|---|
| 191 | 184 | |
|---|
| … | … | |
| 214 | 207 | } |
|---|
| 215 | 208 | |
|---|
| 216 | | sipc_disconnect(reader_ipc); |
|---|
| 217 | | sipc_destroy_handle(reader_ipc); |
|---|
| | 209 | sipc_close(reader_ipc); |
|---|
| 218 | 210 | } |
|---|
| 219 | 211 | |
|---|
| … | … | |
| 228 | 220 | bzero(data, DATA_LEN); |
|---|
| 229 | 221 | strncpy(data, DATA_END, DATA_LEN); |
|---|
| 230 | | if (sipc_send_data(sipc) < 0) { |
|---|
| | 222 | if (sipc_send_data(sipc, DATA_LEN) < 0) { |
|---|
| 231 | 223 | sipc_error(sipc, "Unable to send end of transmission message.\n"); |
|---|
| 232 | 224 | return; |
|---|
| … | … | |
| 249 | 241 | |
|---|
| 250 | 242 | rbytes = fread(data, sizeof(char), DATA_LEN-1, ifile); |
|---|
| 251 | | if (rbytes < 0) |
|---|
| | 243 | if (rbytes < 0) { |
|---|
| | 244 | fclose(ifile); |
|---|
| 252 | 245 | return -1; |
|---|
| | 246 | } |
|---|
| | 247 | fclose(ifile); |
|---|
| 253 | 248 | |
|---|
| 254 | 249 | if (strcmp(data, recv_data)) |
|---|
Download in other formats:
* Generating other formats may take time.