Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send integer with pipe between two processes!

Tags:

c

linux

pipe

ipc

I am trying to send an integer with pipe in a POSIX system but write() function is working for sending string or character data. Is there any way to send integer with a pipe?

Regards

like image 828
erogol Avatar asked Mar 08 '11 19:03

erogol


1 Answers

The safe way is to use snprintf and strtol.

But if you know both processes were created using the same version of compiler (for example, they're the same executable which forked), you can take advantage of the fact that anything in C can be read or written as an array of char:

int n = something();
write(pipe_w, &n, sizeof(n));

int n;
read(pipe_r, &n, sizeof(n));
like image 185
aschepler Avatar answered Oct 26 '22 20:10

aschepler