I am writing a multiprocessing C program in Linux using message queues and signals. and sometimes the following error pops up in the terminal.
error:
scheduler.out: malloc.c:2617: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize – 1)) == 0)’ failed.
code:
Process* stringtoProcess(char* str) {
Process* p = malloc(sizeof(Process));
sscanf(str, "%d %d %d %d %d %d %d %s", &p->id, &p->arrival, &p->startTime, &p->runtime, &p->priority, &p->WaitingTime, &p->remainingTime, p->state);
return p;
}
void receiveProcess() {
key_t key_up, key_down;
int msgid_up, msgid_down;
struct msgbuf buffer_up, buffer_down;
key_up = ftok("keyfile", 'A');
key_down = ftok("keyfile", 'Z');
if (key_up == -1 || key_down == -1) {
perror("ftok");
exit(EXIT_FAILURE);
}
msgid_up = msgget(key_up, 0666 | IPC_CREAT);
msgid_down = msgget(key_down, 0666 | IPC_CREAT);
if (msgid_up == -1 || msgid_down == -1) {
perror("msgget");
exit(EXIT_FAILURE);
}
memset(&buffer_up, 0, sizeof(buffer_up));
memset(&buffer_down, 0, sizeof(buffer_down));
if (msgrcv(msgid_up, &buffer_up, sizeof(buffer_up.mtext), 5, !IPC_NOWAIT) == -1) {
perror("msgrcv");
exit(EXIT_FAILURE);
}
Process *p = stringtoProcess(buffer_up.mtext);
TotalRunTime += p->runtime;
if(FirstArrival == -1)
FirstArrival = p->arrival;
if(algo == 1)
enqueue(ready_queue, p);
else push(ready_queue, p, prio_flag);
buffer_down.mtype = 5;
if(msgsnd(msgid_down, &buffer_down, sizeof(buffer_down.mtext), 5) == -1) {
perror("msgsend");
exit(EXIT_FAILURE);
}
}
here is the process struct
typedef struct Process {
int id;
int pid;
int arrival;
int startTime;
int runtime;
int priority;
int WaitingTime;
int remainingTime;
char state[20];
int Sent;
} Process;
the receiveProcess function is a signal handler which is called multiple times consecutively (about 1 – 5 times). and I have found after some debugging is that this error happens in the third time or fourth time it enters the function.
keep in mind that usually it doesn’t happen at all.
Youssef Tarek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.