我有以下代码
typedef struct
{
char word[64];
} DataStructure;
sigset_t set;
FILE *pFile;
static void *print(void *ptr)
{
char *message;
message = (char *)ptr;
int sig;
while(!feof(pFile))
{
sigwait(&set, &sig);
printf("%s \n", message);
sleep(2);
}
return 0;
}
int main(int argc, char *argv[])
{
DataStructure Data;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
pthread_sigmask(SIG_BLOCK, &set, NULL);
pthread_t thread;
pthread_create(&thread, NULL, print, (void *)Data.word);
pFile = fopen("rhyme1.txt", "r");
while(!feof(pFile))
{
fscanf(pFile, "%s", Data.word);
pthread_kill(thread, SIGUSR1);
}
pthread_join(thread, NULL);
pthread_exit(NULL);
}
/*The output should look like:
Hickory,
dickory,
dock,
The
mouse
ran
up
the
clock.
This code results in:
clock. */
我正在尝试打开一个文本文件并使用 scanf 读取该行的每个单词。然后该词被发送到“打印”方法(通过 pthread_create 的参数)并在发送信号时打印读入的词(通过 pthread_kill)。该过程一直重复,直到文件被完全读入并打印出来。