sustrik / libdill

Structured concurrency in C
MIT License
1.68k stars 156 forks source link

Examples using ipc_close should use deadline #212

Open suvarchal opened 2 years ago

suvarchal commented 2 years ago

Examples (at least) listed below should end with ipc_close(s, -1); http://libdill.org/ipc_connect.html http://libdill.org/ipc_close.html

example fix (just last line)

int s = ipc_connect("/tmp/test.ipc", -1);
bsend(s, "ABC", 3, -1);
char buf[3];
brecv(s, buf, sizeof(buf), -1);
ipc_close(s, -1);
ljluestc commented 7 months ago
#include <libdill.h>
#include <stdio.h>

int main() {
    // Connect to the IPC endpoint
    int s = ipc_connect("/tmp/test.ipc", -1);
    if (s < 0) {
        perror("Error connecting to IPC endpoint");
        return 1;
    }

    // Send data
    int sent = bsend(s, "ABC", 3, -1);
    if (sent < 0) {
        perror("Error sending data");
        ipc_close(s, -1);
        return 1;
    }

    // Receive data
    char buf[3];
    int received = brecv(s, buf, sizeof(buf), -1);
    if (received < 0) {
        perror("Error receiving data");
        ipc_close(s, -1);
        return 1;
    }

    printf("Received data: %.*s\n", received, buf);

    // Close the IPC connection
    ipc_close(s, -1);

    return 0;
}