English 中文(简体)
与启动进程有关的问题
原标题:Questions Related To Forking a Process

我正在制作一个服务器,我使用叉(k)创建儿童进程,但我对此表示怀疑。 这里有一些:

  1. Why do you need to close main socket in a child process and new connection accepted socket in the parent process? (after accepting a new connection) I thought that sockets are only integers with some id which are used to access opened sockets in some system-wide object which is only acceptable through system functions calls. In that case fork would only copy the integer but would have no affect on socket opened.
  2. I have checked and found out that if I fork a process inside a class method, all members are copied. Well, I have found out it is copy-on-edit so does it mean that my server class will be copied in each child that uses a non-constant function? How do I make some memory shared between all such processes (like a list of tasks and each child puts something into it while the parent is reading stuff from it)? I guess fork is not the right function for that. What is the best way?

P.S.,我很相信,我知道第二个问题的答复,即“衣物”,但只是想确保这一功能正确。

最佳回答
  1. Sockets in Unix are File descriptors and they are indeed integers, as seen by the user, but they really are indexes into a table that the kernel maintains per process. In this table each file descriptor (FD) refers to an open file description (OFD) that are system-wide objects maintained in kernel. When you do fork() the opened file descriptors are duplicated and both child s and parent s point to the same OFD. Having two FDs that refer to the same OFD is not usually a problem, but particularly with sockets they can have subtle problems, as the connection is closed only when you close all the FDs that refer to it.

  2. 您应真正考虑使用校对(如果你使用read子,则不要关闭袖珍)。 你们的替代办法是使用共享记忆,但这种记忆更为复杂。

问题回答
  1. <打字码>int是一种处理,但单体本身仍与这一过程有关。 该儿童主要出于安全原因关闭了倾听书状(不需要,如果孩子们发现另一个过程,这一过程也会继承书状);服务器程序关闭了新链接的袖珍,因为否则,在服务器程序退出之前,该连接将保持开放(只要至少一个过程仍然可以处理)。

  2. 你们要么需要多读,要么需要适当的共享记忆方法。 就是假日。

独立进程之间共享的记忆会产生令人感兴趣的问题,但也提供了本来是不可能的能力(例如,你可以重新启动主服务器进程,让为开放式连接服务的流程运行,这很难成为两个不同版本的服务必须相互交谈的,但可以进行无缝升级,而不必与客户脱节或中断服务)。

在校对之间分享记忆相对容易,但校对共用相同的一套文件说明,因此,你在这里不会胜过。

最后,还有第三种选择:一事件看着多份书状,只有在实际发生时才会注意每一件。 查阅<代码>电子格式>和<代码><>>>/代码>功能的文件。

删除重复档案,因此,你必须关闭复制件。

还可有效复制所有记忆(尽管在实践中,它带有影印本,因此并不昂贵)。 除非你明确建立一些共同记忆,否则你会形成一个与父母进程完全分开的新

Maybe you intended to spawn a new thread rather than forking a new process?

我认为,你不妨通过,作为关于k(a)的参考。

  1. Yes you do need to close the socket bound to listen in the child and accepted socket in the parent. The integers aka file handles point to real structures see this so unless you want the kernel to dump a new connection on a child or parent being able to send the data to the connected client you might want to prevent this outright.
  2. To share data for between the processes the best way is shared memory. The book I referred you to will have quite a bit of information regarding that too. In general if you need to share memory without shared memory then you might want to look at threads.

P.S. I m不能确定你指的是哪一种衣物。 申请复制是通过影印机进行的。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?