commit 61a43bd7325b4b74482c3da41896dc4b5055372e Author: talksik Date: Tue Nov 21 09:11:47 2023 -0800 working named pipe and pipe simple with c and golang diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dd1646a --- /dev/null +++ b/Makefile @@ -0,0 +1,34 @@ +.PHONY: all clean test pipe named-pipe +.ONESHELL: + +all: clean + echo "going to run all ipc/ffi examples" + sleep 1; + make test; + make pipe; + +test: + mkdir -p test; + cd test; + pwd; + cd ..; + # note: below line stays in root dir because no `\` at the end of previous line + rm -rf test; + +pipe: + cd pipe; + gcc test_pipe.c -o test_pipe_c_program; + go build -o test_pipe_go_program main.go; + ./test_pipe_go_program + +named-pipe: + cd named_pipe; + gcc test_named_pipe_receiver.c -o test_named_pipe_receiver_c_program; + go build -o test_named_pipe_sender_go_program main.go; + ./test_named_pipe_receiver_c_program & ./test_named_pipe_sender_go_program; + +clean: + echo "cleaning..." + rm -rf pipe/test_pipe_c_program pipe/test_pipe_go_program + rm -rf named_pipe/test_named_pipe_receiver_c_program named_pipe/test_named_pipe_sender_go_program + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e4c48a3 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/talksik/ipc_and_ffi_example + +go 1.21.4 diff --git a/named_pipe/main.go b/named_pipe/main.go new file mode 100644 index 0000000..c3c5e1e --- /dev/null +++ b/named_pipe/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "fmt" + "os" + "syscall" +) + +// createNamedPipe creates a named pipe (FIFO) +func createNamedPipe(pipeName string) error { + err := syscall.Mkfifo(pipeName, 0666) + if err != nil && !os.IsExist(err) { + return err + } + return nil +} + +func main() { + // Create a named pipe (FIFO) + pipeName := "my_pipe" + err := createNamedPipe(pipeName) + if err != nil { + fmt.Println("Error creating named pipe:", err) + return + } + + // Open the named pipe for writing + pipe, err := os.OpenFile(pipeName, os.O_WRONLY, os.ModeNamedPipe) + if err != nil { + fmt.Println("Error opening named pipe for writing:", err) + return + } + defer pipe.Close() + + // Write data to the named pipe + message := "Hello from the writer program!" + _, err = pipe.WriteString(message) + if err != nil { + fmt.Println("Error writing to named pipe:", err) + return + } +} diff --git a/named_pipe/test_named_pipe_receiver.c b/named_pipe/test_named_pipe_receiver.c new file mode 100644 index 0000000..98b52b5 --- /dev/null +++ b/named_pipe/test_named_pipe_receiver.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include + +int main() { + const char *pipeName = "my_pipe"; + + // Open the named pipe for reading + int fd = open(pipeName, O_RDONLY); + if (fd == -1) { + perror("Error opening named pipe for reading"); + return 1; + } + + // Read data from the named pipe + char buffer[100]; + ssize_t bytesRead = read(fd, buffer, sizeof(buffer)); + if (bytesRead == -1) { + perror("Error reading from named pipe"); + close(fd); + return 1; + } + + // Print the received data + printf("Data read from the named pipe: %.*s\n", (int)bytesRead, buffer); + + // Close the named pipe + close(fd); + + return 0; +} diff --git a/named_pipe/test_named_pipe_receiver_c_program b/named_pipe/test_named_pipe_receiver_c_program new file mode 100755 index 0000000..94b7474 Binary files /dev/null and b/named_pipe/test_named_pipe_receiver_c_program differ diff --git a/named_pipe/test_named_pipe_sender_go_program b/named_pipe/test_named_pipe_sender_go_program new file mode 100755 index 0000000..323cb33 Binary files /dev/null and b/named_pipe/test_named_pipe_sender_go_program differ diff --git a/pipe/main.go b/pipe/main.go new file mode 100644 index 0000000..d9d9b7c --- /dev/null +++ b/pipe/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + "os" + "os/exec" +) + +func main() { + message := "Hello from Go!" + + // Create a pipe + reader, writer, err := os.Pipe() + if err != nil { + fmt.Println("Error creating pipe:", err) + return + } + + // Execute the C program + cmd := exec.Command("./test_pipe_c_program") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Stdin = reader + + // Start the C program + if err := cmd.Start(); err != nil { + fmt.Println("Error starting C program:", err) + return + } + + // Write the message to the pipe + _, err = writer.Write([]byte(message)) + if err != nil { + fmt.Println("Error writing to pipe:", err) + return + } + + // Close the write end of the pipe + writer.Close() + + // Wait for the C program to finish + if err := cmd.Wait(); err != nil { + fmt.Println("Error waiting for C program:", err) + return + } +} diff --git a/pipe/test_pipe.c b/pipe/test_pipe.c new file mode 100644 index 0000000..37a698c --- /dev/null +++ b/pipe/test_pipe.c @@ -0,0 +1,19 @@ +#include +#include +#include + +int main() { + printf("starting C program!\n"); + + char buffer[100]; + ssize_t bytesRead = read(STDIN_FILENO, buffer, sizeof(buffer)); + + if (bytesRead < 0) { + perror("Error reading from pipe"); + return 1; + } + + printf("C Program received: %.*s\n", (int)bytesRead, buffer); + + return 0; +} diff --git a/pipe/test_pipe_c_program b/pipe/test_pipe_c_program new file mode 100755 index 0000000..7e638ed Binary files /dev/null and b/pipe/test_pipe_c_program differ diff --git a/pipe/test_pipe_go_program b/pipe/test_pipe_go_program new file mode 100755 index 0000000..0e77d25 Binary files /dev/null and b/pipe/test_pipe_go_program differ