输入到数组中的值是threadRun()
也应打印的正确值。由于某种原因,我在第二个循环中使用threadID(tid
)和startTime
/ lifeTime
获得了奇怪的字符,这与我放入数组的内容不一致。
int readFile(char *fileName, Thread **threads) //use this method in a suitable way to read file
{
FILE *in = fopen(fileName, "r");
if (!in) {
printf("Child A: Error in opening input file...exiting with error code -1\n");
return -1;
}
struct stat st;
fstat(fileno(in), &st);
char *fileContent = (char *)malloc(((int)st.st_size + 1) * sizeof(char));
fileContent[0] = '\0';
while (!feof(in)) {
char line[100];
if (fgets(line, 100, in) != NULL) {
strncat(fileContent, line, strlen(line));
}
}
fclose(in);
char *command = NULL;
int threadCount = 0;
char *fileCopy = (char *)malloc((strlen(fileContent) + 1) * sizeof(char));
strcpy(fileCopy, fileContent);
command = strtok(fileCopy, "\r\n");
while (command != NULL) {
threadCount++;
command = strtok(NULL,"\r\n");
}
*threads = (Thread *)malloc(sizeof(Thread) * threadCount);
char *lines[threadCount];
command = NULL;
int i = 0;
command = strtok(fileContent, "\r\n");
while (command != NULL) {
lines[i] = malloc(sizeof(command) * sizeof(char));
strcpy(lines[i],command);
i++;
command = strtok(NULL,"\r\n");
}
for (int k = 0; k < threadCount; k++) {
char *token = NULL;
int j = 0;
token = strtok(lines[k], ";");
while (token != NULL) {
//this loop tokenizes each line of input file
//write your code here to populate instances of Thread to build a collection
if (j == 0) {
printf("id of thread: %s\n", token);
strcpy((*threads)[k].tid, token);
token = strtok(NULL, ";");
j++;
} else
if (j == 1) {
printf("startTime of thread %s: %s\n", (*threads)[k].tid, token);
(*threads)[k].startTime = atoi(token);
token = strtok(NULL, ";");
j++;
} else
if (j == 2) {
printf("lifeTime of thread %s: %d\n\n", (*threads)[k].tid, atoi(token));
(*threads)[k].lifeTime = atoi(token);
token = strtok(NULL, ";");
j++;
token = strtok(NULL, ";");
} else
if (j == 3) {
j = 0;
}
}
}
return threadCount;
}
readfile()
-接收threads“;”的txt文件以信息定界:iD;startTime;lifeTime
。输入此信息以创建一个结构线程数组,并返回那里的线程数(相当于行数)
typedef struct thread { //represents a single thread
char tid[5];//id of the thread as read from file
int startTime;
int lifeTime;
} Thread;
int main(int argc, char *argv[]) {
Thread **threads = (Thread**)malloc(sizeof(Thread*));
int threadCount = readFile(argv[1], threads); //returns number of threads and populates list
int i = 0;
startClock();
while (i < threadCount) { //run for ea. thread
sleep((*threads)[i].startTime);
getCurrentTime();
//=============FIX OUTPUT BELOW===================
pthread_t activeThread;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&activeThread, &attr, threadRun, threads[i]);
pthread_join(activeThread, NULL);
i++;
}
return 0;
}
void *threadRun(void *t) { //implement this function in a suitable way
Thread *currThread = (Thread *)t;
int life;
int start;
start = currThread->startTime;
life = currThread->lifeTime;
logStart(currThread->tid);
printf("startTime: %d\n", currThread->startTime);
printf("lifetime: %d\n", currThread->lifeTime);
printf("tid: %s\n", currThread->tid);
logFinish(currThread->tid);
pthread_exit(0);
}
原来问题出在我的读取文件fnc中,我为数组中的第一个结构分配了内存空间,但没有为其他结构分配内存!