Inter-process communication using pipes - c

I think maybe this is an obvious question, but I just want to be sure by asking you guys.
I'm working with parent-child process communication using the pipe system call to create a unnamed pipe.
My child process needs to gather some information and send it to its parent. My questions are:
Can I only send and receive strings using the write and read functions, right? I have to forget about sending structures.
If the answer to my previous question is "yes", the right way to transfer all the information to the parent process is to call the functions write and read several times?
Thank you very much!

You can write() and read() structs just fine; use a pointer to the struct as the buf parameter. It's when you want to do this between processes not running on the same machine that you run into problems and need to do marshaling/unmarshaling to portable representations to insure the values are understood the same way everywhere. This includes recognizing the start and end of data "packets", since a pipe doesn't really have the concept of packets: if all you're doing is writing a series of identical structs, then you can just write() them and the reader can rely on read() returning 0 to indicate the end of the series; but if you need to send other information as well then you'll need a framing protocol to say "what follows is such-and-such struct", "what follows is a string", etc.

Related

Is it possible to pipe() within a program in C?

Let's say that there is an existing program that listens on stdin for it's inputs. I want to create a pthread within the same program that is now the one to listen to stdin, and depending on what comes through, let it go through to the original program.
For this, I would create a pipe(), and configure the pthread to write to the input file descriptor, and the original program to listen to the output descriptor. Is this a correct way to have this done? I understand piping between processes, but is it possible to pipe like this within a single process?
Sure, you can use pipe(), but the data has to pass through the kernel even though both the end points are within the same process.
If you have source code for this (which I assume you have) and you don't mind making non-trivial changes, and performance is a priority for you, I would suggest using shared memory to send the data to the original program. It will be much faster than using pipe()

Passing multiple chunks of data using pipe in C

I need to send 3 char buffers to child process and I want to treat them as 3 separate chunks of data. I thought of using read() and write() system calls but after reading man I can't see a way to separate the data - if I understand it correctly, if I write 3 buffers one by one in parent process, then one call of read() will read all the data. Of course I could put some separators like '\0' in input buffers and separate the data in child, but I'm looking for some more elegant way to do this. So, is there some kind of system call that enables to pass data sequentially?
One possibility is to use what stdio.h already gives you: fdopen() the respective ends of the pipes and use fgets()/fputs() with the FILE pointers. This assumes your data doesn't contain newlines.
Some alternatives could be to use fixed sizes with read()/write() or to use some other delimiter and parse the received data with strtok(). You could also send the size first so the child knows how many bytes to read in the next read() call. There are really lots of options.
If you have it, you can use O_DIRECT to get a "packet-oriented" pipe, but there are limitations of course.
In general for a text-based streamed protocol having separators is cleaner in my opinion.
You have two choices
Put delimiters in the data (as you mentioned in the question).
Provide feedback from the child. In other words, after writing a chunk of data to the pipe, the parent waits for a response from the child, e.g. on a second pipe, or using a semaphore.
You could precede each chunk of data with a header, including a length field if chunks can be variable length. The reader can read the header and then the chunk contents.

Can we pass variables from one c program to another c program?

So I want to pass a variable from one c program to another c program.
For example:
main()
{
char str[]="Hello,there!";
system("program2.exe");
}
And I want to use str[] in program2.exe. Is there a way to pass a variable to another program?
I used files to write data from first program and read data from second program but I want to know is there any other way to do this?
Is it good to use files for passing data from program to another?
You can't literally pass a variable between two processes because each process on a system will generally have it's own memory space - each variable belongs to a process and therefore can't be accessed from another process (or so I believe). But you can pass data between processes using pipes.
Pipes are buffers that are implemented by the OS and are a much more effective method of sharing data between processes than files (yes, you can use files for inter-process communication). This is because files must be written to a disk before being accessed which makes them slow for inter-process communication. You'd also have to implement some kind of method for ensuring the two processes don't corrupt a file when reading and writing to it.
Also, pipes can be used to ensure continuous communication between two processes, making them useful in many situations. When using half duplex pipes (linked above), you can have a pipe for each process to establish a communication channel between them (i.e. a one way communication channel for each).
you can:
1) pass arguments to a program.
2) use sockets to communicate between processes.

C (linux) - Emulate / Skip scanf input

I have a program running 2 threads. The first is waiting for user input (using a scanf), the second is listening for some data over an udp socket. I would like to emulate user input to handle a specific notification with the first thread everytime I recive a specific udp packet. I know I can share variables between threads, so my question is: can I force the scanf to take input from a different thread? Can I skip the scanf in the first thread?
I believe scanf() by definition reads from stdin. Like you said, though, the different threads share memory so it's easy to pass information between them. Maybe have some shared variable and some sort of boolean value indicating whether or not the information has been updated from the thread reading from the network. It all depends on what you're specifically trying to do, but you may want to have some other mechanism for simulation that bypasses the scanf().
Since you've specifically mentioned Linux, I'm going to suggest a novelty here.
You can open (/proc/%d/fd/%d, getpid(), STDIN_FILENO) and write to it. This will actually open the input of the terminal. I wouldn't recommend this for a real program, but then again, scanf shouldn't be used in real programs either.

How to pass data from one process to another in c?

Is there a way to pass data (ex: int value) from one process to another process in c?
In my experience, we just can send signal from one process to another. But looks like there is no way to "attach" some information along with that signal to another process.
With the sigqueue function, you can pass a single integer or pointer along with a signal (but keep in mind, pointers will be useless if the target of the signal is another process, since different processes don't share address space).
Some other methods are pipes, shared memory (POSIX or SysV style), files, ...
You can use one of the various Inter Process Communication Mechanisms available.
Use Google. As a reference you can also look here
A clean, portable, powerful way is use Socket.
You can use pipes to do that. The main purpose of pipes is to communicate data between different processes.
Pipes are the simplest mechanism offered by the operating system for inter-process communication. A pipe is a communication buffer between two processes: it has two descriptors, one for writing another for reading. Write and read operations are done in a FIFO order (first-in-first-out).
There are two kinds of pipes: unnamed pipes and named pipes (also known as FIFOs).
Unnamed pipes only allow communication between hierarchically related processes (parent and child processes);
Named pipes allow the communication between any process. A special file is created in the file-system through
If you want some example code just go here: http://pastebin.com/1W216nyN
I think we can use global variable between process, not sure but. If any one tried then please let me know. If we use a header which contain extern valriable , we can use this in another main() which is nothing but a independednt program (process). but we have to link the two main() together which executing.

Resources