2017-03-18 55 views
0

我的意思是編寫一個程序,該程序需要2個不同的命令行選項,並使用信號量將輸出從「第一個」切換到「下一個」。更詳細 -信號量執行的問題

•程序與不同的命令行選項

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

•F =首先運行,N =下次運行
•每個運行是指來產生隨機數的randNum量,但COUTS運行兩次每個隨機數之後的一行。
•使用信號量在F運行和N運行之間交替使用cout。 (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的正確方法,但正如我所說的,現在只是一個粗略的草案。

謝謝任何​​能夠幫助我的人。

+0

在這個上下文中將信號量想象成一個從進程到進程傳遞的標記,以便現在象徵「我擁有輸出流」。在這種情況下,每個進程必須產生並刷新輸出,然後安全地遵循一些將令牌傳遞到另一個進程的協議。您應該在生成每個輸出行之前獲取()或unique_lock信號量,並且在您的進程使用輸出流之後釋放()它(或銷燬unique_lock)。 –

+0

@ArtYerkes對不起,在這裏我的無知,但在條款或我的代碼,讓我們說我想創建信號量,所以我做信號量信號量(101,2)。這應該會變成並創建2個信號量。那是我需要的嗎?或者一個足夠?我寫的代碼適合哪裏(不包括頭文件)? – thanksForHelp

+0

我的閱讀是,你只需要1個信號量。顯然你需要創建一個信號量對象,然後才能使用它:-) –

回答

0

以下是我認爲你可以做你的代碼 -

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是在我的代碼微不足道。