C++ getline(string) 与多线程有关的段错误

标签 c++ c multithreading shell fork

对于这个有点含糊的标题,我深表歉意,但我真的想不出更好的写法。

我正在为一个应该能够处理后台的类设计一个 shell 。但是,由于启用后台时 shell 不会等待子进程,因此我认为最好创建第二个线程,其唯一目的是在子进程完成时清理子进程。现在,这可以正常工作,直到子进程结束。当子进程结束时,我输入的下一个输入会出现段错误。使用 gdb,我发现错误来 self 的 getline 函数。

我已经测试过了,std::cin 似乎不是问题。另外,当没有后台时,整个事情运行良好,没有任何问题。

为了测试后台,我编写了一个程序 a.out,它会打印、休眠十秒钟,然后在退出之前再次打印。 输出:

/Users/opname/Shell Project>./a.out&
/Users/opname/Shell Project>Sleep time!
ls
opshell   a.out     pseudocode.c    test.txt
opshell.cpp a.out.dSYM  shell.h     test2.txt
opshell.dSYM    gcctest.c   sleeper.c   testdir
/Users/opname/Shell Project>Awake time!
ls
Segmentation fault: 11

这是我 fork 进程并创建新线程的代码:

            pid_t cpid = fork();
            if (cpid > 0) {
                int status;
                if (conc) { // if backgrounding enabled
                    pthread_t waiter;
                    struct tcpara *pm = (struct tcpara*) malloc(sizeof(struct tcpara));
                    pm->cpid = cpid;
                    pm->status = &status;
                    pthread_create(&waiter, NULL, &thread_handler, (void*)pm);
                    pthread_detach(waiter);
                }
                else waitpid(cpid, &status, 0);
                break;

            } else if (cpid == 0) {
                loc += cmd;
                execv(loc.c_str(), argv);
                _exit(0);
            }

这是服务员线程调用的子例程:

void *thread_handler(void *pm) {
  struct tcpara *ofpm = (struct tcpara*) pm;
  waitpid(ofpm->cpid, ofpm->status, 0);
  free(pm);
  return NULL;
}

最佳答案

尝试动态分配状态变量。当线程必须使用它时,它就不再存在,因为它超出了范围。 我认为 waiter 的情况也是如此。 您可以尝试将它们设为静态。

关于C++ getline(string) 与多线程有关的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26149425/

相关文章:

c++ - 在 C++ 中,如何从函数返回对象数组?

c++ - 如何访问 friend 类方法?

c - c : Function which deletes every second element of linked list 中的指针

python - FastAPI 与 pandas.read_sql() 的并行性

c++ - 着色玩具到 SFML

C++解析比特流

c - opencv 函数 cvCreateMatHeader 中的非正宽度或高度

c - 多维数组不保存值

python:如果键不在字典中,则添加键。防止多线程竞争条件

java - 在调用notifyAll之前获取所有等待对象的线程