0
usleep()에 대한 rondom 시드 생성기로 사용할 thread_mutex 초기화 함수에 전달할 숫자가 있어야합니다. 난 그게 무작위 수의 씨앗을 만드는 것이 무슨 뜻인지 모르겠다. srand()를 사용할 때 문제는 코드를 넣는 것입니다. for 루프에 srand(timeDelay)
을 지정하면 rand()가 항상 동일하게됩니다. 직접 지시 말 :어떻게 pthread 함수 usleep()에 대한 for 루프에서 임의의 시드 생성기를 사용합니까?
This action happens repetitively, until its position is at FINISH_LINE:
// Randomly calculate a waiting period, no mare than MAX_TIME (rand(3))
// Sleep for that length of time.
// Change the display position of this racer by +1 column*:
// Erase the racer's name from the display.
// Update the racer's dist field by +1.
// Display the racer's name at the new position.
코드
int main(int argc, char *argv[]) {
...
long int setDelay = strtol(argv[1], &pEnd, 10);
if (setDelay == 0) {
//printf("Conversion failed\n");
racersNumber = argc-1;
setDelay = 3;
...
} else {
//printf("Conversion SSS\n");
racersNumber = argc-2;
}
...
initRacers(setDelay);
for (idx = 0; idx < racersNumber; idx++) {
printf("Thread created %s\n", racersList[idx]);
//make a racer arra or do I add a pthread make function?
rc = pthread_create(&threads[idx], NULL, run, (void*) makeRacer(racersList[idx], idx+1));
...
코드 에 대한 초기화 레이서는 "임의 씨"실행에 대한이
static pthread_mutex_t mutex1;
static long int timeDelay;
void initRacers(long distance) {
printf("Setting the time delay\n");
//set the mutex
pthread_mutex_init(&mutex1, NULL);
timeDelay = distance;
}
코드를 생성 할 수 기능
#define MAX_TIME 200 // msec
void *run(void *racer){
//pre-cond: racer is not NULL
if (racer == NULL) {
return -1;
}
//convert racer back to racer
for (int col = 0; col < FINISH_LINE; col++) {
Racer *rc;
//conversion
rc= (Racer*)racer;
pthread_mutex_lock(&mutex1);
if (col != 0) {
set_cur_pos(rc->row, col-1);
put(' ');
}
set_cur_pos(rc->row, col);
pthread_mutex_unlock(&mutex1);
//loop function for each at same column
for (int idx = 0; idx < sizeof(rc->graphic); idx++) {
put(rc->graphic[idx]);
}
//srand(timeDelay);
usleep(100000L*(rand()%timeDelay));
}
}
"* for 루프 *"에 코드 :'srand (timeDelay)'넣기. 'srand()'를 루프에 넣지 마십시오. 프로그램의 처음에 한 번, 일반적으로 시간 인수를 사용하여 호출하기 때문에 프로그램을 실행할 때마다'rand()'에서 얻는 임의의 시퀀스가 다릅니다. –
그게 도움이. 이것이 srand ([int only])의 오류 검사 및 매개 변수를 제외하고는 문제의 범위였습니다. 곧 답변을 다시 게시 할 수 있습니까? –