fork()的问题和存储用户的数据 [英] Problem with fork() and storing data of user

查看:86
本文介绍了fork()的问题和存储用户的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初编写了一个简单的信使,我没有使用fork在主机和用户之间进行收缩,并将用户信息存储到一个结构中,所以当用户要求它们时我把它们还给了她/他。

但是我发现主机无法在没有分叉的情况下同时回答多个用户,现在当我使用fork时我不知道发生了什么,所以我无法将用户信息发回给她/他。

这是紧急情况我知道使用数据库会解决它但是还有其他任何建议(那是我的大学项目和截止日期是大约5个小时:()



我尝试了什么:



如果您有兴趣看到部分代码通知我因为它太长而且不认为是有用的

I wrote a simple messenger at first I didn't use fork to contract between host and user and stored users information into a structure so when user asked for them I turned them back to her/him.
but I find host cannot answers to multiple users at the same time without forking and now when I use fork I don't know what is happening so I cannot get back users information to her/him.
it's urgent situation I know using database would solve it but is there any other suggestion please(that was for my university project and deadline is about 5 more hour :( )

What I have tried:

if you interested to see part of the code inform me cause it is too long and don't think be useful

推荐答案

为了支持同时连接的多个用户,您可以创建为连接提供服务的线程。这比分支整个进程更容易加权并允许访问通讯和共享数据。



另一个解决方案是使用一个包含连接和用户相关数据的列表。



但这些是在开始编写代码之前应该做出的设计决策。



在您的情况下,使用线程可能更简单(更快)实现。但即使是经验丰富的开发人员也可能在五小时内完成。
To support multiple users connected at the same time you can create threads that service the connections. That is more leight weighted than forking a whole process and allows access to common and shared data.

Another solution is using a list that holds the connection and user related data.

But these are design decisions that should be done before starting writing code.

In your case using threads might be simpler (faster) to be implemented. But even I as experienced developer would it probably not get done in five hours.


Quote:

I知道在5个小时内不可能:(但是想在假期后再尝试,所以它与数据库有关系吗?(我现在甚至不是什么数据库)

I know that it isn't possible in 5 hours :(, but wanted to try it later in holiday, so it is related to database or not?(I don't now even what is database)



可能不是:问题在于,当你告诉你的Socket听,它没有返回,直到它有连接,所以为了支持多个用户你需要多个进程 - 这意味着你需要使用 fork 然后在收到消息时响应相应的Socket。数据库无法帮助你,它不支持多个套接字,它只存储数据!



你还剩下三个小时,说实话,除非你能在这段时间内使叉子/插座工作,你如何存储数据是无关紧要的(更不用说学习数据库了)这是一个五分钟的工作 - 哎它可能需要你首先要安装一个多用户数据库系统,不要介意学习使用它!)



如果你可以在假期工作,然后执行:首先排序分叉和套接字,并且在此之前不要担心数据。


Probably not: the problem is that when you tell your Socket to Listen, it does not return until it has a connection, so to support multiple users you need multiple processes - which means you need to use fork and then respond to the appropriate Socket when it gets a message. A database won't help you with that, it doesn't support multiple Sockets, it just stores data!

You only have three hours left, and to be honest, unless you can get the fork / sockets working in that time how you store the data is irrelevant (not to mention that learning databases isn't a five minute job - heck it'll probably take you longer than that to install a multiuser database system in the first place, never mind learning to use it as well!)

If you can work on it over the holiday, then do: get forking and sockets sorted first and don't worry about data until after that.


这篇关于fork()的问题和存储用户的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆