这个问题难啊,需要亲自调试,应该是PV操作的问题,
建议:仔细查一下PV操作的顺序。

解决方案 »

  1.   

    我觉得THREAD1并不一定需要,可由FTP进程直接写FILELIST,至于子进程WAIT你是否用SIGCHLD信号控制比较方便
      

  2.   

    终于,有人回答了!一定给分!
    FILELIST不是共享内存,它只在Main进程中可见。
    SIGCHLD是解决有进程wait不到的方法,但一定要wait,否则会造成很多僵尸进程的存在。
    谁对pipe 或线程同步熟悉?!
    帮忙看看!
      

  3.   

    >thread1不知道为什么block在read pipe,尽管ftp process 还在write pipe?不可能吧,PIPE的读端没有读出,写端怎么可能不停的写呢,PIPE的容量是多少?
    对FILELIST与PROCESS_COUNTER操作的时候加锁了吗
    把代码帖出来看看.
      

  4.   

    先说filelist:
    int file_num=0;
    RWTPtrSlist<RWCString> filelist;
    //是用Rogue Wave的库,方法有insert(), get()...认为是数组吧.
    mutex_t filelist_mutex;
    cond_t filelist_cond; //fork ftp process
    if (mutex_init(&filelist_mutex, USYNC_THREAD, NULL)!= 0) {
    ...
    }
    //and init all mutex and cond
    //creat 2 thread
    ...
    //in readpipe thread
    if (mutex_lock(&filelist_mutex)!=0) { 
    ...
    }
    filelist.insert(...);
    file_num++;
    if (cond_signal(&filelist_cond)!=0) {
    ...
    }
    if (mutex_unlock(&filelist_mutex)!=0) {
    ...
    }//in Main
    if (mutex_lock(&filelist_mutex)!=0) { 
    ...
    }
    while (file_num==0) {
    thr_result = cond_wait(&filelist_cond, &filelist_mutex);
    if (thr_result!=0) {
    if (thr_result!=EINTR) {
    exit(0);
    }
    else {
    continue;
    }
    }
    }
    filename=filelist.get(); //remove one file name from filelist
    file_num--;
    if (mutex_unlock(&filelist_mutex)!=0) {
    ...
    }
    //then fork1 deal file process to deal filename
      

  5.   

    我已经去掉waitpid thread, 改为在注册的signal(SIGCHLD)处理函数sig_chld中waitpid, 以防止有进程wait不到
    mutex_t counter_mutex;
    cond_t counter_cond; //fork ftp process
    if(signal(SIGCHLD,sig_chld)==SIG_ERR){
    ...
    }
    if (mutex_init(&counter_mutex, USYNC_THREAD, NULL)!= 0) {
    ...
    }
    //init all mutex and cond//creat read pipe thread//in Main while(1) loop and after get filename
    if ( (pid_sql = fork1()) < 0) {
    ...
    }
    else if (pid_sql > 0) { /* parent */
    if(mutex_lock(&counter_mutex)!= 0) {
    ...
    }
    while (process_counter >=max_file_num) {
    //max_file_num=30
    thr_result = cond_wait(&counter_cond, &counter_mutex);
    if (thr_result!=0) {
    if (thr_result!=EINTR) {
    kill(pid_ftp, SIGUSR1);
    exit(0);
    }
    else
    continue;
    }
    }
    }
    process_counter++;
    if (mutex_unlock(&counter_mutex)!=0) { 
    ...
    }
    }
    //pid_sql==0 child ...
    -----------------------------------------------------------------
    void sig_chld(int signo){
    signal(SIGCHLD,sig_chld);
    int status; pid_t pid_son;
    if(signo==SIGCHLD){
    if(mutex_lock(&counter_mutex)!= 0) {
    kill(pid_ftp, SIGUSR1);
    exit(1);
    }
    if(process_counter>0){//目前测试从未<0 
    //ftp 没计算在process_counter中,如ftp exit,有可能(process_counter--)<0
    process_counter--;
    }
    else{
    ...
    }
    if (cond_signal(&counter_cond)!=0) {
    kill(pid_ftp, SIGUSR1);
    exit(1);
    }
    if (mutex_unlock(&counter_mutex)!=0) {
    kill(pid_ftp, SIGUSR1);
    exit(1);
    }
    if ((pid_son=waitpid(-1,&status,WNOHANG)) > 0) {
    if (pid_son == pid_ftp) { //ftp process exit and should exit!
    kill(pid_ftp, SIGUSR1);
    exit(1);
    }
    }
    else{
    ...
    }
    }
    return;
    }
      

  6.   

    pipe问题的确很奇怪,我还没有找到原因.
    pipe最大4*1024 or 2*1024
      

  7.   

    建议去掉thread1,没有一点用,还复杂了。在主线程中read pipe 即可。感觉上read pipe 不会有问题。如果实在没办法,不如用更可靠的通信机制,如socket?