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
Related
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.
I'm working with semaphores in C , especifically to control the access to a shared memory zone in linux. but there is one thing that I can't understand.
I am using a mutex to control the access to a specific zone because i have 2 processes that must read/write from that zone. the thing is, when we use the fork() to create a new child process, the whole program is "copied" to another program as if they were two seperate programs right ? so, when i do V(mutex) in one process, how does the other one know he can't access ?
I know its a noob question but nobody could explain this to me until now.
After the fork neither process is going to know about the memory actions of the other because they are separate copies. You have to put your shared variables in shared memory, including mutexes and semaphores. Then all the processes are operating on the same resource.
For unrelated (i.e. non-forked) process there are usually system facilities (e.g. named semaphores) that each process can open based on a path name or similar method that each can use to find and use the resource.
You synchronisation objects must be placed in process shared memory, for example created with mmap (... MAP_ANONYMOUS ...). In addition, they must have the PTHREAD_PROCESS_SHARED attribute set, for example, by using pthread_mutexattr_setpshared.
See here:
Semaphores and Mutex for Thread and Process Synchronization
So mutex in practice is often used in threads, which makes sharing trivial. For processes however, mutex could be stored as a part of the shared mem.
For semaphores however, linux has built in library, which identifies global semaphores by keys. See below.
http://beej.us/guide/bgipc/output/html/multipage/semaphores.html
Or you can use other IPC to sync. Signals, for example.
Hope this helps.
I am new to threads and processes.
I have code that works fine right now with forking the code into multiple processes. However each process needs to add to a global variable, but from what I read, each time the process forks, it takes a copy of the global, and adds them independently. Is there a way to join them, like you can with threads?
Different processes can communicate and exchange data via shared memory.
On linux, you can look:
man shm_overview
for attaching a memory segment on several processes
and
man sem_overview
for the semaphore library for controlling parallel access.
You should define a struct with two fields, one for your global and one for a semaphore. Then, before any forking occurs, create some shared memory in the parent process big enough to hold this struct and initialize one there. In the children, map in the shared memory so they can access the global. All processes, parent and children, should obey the rules of the semaphore when accessing the global.
To avoid unnecessary blocking which can hurt performance, try not to hold the semaphore too long. When reading the global, make a quick copy of it in a process and use that, rather than holding the semaphore for the entire time you are using its value. Likewise, when changing the global, prepare your changes ahead of time (before you grab the semaphore) and, once you have the semaphore, copy them in all at once. Sometimes your work depends on reading and writing the global without it changing in between being read and written. In this case, some blocking may be inevitable.
It is not clear what platform you are on, but all major PC and server platforms (Windows, Linux/Unix/Mac OS) have support for shared memory and semaphores. The APIs may be different, but the functionality you need is there.
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.
Say I have a pointer to some structure in a thread, and I want to pass it to the parent process via a pipe.
Example:
MyType * someType;
I then want to cast someType to void * and put it on the pipe. How can it be done?
While you can physically pass a pointer to a parent process, the value would be meaningless to the process and your best case scenario would be an immediate crash. Pointers indicate an address of an object in memory. This address will only be valid in the context of the child process and will point to a completely different object in the parent process.
You'll need to do one of the following in order to enable this scenario
Pass the entire object across the pipe in some serialized from
Pass a pointer relative to the shared memory base between the processes and do the appropriate fixup in the parent process.
EDIT
Note, my answer was written when the question was asking about how to pass a pointer between a child and parent process. It was later updated to threads.
Another option would be to store the object in shared memory, then pass the segment ID to the parent process.
The parent can then attach to the memory and access/modify the object.
This gives some background:
http://fscked.org/writings/SHM/shm.html
Reading between the lines in this and the other question you cite, you are using "parent process" to refer to the main process thread, and "thread" to refer to a new thread created within that same process. This is causing confusion both for your thinking about the problem and for others trying to answer the questions.
In this scenario, there is exactly one process, and it has two threads. The first thread was created for you when the OS started the process. The second was created deliberately by the first. You have decided to use pipes to communicate between these threads.
First, I'd agree with a lot of the answers on the other question that pipes are a bit of a heavy-weight solution to inter-thread communication since they were designed to handle inter-process communication. That said, they will work.
Second, be aware that you won't be able to meaningfully move a pointer between processes. Pointers are only valid within a single process. Even pointers to shared memory have issues since the shared memory regions might be mapped to different virtual addresses in each process. Since it looks like both ends of the pipe are in the same process, this isn't an issue, but if that weren't true, it would be a big issue.
With all of that in mind, you then just need to agree with yourself on a representation for the pointer. The simplest answer is to just write sizeof(void *) bytes to the pipe. When read out, you put those bytes back into a pointer variable, and cast back to the real type. Your surrounding protocol must know what that type is, of course.
If you become tempted to let the two threads exist in separate processes, or to reuse this code to persist (checkpoint) your work in progress in a file, then you have a more complicated problem. Searching for discussions of data and state persistence, pickling, and marshaling will lead to things to think about.
Put it in shared memory, and pass a pointer relative to the shared memory base.
Pointer casting depends on how your compiler aligns objects. An remember when getting relative pointer positions, pointer arithmetic is based on sizeof the thing pointed to.