Chinaunix

标题: 在LInux下怎么样进行父子进程同步呢 [打印本页]

作者: zenglei286    时间: 2007-10-15 11:17
标题: 在LInux下怎么样进行父子进程同步呢
怎么样在父子进程中进行同步控制呢,我想让父进程执行完了,再执行子进程,能不能弄个例子来说明呀,
还有如何与其他的不相干的进程进行同步呢,最好不要用到信号量
作者: scutan    时间: 2007-10-15 11:46
不用信号量的话就用fcntl文件锁吧, 不过为什么不用信号量呢?
在<UNIX网络编程>卷二中有关于信号量的详细的实例, 可以看看. 也不是太难.
作者: cugb_cat    时间: 2007-10-15 12:11
如果只是规定父子进程的执行顺序的话可以用信号的,子进程在需要同步的地方调用pause,父进程在适当的时候发送一个信号把pause打断
作者: zenglei286    时间: 2007-10-15 12:12
标题: 回复 #2 scutan 的帖子
fcntl是文件锁吗,那和同步有什么关系呢,怎么样控制进程的同步呢
作者: zenglei286    时间: 2007-10-15 12:13
标题: 回复 #3 cugb_cat 的帖子
能写一个例子吗,不用太复杂,只要能说明问题的部分写出来就可以
作者: zwylinux    时间: 2007-10-15 12:14
apue 英文版2nd  P337 有相关的实现,基于信号的
作者: scutan    时间: 2007-10-15 12:38
原帖由 zenglei286 于 2007-10-15 12:13 发表
能写一个例子吗,不用太复杂,只要能说明问题的部分写出来就可以


  1. #include<stdio.h>
  2. #include<fcntl.h>
  3. #include<semaphore.h>
  4. #include<stdlib.h>
  5. #include<unistd.h>
  6. #include<sys/types.h>
  7. #include<sys/mman.h>
  8. int main(int argc, char **argv)
  9. {
  10.         int fd, i, nloop, zero = 0;
  11.         int *ptr;
  12.         sem_t *mutex;

  13.         if (argc != 3)
  14.         {
  15.                 fprintf(stderr, "usage: test <pathname> <#loops>\n");
  16.                 exit(1);
  17.         }
  18.         nloop = atoi(argv[2]);
  19.         if ((fd = open(argv[1], O_RDWR|O_CREAT, 0644)) < 0)
  20.         {
  21.                 fprintf(stderr, "open %s error\n", argv[1]);
  22.                 exit(1);
  23.         }
  24.         if (write(fd, &zero, sizeof(int)) < 0)
  25.         {
  26.                 fprintf(stderr, "write error\n");
  27.                 exit(1);
  28.         }
  29.         ptr = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  30.         if (ptr == NULL)
  31.         {
  32.                 fprintf(stderr, "mman error\n");
  33.                 exit(1);
  34.         }
  35.         close(fd);
  36.         mutex = sem_open("/hello", O_CREAT|O_EXCL, 0644, 1);
  37.         if (mutex == SEM_FAILED)
  38.         {
  39.                 fprintf(stderr, "sem_open fail\n");
  40.                 exit(1);
  41.         }
  42.         sem_unlink("/hello");
  43.         setbuf(stdout, NULL);
  44.         if (fork() == 0)
  45.         {
  46.                 for (i = 0; i < nloop; i++)
  47.                 {
  48.                         sem_wait(mutex);
  49.                         printf("child: %d\n", (*ptr)++);
  50.                         sem_post(mutex);
  51.                 }
  52.                 exit(0);
  53.         }
  54.         for (i = 0; i < nloop; i++)
  55.         {
  56.                 sem_wait(mutex);
  57.                 printf("parent: %d\n", (*ptr)++);
  58.                 sem_post(mutex);
  59.         }
  60.         return 0;
  61. }

复制代码

这是<UNIX网络编程>卷2上的一个父子进程同步的例子. 使用信号量的方法, 比较简单. 你看看吧

用文件锁当然可以了. 你父进程先去把文件锁了, 然后子进程每次运行时去读那个文件, 看是否锁住, 如果锁住则不运行, 如果没锁则将其锁定, 就这样啊.
作者: zenglei286    时间: 2007-10-20 21:37
标题: 回复 #3 cugb_cat 的帖子
怎么打断PAuse这个方法呀
作者: ddvv    时间: 2007-10-20 22:55
进程间的通信方法有很多,pipe,FIFO,消息队列等等,去看看相关的书籍就知道了
作者: li32768    时间: 2009-12-10 20:02
那个代码段咋不能运行,莫名其妙,那些头文件我明明是包含的,结果还是报错
flock_syn.c:45: undefined reference to `sem_open'
flock_syn.c:51: undefined reference to `sem_unlink'
flock_syn.c:65: undefined reference to `sem_wait'
flock_syn.c:67: undefined reference to `sem_post'
/flock_syn.c:57: undefined reference to `sem_wait'
flock_syn.c:59: undefined reference to `sem_post'
而我的man显示头文件都包含了
作者: CMAX    时间: 2009-12-15 23:01
原帖由 li32768 于 2009-12-10 20:02 发表
那个代码段咋不能运行,莫名其妙,那些头文件我明明是包含的,结果还是报错
flock_syn.c:45: undefined reference to `sem_open'
flock_syn.c:51: undefined reference to `sem_unlink'
flock_syn.c:65: und ...


你用-lpthread,它们都是线程库里实现的
作者: langue    时间: 2009-12-15 23:24
标题: 回复 #10 li32768 的帖子
undefined reference 错误显然是在链接的时候发生的。头文件仅对预处理器有效,正确设置头文件可以通过编译,但仍然需要链接到正确的库。参照 11 楼的提示。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2