こんにちは。お時間をいただきありがとうございます。
いくつかのコマンドを実行するプログラムを並列化しようとしていますが、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, ¶llelexecute, 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
進め方がよくわかりません。
また、サポート機能の正確性についてもある程度確信を持っています。
どんな助けでも大歓迎です。