working named pipe and pipe simple with c and golang
This commit is contained in:
@@ -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