免费注册 查看新帖 |

Chinaunix

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

嵌入式 LINUX 中 system() 函数 阻塞的问题。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-07-15 11:14 |只看该作者 |倒序浏览
我在一个 arm 9 的板子上 执行了一个 system(“ ls -l ”)函数.

反复的运行多次,就发现 会阻塞在 这个函数, 没有返回了。

后来我把我使用的 uClibc-0_9_28 库里的 system 函数弄出来了。 最后发现是 , 在阻塞的时候 if ((pid = vfork()) ==0 )  不执行。

导致 父进程在 死等 子进程的结束。   

这里我有个很大的疑问,  为什么 fork() 不返回 0 , 子进程一直得不到调度吗???????

百思不得其解,  恳求 各位大侠有遇到这样问题的吗?  请指点下。  谢谢!

#include <stdio.h>
#include <stddef.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>

/* uClinux-2.0 has vfork, but Linux 2.0 doesn't */
#include <sys/syscall.h>
#if ! defined __NR_vfork
#define vfork fork       
#endif

int __libc_system(char *command)
{
        int wait_val, pid;
        __sighandler_t save_quit, save_int, save_chld;

        if (command == 0)
                return 1;

        save_quit = signal(SIGQUIT, SIG_IGN);
        save_int = signal(SIGINT, SIG_IGN);
        save_chld = signal(SIGCHLD, SIG_DFL);

        if ((pid = vfork()) < 0) {
                signal(SIGQUIT, save_quit);
                signal(SIGINT, save_int);
                signal(SIGCHLD, save_chld);
                return -1;
        }
        if (pid == 0) {
                signal(SIGQUIT, SIG_DFL);
                signal(SIGINT, SIG_DFL);
                signal(SIGCHLD, SIG_DFL);

                execl("/bin/sh", "sh", "-c", command, (char *) 0);
                _exit(127);
        }
        /* Signals are not absolutly guarenteed with vfork */
        signal(SIGQUIT, SIG_IGN);
        signal(SIGINT, SIG_IGN);

#if 0
        printf("Waiting for child %d\n", pid);
#endif

        if (wait4(pid, &wait_val, 0, 0) == -1)
                wait_val = -1;

        signal(SIGQUIT, save_quit);
        signal(SIGINT, save_int);
        signal(SIGCHLD, save_chld);
        return wait_val;
}
weak_alias(__libc_system, system)

论坛徽章:
0
2 [报告]
发表于 2010-07-15 12:59 |只看该作者
fork() 出来的进程与父进程运行无关,但 要是vfork() 那父进程会一直等待子进程退出

论坛徽章:
0
3 [报告]
发表于 2010-07-15 14:07 |只看该作者
本帖最后由 duanjigang 于 2010-07-15 14:11 编辑

  1. vfork() is a special case of clone(2).  It is used to create new processes without
  2.        copying the page tables of the parent process.  It may be  useful  in  performance
  3.        sensitive applications where a child will be created which then immediately issues
  4.        an execve().

  5.        vfork() differs from fork() in that the parent is suspended until the child  makes
  6.        a  call  to  execve(2)  or _exit(2).  The child shares all memory with its parent,
  7.        including the stack, until execve() is issued by the child.  The  child  must  not
  8.        return from the current function or call exit(), but may call _exit().
复制代码
参考下man的信息
一般情况下这样简单调用没什么异常现象,fork返回在子进程肯定是0,不知道跟平台有无关系,LZ解决问题没?

论坛徽章:
0
4 [报告]
发表于 2010-07-15 20:52 |只看该作者
问题还是没有搞定,

我自己编了个 hello world 程序, 编译成可执行的文件 hello , 拷贝到 /sbin 下,

然后循环的 调用 system(" hello ");            sleep( 2 );

有时候运行了 几百次就 阻塞在 system()   函数里了。 不再往下执行。

有时候是 几千次 才塞在那里。  然后发现还是 上面的代码中    if (pid == 0) {  没有执行进去。

停留在   if (wait4(pid, &wait_val, 0, 0) == -1)

这里, 再也出不来了。

真是搞不定啊, 请高手指点下。  谢谢啊。

论坛徽章:
0
5 [报告]
发表于 2010-07-15 20:54 |只看该作者
我的板子的运行环境是:

LINUX2.6.14,    C 库是   uClibc-0_9_28 。

我的系统中有多个线程, 其中有几个线程 需要动态的 不断 申请 和 释放 内存。

跟这个有没有关系呢?

论坛徽章:
84
每日论坛发贴之星
日期:2015-12-29 06:20:00每日论坛发贴之星
日期:2016-01-16 06:20:00每周论坛发贴之星
日期:2016-01-17 22:22:00程序设计版块每日发帖之星
日期:2016-01-20 06:20:00每日论坛发贴之星
日期:2016-01-20 06:20:00程序设计版块每日发帖之星
日期:2016-01-21 06:20:00每日论坛发贴之星
日期:2016-01-21 06:20:00程序设计版块每日发帖之星
日期:2016-01-23 06:20:00程序设计版块每日发帖之星
日期:2016-01-31 06:20:00数据库技术版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-14 06:20:00
6 [报告]
发表于 2010-07-22 18:12 |只看该作者
系统或库的bug吧
    内核 arm相关的补丁都打了吗? http://www.arm.linux.org.uk/

论坛徽章:
0
7 [报告]
发表于 2010-07-22 22:35 |只看该作者
支持楼上的说法,在普通机器上应该问题吧,那个程序?
还是检查你的软硬件环境,补丁是否都打上了

论坛徽章:
0
8 [报告]
发表于 2010-07-23 21:44 |只看该作者
fork() 不返回 0,说明fork错误

论坛徽章:
84
每日论坛发贴之星
日期:2015-12-29 06:20:00每日论坛发贴之星
日期:2016-01-16 06:20:00每周论坛发贴之星
日期:2016-01-17 22:22:00程序设计版块每日发帖之星
日期:2016-01-20 06:20:00每日论坛发贴之星
日期:2016-01-20 06:20:00程序设计版块每日发帖之星
日期:2016-01-21 06:20:00每日论坛发贴之星
日期:2016-01-21 06:20:00程序设计版块每日发帖之星
日期:2016-01-23 06:20:00程序设计版块每日发帖之星
日期:2016-01-31 06:20:00数据库技术版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-14 06:20:00
9 [报告]
发表于 2010-07-23 23:01 |只看该作者
fork() 不返回 0,说明fork错误
0vk0 发表于 2010-07-23 21:44



< 0 才是错误
在父进程上下文中返回的是pid
在子进程上下文中返回的是0

论坛徽章:
0
10 [报告]
发表于 2010-07-24 11:26 |只看该作者
< 0 才是错误
在父进程上下文中返回的是pid
在子进程上下文中返回的是0
yjh777 发表于 2010-07-23 23:01



    如果子进程返回一个大于0的值,你觉得正确么
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP