【问题标题】:Trouble with semaphore implementation信号量实现的问题
【发布时间】:2017-03-18 00:39:22
【问题描述】:

我打算编写一个程序,它采用 2 个不同的命令行选项并使用信号量将输出从“First”交替到“Next”。更多细节 -

• 程序使用不同的命令行选项运行两次

progName randNumber fRunSeed "F" & progName randNumber nRunSeed "N" &

• F = 第一次运行,N = 下一次运行
• 每次运行都是为了生成 randNum 数量的随机数,但在每个随机数之后会增加一行。
• 使用信号量在 F 运行和 N 运行之间交替使用 couts。 (F是第一)

输出应包括运行(F 或 N)、进程 ID 和随机数。

到目前为止,我已经编写了头文件,并进行了一些实际的代码处理。但我不知道在哪里实际实现信号量......我确实理解信号量的用途,并且这个程序并没有真正很好地利用它,但它是我打算使用的。

是否有人可以查看我的代码并指导我在正确的方向插入信号量,或者告诉我该怎么做?

感谢大家的帮助——代码如下

我的标题:

    union semun {
        int val;
        struct  semid_ds *buf;
        ushort  *array;
    };

    class Semaphore {
    private:
      const unsigned int SemCount;
      int SemID;

    public:
      Semaphore (key_t key, int howManySemaphoresToCreate);
      void Init (int SemaphoreNumber, int Value);
      int ReadValue (int SemaphoreNumber);

    // Decrease the value of a semaphore, semaphoreNumber, by 1
    void Wait (int SemaphoreNumber);

    // Increase the value of a semaphore, semaphoreNumber, by 1.
    void Signal (int SemaphoreNumber);

    // Remove the semaphore group.
    void Destroy( );
    };  

以及我编写的用于处理数据处理的代码。我不知道在哪里插入信号量,所以现在实际的代码是一个粗略的草图。

    int main(int argc, char *argv[]){

      int firstSeed = atoi(argv[2]);
      int nextSeed = atoi(argv[2]);
      int numRandNums = atoi(argv[1]);

      if(argv[3] == "F"){
        srand(firstSeed);
        for(int i = 0; i < numRandNums; i++){
          cout << "F: " << "ID: " << getpid() << " Num: " << rand() << endl;
        }
      }
      else if(argv[3] == "N"){
        srand(nextSeed);
        for(int j = 0; j < numRandNums; j++){
         cout << "N: " << "ID: " << getpid() << " Num: " << rand() << endl;
        }
      }
     }  

我知道这个网站不是为了让人们为你完成你的工作,但在这一点上我不了解信号量的实际实现,虽然我已经阅读了很多关于它们的信息,但我找不到与我需要做才能真正看到相似之处。另外,我不认为getpid() 是获取信号量进程 ID 的正确方法,但正如我所说,现在只是一个粗略的草稿。

感谢任何可以帮助我的人。

【问题讨论】:

  • 在此上下文中将信号量视为从进程传递到进程的令牌,以象征“我拥有输出流”。在这种情况下,每个进程都必须生成并刷新输出,然后安全地遵循某种协议,通过该协议将令牌传递给另一个进程。您应该在生成每个输出行之前 Acquire() 或 unique_lock 信号量,并在您的进程使用输出流之后 Release() 它(或销毁 unique_lock)。
  • @ArtYerkes 对不起,我在这里的无知,但就我的代码而言,假设我想创建信号量,所以我做了 Semaphore semaphore(101, 2)。那应该继续并创建2个信号量。这是我需要的吗?或者一个就足够了?我写的代码在哪里适合(不包括头文件)?
  • 我读到的是你只需要 1 个信号量。显然,您需要先创建一个信号量对象才能使用它:-)

标签: c++ unix posix semaphore


【解决方案1】:

这是我认为您可以在代码中添加信号量的方法-

int main(int argc, char *argv[]){

  //two semaphores to check if it is time to ru==do first run or next run
  Semaphore firstRun=0,nextRun=0;


  int firstSeed = atoi(argv[2]);
  int nextSeed = atoi(argv[2]);
  int numRandNums = atoi(argv[1]);

  //Now the program is ready for first run
  firstRun.signal();

  //the following code is only executed when we have signal in firstRun so the wait function is used
  firstRun.wait();

  if(argv[3] == "F"){
    srand(firstSeed);
    for(int i = 0; i < numRandNums; i++){
      cout << "F: " << "ID: " << getpid() << " Num: " << rand() << endl;
    }

   //First Run has been done so generate signal for second
   nextRun.signal();
  }


  //similarly a wait on nextRun to ensure it executes after firt run is finished
  nextRun.wait();

  else if(argv[3] == "N"){
    srand(nextSeed);
    for(int j = 0; j < numRandNums; j++){
     cout << "N: " << "ID: " << getpid() << " Num: " << rand() << endl;
    }
  }
 }  

或者您只能使用一个信号量 nextRun,因为我认为 firstRun 在我的代码中的使用是微不足道的。

【讨论】:

    猜你喜欢
    • 2011-08-25
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多