通过gnome-terminal打开终端然后执行命令,错误:“无法执行子进程”

时间:2018-02-05 17:16:59

标签: bash gnome-terminal

通过bash命令,我想打开一个终端,并从新终端执行一个简单的bash命令。

我尝试:

#include <iostream>
#include <thread>
#include <mutex>

#include <pthread.h>

std::mutex stmutex;

pthread_mutex_t pthrmutex = PTHREAD_MUTEX_INITIALIZER;

void thread1(int i)
{
  stmutex.unlock();
  std::cout << "Un Locked in thread 1" <<  std::this_thread::get_id() << std::endl;
}

void pthread1(int i)
{
  pthread_mutex_unlock(&pthrmutex);
  std::cout << "Un Locked Pthread mutex in thread 1" <<  std::this_thread::get_id() << std::endl;
}

void thread2(int i)
{
  stmutex.lock();
  std::cout << "Locked in thread 2" <<  std::this_thread::get_id() << std::endl;
}

void thread3(int i)
{
  stmutex.unlock();
  std::cout << "UNLocked in thread 3" <<  std::this_thread::get_id() << std::endl;
}

int main(int argc, char **argv)
{
  try {
   stmutex.lock();
   std::cout << "Locked in main thread : " << std::this_thread::get_id() << std::endl;
   std::thread t1(thread1,1);
   t1.join(); 
   stmutex.lock();
   std::cout << "Locked in main thread after unlocking in thread1" << std::endl;
   stmutex.unlock();
   std::cout << "Un Locked in main thread " << std::endl;


   pthread_mutex_lock(&pthrmutex);
   std::cout << "Locked pthread mutex in main thread : " << std::this_thread::get_id() << std::endl;
   std::thread t2(pthread1,1);
   t2.join(); 
   pthread_mutex_lock(&pthrmutex);
   std::cout << "Locked Pthread mutext in main thread after unlocking in thread1" << std::endl;
   pthread_mutex_unlock(&pthrmutex);
   std::cout << "Un Locked Pthread mutext in main thread " << std::endl;

   std::thread t3(thread2,1);
   t3.join(); 
   std::thread t4(thread3,1);
   t4.join(); 

  } catch (std::exception& ex)
  {
    std::cerr << "Exception In main thread: " << ex.what() << std::endl;
  }
}

但是我收到了这个错误:

enter image description here

我不明白错误,我找不到任何帮助我的例子..

1 个答案:

答案 0 :(得分:7)

引号告诉终端在/bin中运行一个名为bash -c ls的可执行文件(空格作为其名称的一部分!)。没有这样的可执行文件。

把它们拿出来:

gnome-terminal -- /bin/bash -c ls

...或者,在用户提供输入之前实际让某些东西保持打开状态......

gnome-terminal -- /bin/bash -c 'ls; read'