Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send signal from C to Java

Tags:

java

c

signals

So I have this Java program that creates processes that run a certain C program on the terminal (unix), and I need to notify the Java program when something happens in the C program (before the termination). How can I do this? I know that I'm gonna need signals, but I don't have much experience on the subject.

Thanks in advance!

EDIT: This the changes I made to the java, after calling the process:

            InputStream stdout = p.getInputStream();
            InputStreamReader isr = new InputStreamReader(stdout);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            System.out.println("<INPUT>");
            while ( (line = br.readLine()) != null)
                System.out.println(line);
            System.out.println("</INPUT>");
            int exitVal = p.waitFor();
            System.out.println("Process exitValue: " + exitVal);

I what I did in the C when I want to notify the java:

char buff[20];
size_t nbytes;
ssize_t bytes_written;
int fd;
strcpy(buf, "This is a test\n");
nbytes = strlen(buf);
bytes_written = write(1, buff, nbytes);

But after running it I only get:

INPUT
/INPUT
Process exitValue: 0
like image 997
danielnovais92 Avatar asked Mar 28 '26 05:03

danielnovais92


2 Answers

One of the ways is reading your C program's stdout

    Process p = Runtime.getRuntime().exec("c.exe");
    InputStream stdout = p.getInputStream();

now C program can talk to Java program

like image 98
Evgeniy Dorofeev Avatar answered Mar 29 '26 17:03

Evgeniy Dorofeev


You can create a socket using c program and send it to java socket server. You can search on goole to find out sample.

like image 36
someone Avatar answered Mar 29 '26 18:03

someone