working named pipe and pipe simple with c and golang
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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;
|
||||
}
|
||||
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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;
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Reference in New Issue
Block a user