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.
Related
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()
I am writing a rudimentary shell program in C which uses a parent process to handle shell events and fork() to create child processes that call execv on another executable (also C).
I am trying to keep a process counter on the parent process. And as such I thought of the possibility of creating a pointer to a variable that keeps track of how many processes are running.
However, that seems to be impossible since the arguments execv (and the program executed by it) takes are of type char * const argv[].
I have tried to keep track of the amount of processes using mmap for shared memory between processes, but couldn't get that to work since after the execv call the process simply dies and doesn't let me update the process counter.
In summary, my question is: Is there a way for me to pass a pointer to an integer on an execv call to another program?
Thank you in advance.
You cannot meaningfully pass a pointer from one process to another because the pointer is meaningless in the other process. Each process has its own memory, and the address is relative to that memory space. In other words, the virtual memory manager lets every process pretend it has the entire machine's memory; other processes are simply invisible.
However, you do have a few options for setting up communications between related processes. The most obvious one is a pipe, which you've presumably already encountered. That's more work, though, because you need to make sure that some process is always listening for pipe communications.
Another simple possibility is to just leave a file descriptor open when you fork and exec (see the close-on-exec flag to see how to accomplish the latter); although mmap is not preserved by exec, you can remap the memory to the open fd in the child process. If you don't want to pass the fd, you can mmap the memory to a temporary file, and use an environment variable to record the name of the temporary file.
Another possibility is Posix shared memory. Again, you might want to communicate the shm name through an environment variable, rather than hard-coding it in to the application.
Note that neither shared mmaps nor shared memory are atomic. If you're incrementing a counter, you'll need to use some locking mechanism to avoid race conditions.
For possibly a lot more information than you really wanted, you can read ESR's overview of interprocess communication techniques in Chapter 7 of The Art of Unix Programming.
let's say I have 2 processes and I have a variable I want to pass from the first one to the second one. I know I can declare a global variable and pass it by reference among differents functions, but I don't know if it is possible to pass a variable among different processes.
I heard that each process is assigned its own portion of virtual memory and that one process cannot access another process' memory space. Is it that true? Or is is actually possible for two processes to share a variable and therefore mutex mechanisms are needed?
I don't know if it is possible to pass a variable among different processes.
No, it is not possible, at least not in the classical sense of passing a variable. You have many options, though: inter-process communication can be done through shared memory (sometimes implemented through memory-mapped files), named pipes, etc.
Or is is actually possible for two processes to share a variable and therefore mutex mechanisms are needed?
Yes, you should look up shared memory, or more generally, IPC / interprocess communication.
If the second process is started from the first one, you could pass it as command line parameter.
Otherwise you should rely on some inter-process communication method (like Socket or FIFO, also known as named pipe).
You could have a look to this other post:
Interprocess Communication via file
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.
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.