很难指出我的细分错误在哪里。一旦我可以找到他们,我就可以解决这个问题。从我的编译中,我能够确定它们的位置。两种功能都在不同的源文件中。我知道seg错误主要处理 NULL 和其他指针,以及在不应该使用时取消引用指针。但是,我看不到任何这些问题。有人可以帮忙吗?
第一个功能
int main(int argc, char *argv[]){
signal(SIGINT, signalHandler);
srand(time(NULL));
int child, nextChild, selectedChild, i, x, sleepTime;
float maxTime; /*Max time for scheduling*/
int shmKey = ftok(".", 40);
int semKey = ftok(".", 41);
int PCB = 0;
float wait = 0; /*Wait time for Child*/
float current = 0;
float currentWaiting = 0; /*Current scheduling*/
/*Initialize Shared Memory*/
shmID = shmget(shmKey, SHMPTRSIZE, 0600 | IPC_CREAT);
if(shmID == -1){
perror("shmget Err"); /*error with shmget*/
exit(1);
}
if(!(ShmPTR = (shmPtr *)(shmat(shmID, 0, 0)))){
perror("shmat Err");
}
logged("Allocated and attached to shared memory\n");
/*Initialization of value in shared memory*/
ShmPTR->seconds = 0;
ShmPTR->nanoseconds = 0;
ShmPTR->wait = 1;
ShmPTR->waitTime = 0;
ShmPTR->forkTime = 0;
ShmPTR->doneTime = 0;
ShmPTR->timeQuantum = QUANTUM;
for(i = 0; i < MAX; pcbTaken[i++] = 0);
sprintf(buff, "Using Time Quantum = %d\n", QUANTUM);
logged(buff);
/*Initialization Semaphore Memory*/
if((semID = semget(semKey, 1, 0600 | IPC_CREAT)) == -1){
sprintf(buff, "OSS: semget");
perror(buff);
exit(1);
}
union semun {
int val;
struct semid_ds *buf;
ushort * array;
} argument;
argument.val = 1;
if(semctl(semID, 0, SETVAL, argument) == 1){
sprintf(buff, "OSS: semctl");
perror(buff);
exit(1);
}
logged("Created Scheduling Semaphore");
/*Forking Children*/
for(i = 0; i < MAX; childFork(i++));
ShmPTR->wait = 0;
while(1){
if(childCount() < MAX){
for(child = 0; child <= MAX; child++){
if((child + PCB) >= MAX)
x = child;
else
x = child + PCB;
if(!pcbTaken[x]){
PCB = x;
selectedChild = x;
break;
}
}
sprintf(buff,"selectedChild child number %d to fork", selectedChild);
logged(buff);
printf("Create new process\n");
childFork(selectedChild);/*Create a new selected process*/
}
semWait(semID);
/*Scheduling Algorithm*/
maxTime = 0;
for(i = 0; i < MAX; i++){
current = (float)ShmPTR->seconds + ((float)ShmPTR->nanoseconds / 1000);
wait = (current - ShmPTR->childPCB[i].lastEx);
/*Update*/
ShmPTR->waitTime = ShmPTR->waitTime + wait;
ShmPTR->cpuTime = ShmPTR->cpuTime + ShmPTR->childPCB[i].cpuTime;
if(ShmPTR->childPCB[i].cpuTime > 0)
currentWaiting = wait / ShmPTR->childPCB[i].cpuTime;
else
currentWaiting = wait;
if(currentWaiting > maxTime){
nextChild = i;
maxTime = currentWaiting;
}
}
sprintf(buff, "Process ID %d", childPID[nextChild]);
logged(buff);
ShmPTR->disPID = childPID[nextChild];
ShmPTR->childPCB[nextChild].run = 0;
/*Update Clock*/
ShmPTR->seconds++;
ShmPTR->nanoseconds = 1 +(rand() % 1000);
sprintf(buff, "Clock is now %d.%04d", ShmPTR->seconds, ShmPTR->nanoseconds);
logged(buff);
printf("%d\n", ShmPTR->seconds);
if(ShmPTR->seconds >= 100){
sprintf(buff,"Minimum run time reached: Exit");
logged(buff);
break;
}
sleepTime = rand() % 2;
sprintf(buff, "Sleep %d", sleepTime);
logged(buff);
sleep(sleepTime);
}
report();
clean(15);
return 0;
}
和 第二功能
void logged(char *message){
char name[10] = "";
sprintf(name, "sim%02d.log", childID);
FILE *fp;
if(!(fp = fopen(name, "a"))){
sprintf(errors, "OSS: sim %02d: opening %s", childID, name);
perror(errors);
exit(1);
}
time_t now;
struct tm *timeinfo;
time(&now);
timeinfo = localtime(&now);
fprintf(fp, "OSS: %02d:%02d:%02d\t", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
fprintf(fp, "OSS: User %02d:\t%s\n", childID, message);
fclose(fp);
}