开发者

Process and Thread related question

开发者 https://www.devze.com 2023-03-01 13:51 出处:网络
I am developing an application based on one library. I am facing problem related to communication in between Parent Process and Forked Process from Parent process.

I am developing an application based on one library.

I am facing problem related to communication in between Parent Process and Forked Process from Parent process.

I need to access Function in the library, and pointer for library is in Parent process and i am calling functins of library from Forked process using pointer in Parent Process. Function in Parent process get called from Forked process but currusponding function in Library stays in bloked state as it should be called from Parent Process only not from Forked process.

What should be the solution for this problem.

***Update:

Library which i mentioned is not exatly loaded library it has one class and i have instantiated that class through my Parent Process and then the library will create its own threds and keep running in it,

So when i do call to library through Parent Process it goes through and when i call Library functions using the Parent Pointer through Forke开发者_运维知识库d Process it does not do through. It does not segements but it gets bloked in the function call.


After the fork, both your processes have the library loaded and any existing pointers are valid in both of them, but there's no further connection between them in terms of function calls or access to data. If the parent calls a function, the parent will run the function. If the child calls a function, the child will run the function. There's no notion of one process calling a function in another process.

If you want your two processes to communicate, you need to write code to make them do so. Read about interprocess communication to learn about the various ways to do that. One simple option is to call pipe() prior to forking, and after the fork, have the parent close one of the file descriptors and the child close the other. Now you have a way for one process to send messages to the other. Do that twice and you have two-way communication. You can make the parent run a loop that waits for messages from the child via one pipe, acts upon them, and sends back the results via the other pipe.


You don't say what OS you are using, but generally pointers are not valid across processes. Most OSes give each process its own virtual memory space, so address 0x12345678 may be a pointer to something in one process, but not even an available valid address in another.

If the forked process wants to call a function, it will have to gain access to it itself (link or open the library itself, etc.)


If you're trying to share memory across two processes, but the same executable, then you should be using threads instead of forking a separate process. As others have mentioned, forking gives you a separate memory space, so you can't pass a pointer. With threads, you'd share the same memory space.


You have reached the Inter Process Communication (IPC) problem, where one program wants to make another one do something.

Since you fork()ed your child process, it now lives on his own, and to be able to execute a function on the parent process you'll have to figure out a way for them to communicate:

Child : Dad, please execute this function with these arguments, and give me the result in this pointer, please.

The problem is very widely known, you have many solutions, one of which is to design your own IPC language and implement a Remote Procedure Call (RPC) over it.

Now, people have solved the problem before, so you can take a look at some of these things:

IPC Methods

  • pipes
  • sockets (unix and network)
  • message queues
  • shared memory

RPC protocols

  • D-Bus
  • Corba
  • TPL (not an RPC protocol, but you can build one with it)
0

精彩评论

暂无评论...
验证码 换一张
取 消