免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2878 | 回复: 13
打印 上一主题 下一主题

[C++] 日志文件被反复调用写入的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-13 14:25 |只看该作者 |倒序浏览
用c++写一个函数,功能要求根据输入参数在log.xml中写入节点。由于程序比较大,而且是多线程的。在这个函数被频繁调用的过程中,应该注意些什么?还有多进程调用时该如何保证写入的先后顺序和节点的正确性?请各位多多指教!谢谢

论坛徽章:
0
2 [报告]
发表于 2009-04-13 14:40 |只看该作者
锁住文件

论坛徽章:
0
3 [报告]
发表于 2009-04-13 14:46 |只看该作者
为什么不用数据库??

论坛徽章:
0
4 [报告]
发表于 2009-04-13 14:51 |只看该作者
因为没有数据库,2楼能具体说一下吗?

论坛徽章:
0
5 [报告]
发表于 2009-04-13 15:00 |只看该作者
原帖由 zshuming616 于 2009-4-13 14:51 发表
因为没有数据库,2楼能具体说一下吗?
  1. SYNOPSIS
  2.        #include <sys/file.h>

  3.        int flock(int fd, int operation);

  4. DESCRIPTION
  5.        Apply or remove an advisory lock on the open file specified by fd.  The argument operation is one of the
  6.        following:

  7.            LOCK_SH  Place a shared lock.  More than one process may hold a shared lock for a given  file  at  a
  8.                     given time.

  9.            LOCK_EX  Place an exclusive lock.  Only one process may hold an exclusive lock for a given file at a
  10.                     given time.

  11.            LOCK_UN  Remove an existing lock held by this process.

  12.        A call to flock() may block if an incompatible lock is held by another process.  To make a  non-blocking
  13.        request, include LOCK_NB (by ORing) with any of the above operations.

  14.        A single file may not simultaneously have both shared and exclusive locks.

  15.        Locks  created  by flock() are associated with an open file table entry.  This means that duplicate file
  16.        descriptors (created by, for example, fork(2) or dup(2)) refer to the same lock, and this  lock  may  be
  17.        modified  or  released  using  any of these descriptors.  Furthermore, the lock is released either by an
  18.        explicit LOCK_UN operation on any of these duplicate descriptors, or when all such descriptors have been
  19.        closed.

  20.        If  a  process  uses  open(2)  (or  similar) to obtain more than one descriptor for the same file, these
  21.        descriptors are treated independently by flock().  An attempt to lock the file using one of  these  file
  22.        descriptors  may be denied by a lock that the calling process has already placed via another descriptor.

  23.        A process may only hold one type of lock (shared or exclusive) on a file.  Subsequent flock()  calls  on
  24.        an already locked file will convert an existing lock to the new lock mode.

  25.        Locks created by flock() are preserved across an execve(2).

  26.        A  shared or exclusive lock can be placed on a file regardless of the mode in which the file was opened.



  27. RETURN VALUE
  28.        On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.


复制代码

[ 本帖最后由 net_robber 于 2009-4-13 15:01 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2009-04-13 15:01 |只看该作者
最后,还是建议你使用数据库


没有数据库就装一个

论坛徽章:
0
7 [报告]
发表于 2009-04-13 15:03 |只看该作者
谢谢,我们这个项目目前不用数据库。

论坛徽章:
0
8 [报告]
发表于 2009-04-13 15:07 |只看该作者
写LOG不用锁可以的.
需要用APPEND方式打开, 每进程的LOG(一次有含义的,比如一行)一次写入.
这样, 写是原子的. 不会被各THREAD的LOG交叉.

论坛徽章:
0
9 [报告]
发表于 2009-04-13 15:46 |只看该作者
用锁,或者用追加方式

论坛徽章:
0
10 [报告]
发表于 2009-04-13 15:51 |只看该作者
请各位详述一下如何使用锁和追加的两种方式可以吗?谢谢了。

[ 本帖最后由 zshuming616 于 2009-4-13 17:03 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP