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

So, I want to pass a variable from one program c to another program c.

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 the first program and read data from the second program, but I want to know if there is another way to do this?

Is it good to use files to transfer data from a program to another?

+3


source to share


2 answers


You cannot literally pass a variable between two processes, because each process on the system usually has its own memory space - each variable belongs to a process and therefore cannot be accessed from another process (or as I suppose), but you can pass data between processes using pipe .

Pipes are buffers that are implemented by the OS and are a much more efficient way to share data between processes than files (yes, you can use files for interprocess communication). This is because files must be written to disk before being accessed, which makes them slow for interprocess communication. You also need to implement some method to ensure that two processes do not damage the file while reading and writing to it.



Additionally, pipes can be used to provide 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 in order to establish a communication path between them (that is, a one-way communication path for each).

+2


source


you can:
1) pass arguments to the program. 2) use sockets for communication between processes.



0


source







All Articles