我正在编写一个创建两个线程的程序 . 每个线程负责读取一个文本文件,每行有一个字符 .
第一种格式如下:
h
0
h
0
...
第二个格式如下:
0
i
0
i
0
i
有时可能会有多个字母在彼此之后,或者在彼此之后有多个零 . 但是,可以肯定的是,如果一个文件的一行上有一个字母,则第二个文件的相应行将为0,反之亦然 .
线程应该继续将文件输入读取到全局char数组中,直到它们达到零 . 此时,它们允许其他线程接管 . 他们一直来回走,直到两个文件完全被读取 .
在这一点上,当我跑步时,我得到的变化是(1)很多h 's followed by many i' s或(2)( the correct answer )连续的hihihi 's, or (3) sometimes many i'流然后是许多h . 所以,我知道我的同步方法已关闭 .
这是我的一个线程的示例:(注意两个线程完全相同,除了正在打开的文件 . )
void *getMessage1()
{
FILE *studentOne = fopen("Student1", "r");
size_t howManyChars;
char *placeHolderChars;
int count = 1;
while (count < 501)
{
placeHolderChars = NULL;
getline(&placeHolderChars, &howManyChars, studentOne);
if(strcmp(placeHolderChars, "0\n") == 0) //if we've reached a zero
{
pthread_mutex_unlock(&lock);
}
else
{ while(1)
{
if(pthread_mutex_trylock(&lock) == 0)
{
break;
}
}
if(strlen(placeHolderChars)>0)
{
placeHolderChars[1] = '\0';
}
strcat(message,placeHolderChars);
}
free(placeHolderChars);
if(feof(studentOne))
{
pthread_mutex_unlock(&lock); //unlock
fclose(studentOne);
break;
}
count++;
}
return 0;
}
这是我的主要方法:
int main(void)
{
pthread_t id1;
pthread_t id2;
pthread_create((&id1), NULL, getMessage1, NULL);
pthread_create((&id2), NULL, getMessage2, NULL);
pthread_join(id1, NULL);
pthread_join(id2, NULL);
int j;
for (j = 0; j < 1001; j++)
{
printf ("%c ",message[j]);
}
return 0;
}
I would appreciate any guidance on how I can better use lock, unlock, wait, and/or signal to create a working synchronization technique with consistent results.
1 回答
这是一个尝试做你想要的程序 . 但是,测试不足;)