0

こんにちは。お時間をいただきありがとうございます。

いくつかのコマンドを実行するプログラムを並列化しようとしていますが、pthreads が良い選択肢になると思いました。

しかし、私はいくつかの問題に直面しています。

ここからスレッドを開始します。

void timetravel(command_stream_t s)
{

  int *retvals[MAXTHREADS];
  if (s == NULL)
    return;
  if (s->num_commands == 0)
    return;
  int err;
  global_table = create_dependency_table(s);
  //global_command = &s;
  global_command = s;
  int fill = 0;

  for (fill = 0; fill < s->num_commands; fill++)
  {

    global_table.status_table[fill] = 1; //Set all commands to waiting
    // printf("global_table.status_table[i] : %d \n", global_table.status_table[fill]);

  }

  int finished = 0;
  while (finished == 0)

  {
    finished++;
    int threadindex = 0;
    for (threadindex = 0; threadindex < MAXTHREADS; threadindex++)
    {
      err = pthread_create(&(tid[threadindex]), NULL, &parallelexecute, NULL);
      if (err != 0)
        printf("\ncan't create thread :[%s]", strerror(err));
      else
        printf("\n Thread created successfully\n");
    }

    for (threadindex = 0; threadindex < MAXTHREADS; threadindex++)
    {
      pthread_join(tid[threadindex], NULL);
    }

    if (completecheck(global_table) == 0)
    {
      finished = 1;
    }
  }
  //  print_dependency_table(global_table);
  //print_command(global_command->command_array[1]);
}

依存関係テーブルはそのまま保存されます

*** DEPENDENCY TABLE ***
 ~x~  ~meh~  ~hello~  ~goodbye~  ~phi~  ~a~  ~gamma~  ~delta~  ~b~  ~c~  ~d~  ~e~  ~f~  ~g~
 1  1  0  0  0  0  0  0  0  0  0  0  0  0
 0  0  1  0  0  0  0  0  0  0  0  0  0  0
 0  0  1  1  1  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  1  1  1  0  0  0  0  0  0
 0  0  0  0  0  0  0  0  1  1  1  1  1  1

コマンド用

cat x meh
echo hello
echo -s hello goodbye > phi
touch a < gamma > delta
touch -rx b c d e f g

コマンド 2 と 3 で「hello」が使用されているため、3 は 2 に依存しているため、次のようになります。

 ~x~  ~meh~  ~hello~  ~goodbye~  ~phi~  ~a~  ~gamma~  ~delta~  ~b~  ~c~  ~d~  ~e~  ~f~  ~g~
 0  0  1  0  0  0  0  0  0  0  0  0  0  0
 0  0  1  1  1  0  0  0  0  0  0  0  0  0

したがって、2 の前に 3 を実行しません。

2 が実行された後、その行を 0 に設定するため、3 はそれに依存しなくなります

書き込み/書き込みの競合がないため、いかなる種類のブロッキングも実装していません。

書き込みの前に読み取りが行われる競合状態が発生する可能性がありますが、スレッドの実行を遅らせるだけなので問題ありません。

これは pthreads プログラムです:

void* parallelexecute(void *arg)
{
  //printf("gets to parallel execute stage\n");
  int i;
  //printf("global_table.num_cmds_rows : %d \n",global_table.num_cmds_rows);
  for (i = 0; i < global_table.num_cmds_rows; i++)
  {

    // status 1 = runnable, status 2 = running
    //status 0 = completed successfully, status -1 = unsuccessful
    //printf("global_table.status_table[i] : %d \n",global_table.status_table[i]);

    if (global_table.status_table[i] == 1
        && (check_nth_command(&global_table, i)) == 0)
    {
      global_table.status_table[i] = 2;
      execute_command(global_command->command_array[i], 0);
      printf("execution triggered");
      completed_nth_command(&global_table, i, 0);
      break;
    }
  }
  return NULL;

}

これらは私のグローバル変数です

#define MAXTHREADS 2
pthread_t tid[MAXTHREADS];
 //global variable for dependency pool
parallel_data global_table;
 //global variable for commands
command_stream_t global_command;

global_tableしかし、からアクセスしようとするとparallelexecute、あらゆる種類の奇妙な値が得られ、その理由がわかりません。

したがって、「グローバルテーブル」は構造体です。

struct parallel_data
  {
    int** dependency_table;  // the main dependency table
    char** file_reference_key;  // find index by name (use strcmp in a loop)
    int* status_table;  // you know you are done when all the statuses are 0 (none should ever be -1)
    int num_files_cols;  // number of columns/files
    int num_cmds_rows;  // number of rows/commands 
  };

そして、各スレッドは の行dependency tableと の行にのみ書き込みます。status_table

進め方がよくわかりません。

また、サポート機能の正確性についてもある程度確信を持っています。

どんな助けでも大歓迎です。

4

2 に答える 2

1

私はまだあなたのコードを読んでいませんが、「書き込み/書き込みの競合がないため、いかなる種類のブロッキングも実装していません」を見ました。これは私にとって重大な危険信号です。読み取り/書き込みの競合がある場合は、... まあ... 競合があります。それはおそらくバグに関連しています。有効なのは、std::atomic変数またはvolatile変数があり、CTRL + F をすばやく押してもどちらもないと表示される場合のみです。コードはおそらくまったく同期していないため、スレッドは他のスレッドが書き込んでいる値を見ること はありません。

于 2013-07-23T19:48:57.377 に答える
0

pthreads スレッド関数に引数を渡す私のスタイルが間違っていることがわかりました。コードに行った編集をすぐに投稿します。

ありがとう@mooing-duck、@wilx

于 2013-07-31T06:48:47.107 に答える