I'm using C and Winsock2 for my learning project.
I have some questions that I hope some one can confirm.
Let say I have 2 unrelated processes, process A and process B ( without using CreateProcess ). By unrelated I mean it's not parent and child.
1)
Is it possible in Windows to Accept a socket in process A and pass it to process B if they are unrelated?
2)
I guess i have to use WSADuplicateSocket? but that only works for related processes?
I hope someone can explain and confirm the above..
Is it possible in Windows to Accept a socket in process A and pass it to process B if they are unrelated?
Yes, via WSADuplicateSocket():
The WSADuplicateSocket function is used to enable socket sharing between processes. A source process calls WSADuplicateSocket to obtain a special WSAPROTOCOL_INFO structure. It uses some interprocess communications (IPC) mechanism to pass the contents of this structure to a target process, which in turn uses it in a call to WSASocket to obtain a descriptor for the duplicated socket. The special WSAPROTOCOL_INFO structure can only be used once by the target process.
I guess i have to use WSADuplicateSocket?
Yes.
but that only works for related processes?
No. It will work fine between any 2 processes, as long as process A knows process B's Process ID, as that is a required parameter of WSADuplicateSocket().
Related
My question is about more philosophical than technical issues.
Objective is to write a multiprocess (not multithread) program with one "master" process and N "worker" processes. Program is linux-only, async, event-based web-server, like nginx. So, the main problem is how to spawn "worker" processes.
In linux world there are two ways:
1). fork()
2). fork() + exec*() family
A short description for each way and what confused me in each of them.
First way with fork() is dirty, because forked process has copy (...on-write, i know) of parent memory: signal handlers, variables, file\socket descriptors, environ and other, e.g. stack and heap. In conclusion, after fork i need to...hmm..."clear memory", for example, disable signal handlers, socket connections and other horrible things, inherited from parent, because child has a lot of data that he was not intended - breaks encapsulation, and many side-effects is possible.
The general way for this case is run infinite loop in forked process to handle some data and do some magic with socket pair, pipes or shared memory for creating communication channel between parent and child before and after fork(), because socket descriptors reopen in child and used same socket as parent.
Also, this is nginx-way: it has one executable binary, that use fork() for spawn child process.
The second way is similar to first, but have a difference with usage one of exec*() function in child process after fork() for run external binary. One important thing is that exec*() loads binary in current (forked) process memory, automatic clear stack, heap and do all other nasty job, so fork will look like a clearly new instance of program without copy of parent memory or something other trash.
There has another problem with communication establishing between parent and child: because forked process after exec*() remove all data inherited from parent, that i need somehow create a socket pair between parent and child. For example, create additional listen socket (domain or in another port) in parent and wait child connections and child should connect to parent after initialization.
The first way is simple, but confuse me, that is not a clear process, just a copy of parent memory, with many possible side-effects and trash, and need to keep in mind that forked process has many dependencies to parent code. Second way needs more time to support two binary, and not so elegant like single-file solution. Maybe, the best way is use fork() for process create and something to clear it memory without exec*() call, but I cant find any solution for second step.
In conclusion, I need help to decide which way to use: create one-file executable file like nginx, and use fork(), or create two separate files, one with "server" and one with "worker", and use fork() + exec*(worker) N times from "server", and want know for pros and cons for each way, maybe I missed something.
For a multiprocess solution both options, fork and fork+exec, are almost equivalent and depends on the child and parent process context. If the child process executes the parents' text (binary) and needs all or a part of parents' staff (descriptors, signals etc) - it is a sign to use fork. If the child should execute a new binary and needs nothing from the parents' staff - it seems fork+exec much more suitable.
There is also a good function in the pthread library - pthread_atfork().
It allows to register handlers that will be called before and after fork.
These handlers may perform all the necessary work (closing file descriptors, for example).
As a Linux Programmer, you have a rich library of multithreading process capabilities. Look at pthread and friends.
If you need a process per request, then fork and friends have been the most widely used since time immemorial.
Linux Kernel version 3.2 and further have a capability called cross memory attach.
Here is the link to it. I was not able to get a lot of help in that regard.
http://man7.org/linux/man-pages/man2/process_vm_readv.2.html
In the syntax we need the address of the remote memory where we want to write to or read from. My question is how do I get the address of this remote memory if I am using fork().
Suppose I am sending something from parent process to child process using cross memory attach. How do I send the address of the remote memory to the parent process from the child process?
The system calls process_vm_readv and process_vm_writev are meant for fast data transfer between processes. They are supposed to be used in addition to some traditional way of interprocess communication.
For example, you may use a regular pipe or fifo to transfer the required addresses between your processes. Then you may use those addresses to establish faster process_vm_ communication. The simpliest way to transfer something between forked processes should be the pipe() function (man 2 pipe has a good example of its usage). There are many other ways to do so of course, like using sockets or messages. You can even write an address to a file and let the other process read it.
Hi all I am new to C so sorry if I am very lost. I am having trouble with this multi-threaded web server I am trying to create. I am attempting to...
have a thread create a new thread
have that new thread execute execvp() to call a different C program on my machine
have that new thread return streams of data from the execvp()
I was thinking about using pthreads to spawn a new process to run execvp() and have it return the data through a pipe. But is that even necessary? Don't pthreads share memory?
Also, I was maybe thinking about using fork() instead of a pthread and have the child send data back to the parent through a pipe.
Can you please help guide me in the correct direction.
What you're looking for is a combination of fork(), one of the exec functions, and pipe() (or maybe socketpair() or something, but pipes work too).
Threads share memory, but execvp() would create a completely new process replacing the caller process -- and even if this process shared memory with its parent (which I'm not sure it does!), the newly run program wouldn't know how to use that memory.
The proper way is to open a pipe when you still have one process, fork() into two processes (parent and child), and have the child call execvp(). The child can now write into its end of the pipe, and the parent can read from the other end.
Remember to wait() for the child to end.
Have you written your non-blocking, single-threaded web-server yet? How would you expect to measure the benefits of multithreading if you don't have something to compare it against? It's far easier to determine where the best performance gains are if you expose a single-threaded project to concurrency, than it is to guess and suffer with a poor framework for the rest of the project's life.
Creating threads is easy, but you really need to read the pthread_create manual first. How else can you trust that your project is handling errors correctly? I also suggest reading about the other pthread functionality. I'm happy to help you resolve issues if you show me that you're trying to resolve them yourself, by the way. I won't bother spoonfeeding you.
As mentioned by aaaaaa123456789, you wouldn't want to spawn using pthread_create/execvp as this would replace your entire program environment (including all of your threads) with the new process.
I have used fork() to create 2 different processes operating on 2 different address spaces.
Now, in parent process I need the value of a variable from child's address space or if the child process can modify the variable in parent's address space.
Is this possible?
No, once you've forked, each process gets its own address space and you'll have to look into either:
some form of IPC between the processes to access each others data (such as shared memory or message queues).
some more lighweight variant of fork that allows sharing of data (including possibly threading).
Once you have two processes, sharing data needs interprocess communication: file, pipe or shared memory.
If you mean exchanging data between these two processes you can not. You can do it by system APIs like SharedMemory, Message Passing, Pipeline, Socket, ...
As you have created two process using fork command Both Process will be in different address space so they will only communicate by IPC, message passing ,Piping , Shared Memory etc. otherwise one process can't access other process data as thay do have Process specific data
and similarly threads also have thread specific data
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.