Jump to content

Weird Compile Error (C)

- - - - -

  • Please log in to reply
3 replies to this topic

#1
breimer273

breimer273

    Newbie

  • Members
  • PipPip
  • 16 posts
Alright I am having some trouble figuring out a compile error. I have been looking everywhere on the internet and keep finding things that are not my problem. First for the error and warning:

libscheduler/libscheduler.c:15: warning: ‘typedef’ is not at beginning of declaration

libscheduler/libscheduler.c:21: error: two or more data types in declaration specifiers

The above is my terminal output after i run make. First I will include libscheduler.c (the lines referenced above have been highlighted below for your ease):

/*

 * Author: Bill Reimer

 * 

 * The University of Illinois

 */


#include <stdio.h>

#include <stdlib.h>

#include <string.h>


#include "libscheduler.h"

#include "../libpriqueue/libpriqueue.h"

/* You may need to define some global variables or a struct to 

   store your job queue elements. */

typedef struct _job_t          <--------------This is the line for the warning (line 15)

{

	int job_number;

	int time;

	int running_time;

	int priority;

} job_t;                             <-------------------This is the line for the error (line 21)


int schedule_scheme = -1;

int number_avail_cores;

priqueue_t * job_queue;

struct job_t **core_busy;

float total_wait_time;

int number_of_jobs;


/*

 * schedular_start_up()

 * Initalizes the scheulder.

 */

void scheduler_start_up(int cores, scheme_t scheme)

{

	schedule_scheme = scheme;

	number_avail_cores = cores;

	core_busy = (job_t **)malloc(sizeof(job_t *) * cores);

	int index = 0;

	for(index = 0; index < cores; index++){

		core_busy[index] = NULL;

	}

	if(scheme == FCFS){

		priqueue_init(job_queue, &fcfs_compare);

	} else if(scheme == SJF){

		priqueue_init(job_queue, &sjf_compare);

	} else if(scheme == PSJF){

		priqueue_init(job_queue, &psjf_compare);

	} else if(scheme == PRI){

		priqueue_init(job_queue, &pri_compare);

	} else if(scheme == PPRI){

		priqueue_init(job_queue, &ppri_compare);

	} else if(scheme == RR){

		priqueue_init(job_queue, &rr_compare);

	}

}



/*

 * scheduler_new_job()

 * Called when a new job arrives.

 *

 * Returns: If the job arriving should be scheduled to run during the next

 * time cycle, return the zero-based index of the core the job should be

 * scheduled on. If another job is already running on the core specified,

 * this will preempt the currently running job.

 *

 * If multiple cores are idle, the job should be assigned to the core with the

 * lowest id.

 *

 * Otherwise, return -1.

 */

int scheduler_new_job(int job_number, int time, int running_time, int priority)

{

	number_of_jobs++;

	job_t * new = (job_t *)malloc(sizeof(job_t));

	new->job_number = job_number;

	new->time = time;

	new->running_time = running_time;

	new->priority = priority;

	if(scheme == FCFS){

		return -1;

	} else if(scheme == SJF){

		return -1;

	} else if(scheme == PSJF){

		if(priqueue_offer(job_queue, new) == 0){

			int index = 0;

			for(index = 0; index < number_avail_cores; index++){

				if(core_busy[index] == 0){

					return index;

				}

			}

			for(index = 0; index < number_avail_cores; index++){

				if(psjf_compare(core_busy[index], new) < 0){

					return index;

				}

			}

			return -1;

		}

	} else if(scheme == PRI){

		return -1;

	} else if(scheme == PPRI){

		if(priqueue_offer(job_queue, new) == 0){

			int index = 0;

			for(index = 0; index < number_avail_cores; index++){

				if(core_busy[index] == 0){

					return index;

				}

			}

			for(index = 0; index < number_avail_cores; index++){

				if(ppri_compare(core_busy[index], new) < 0){

					return index;

				}

			}

			return -1;

		}

	} else if(scheme == RR){

		return -1;

	}

}



/*

 * scheduler_job_finished()

 * Called when a job has completed execution.

 *

 * Returns: If any job should be scheduled to run on the core free'd up by the

 * finished job, return the job_number of the job that should be scheduled to

 * run on core core_id.

 *

 * Otherwise, if the core should remain idle, return -1.

 */

int scheduler_job_finished(int core_id, int job_number, int time)

{

	free(core_busy[core_id]);

	priqueue_t * next_job = (priqueue *)priqueue_poll(job_queue);

	if(next_job == NULL){

		return -1;

	}

	core_busy[core_id] = (job_t *)next_job->data;

	free(next_job);

	return core_busy[core_id]->job_number;

}



/*

 * scheduler_quantum_expired()

 * When the scheme is set to RR, called when the quantum timer has expired

 * on a core.

 *

 * Returns: If any job should be scheduled to run on the core free'd up by

 * the quantum expiration, return the job_number of the job that should be

 * scheduled to run on core core_id.

 *

 * Otherwise, if the core should remain idle, return -1.

 */

int scheduler_quantum_expired(int core_id, int time)

{

	priqueue_offer(job_queue, core_busy[core_id]);

	priqueue_t * next_job = (priqueue *)priqueue_poll(job_queue);

	if(next_job == NULL){

		return -1;

	}

	core_busy[core_id] = (job_t *)next_job->data;

	free(next_job);

	return core_busy[core_id]->job_number;

}



/*

 * scheduler_average_waiting_time()

 * Returns the average waiting time of all jobs scheduled by your scheduler.

 */

float scheduler_average_waiting_time()

{

	return 0.0;

}



/*

 * scheduler_average_turnaround_time()

 * Returns the average turnaround time of all jobs scheduled by

 * your scheduler.

 */

float scheduler_average_turnaround_time()

{

	return 0.0;

}



/*

 * scheduler_average_response_time()

 * Return the average response time of all jobs scheduled by your scheduler.

 */

float scheduler_average_response_time()

{

	return 0.0;

}



/*

 * scheduler_clean_up()

 * Free any memory associated with your scheduler.

 */

void scheduler_clean_up()

{


}



/*

 * scheduler_show_queue()

 * This function may print out any debugging information you choose. This

 * function will be called by the simulator after every call the simulator

 * makes to your scheduler.

 *

 * This function is not required and will not be graded. You may leave it

 * blank if you do not find it useful.

 */

void scheduler_show_queue()

{


}


And now libscheduler.h:

/*

 * Author: Bill Reimer

 * 

 * The University of Illinois

 */


#ifndef LIBSCHEDULER_H_

#define LIBSCHEDULER_H_



typedef enum {FCFS = 0, SJF, PSJF, PRI, PPRI, RR} scheme_t;




void  scheduler_start_up               (int cores,  scheme_t scheme);

int   scheduler_new_job                (int job_number, int time, int running_time, int priority);

int   scheduler_job_finished           (int core_id, int job_number, int time);

int   scheduler_quantum_expired        (int core_id, int time);

float scheduler_average_turnaround_time();

float scheduler_average_waiting_time   ();

float scheduler_average_response_time  ();

void  scheduler_clean_up               ();


void  scheduler_show_queue             ();


#endif /* LIBSCHEDULER_H_ */

On the internet I usually see that people forget the ';' after the struct but as you can see I didn't and I still have the error. Thanks in advance for all your help.

#2
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
It's been a while since I last wrote a struct, but try changing:

typedef struct _job_t          <--------------This is the line for the warning (line 15)

{

	int job_number;

	int time;

	int running_time;

	int priority;

} job_t;

to

typedef struct _job_t          <--------------Take out the name here or

{

	int job_number;

	int time;

	int running_time;

	int priority;

};            <--------------------------------- Take out the name here

If this didn't help, I apologize... :D

#3
breimer273

breimer273

    Newbie

  • Members
  • PipPip
  • 16 posts
lintwurm: Thanks for the reply. I tried it but still no dice. I have been using structs a lot for the past year and have never had this issue before. Its frustrating...

#4
breimer273

breimer273

    Newbie

  • Members
  • PipPip
  • 16 posts
Well I figured out the problem with the struct, apparently it didn't like me defining the struct after the #include libscheduler.h statement... Wonder why that is? But now onto new errors, hopefully you guys can help. Here is the error:


libscheduler/libscheduler.c:22: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token

(with the error line highlighted.
libscheduler.c:


/*

 * Machine Problem #4

 * CS 241

 * The University of Illinois

 */


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct {

	int job_number;

	int time;

	int running_time;

	int priority;

} job_t;

int schedule_scheme = -1;

int number_avail_cores;


#include "libscheduler.h"

#include "../libpriqueue/libpriqueue.h"


priqueue_t *job_queue;       <--------------------------------Error happens here (line 22)

struct job_t **core_busy;

float total_wait_time;

int number_of_jobs;




int fcfs_compare(const void * data1, const void * data2){

	return 0;

}

int sjf_compare(const void * data1, const void * data2){

	return 0;

}

int psjf_compare(const void * data1, const void * data2){

	return 0;

}

int pri_compare(const void * data1, const void * data2){

	return 0;

}

int ppri_compare(const void * data1, const void * data2){

	return 0;

}

int rr_compare(const void * data1, const void * data2){

	return 0;

}


/*

 * schedular_start_up()

 * Initalizes the scheulder.

 */

void scheduler_start_up(int cores, scheme_t scheme)

{

	schedule_scheme = scheme;

	number_avail_cores = cores;

	core_busy = (job_t **)malloc(sizeof(job_t *) * cores);

	int index = 0;

	for(index = 0; index < cores; index++){

		core_busy[index] = NULL;

	}

	if(scheme == FCFS){

		priqueue_init(job_queue, &fcfs_compare);

	} else if(scheme == SJF){

		priqueue_init(job_queue, &sjf_compare);

	} else if(scheme == PSJF){

		priqueue_init(job_queue, &psjf_compare);

	} else if(scheme == PRI){

		priqueue_init(job_queue, &pri_compare);

	} else if(scheme == PPRI){

		priqueue_init(job_queue, &ppri_compare);

	} else if(scheme == RR){

		priqueue_init(job_queue, &rr_compare);

	}

}



/*

 * scheduler_new_job()

 * Called when a new job arrives.

 *

 * Returns: If the job arriving should be scheduled to run during the next

 * time cycle, return the zero-based index of the core the job should be

 * scheduled on. If another job is already running on the core specified,

 * this will preempt the currently running job.

 *

 * If multiple cores are idle, the job should be assigned to the core with the

 * lowest id.

 *

 * Otherwise, return -1.

 */

int scheduler_new_job(int job_number, int time, int running_time, int priority)

{

	number_of_jobs++;

	job_t * new = (job_t *)malloc(sizeof(job_t));

	new->job_number = job_number;

	new->time = time;

	new->running_time = running_time;

	new->priority = priority;

	if(scheme == FCFS){

		return -1;

	} else if(scheme == SJF){

		return -1;

	} else if(scheme == PSJF){

		if(priqueue_offer(job_queue, new) == 0){

			int index = 0;

			for(index = 0; index < number_avail_cores; index++){

				if(core_busy[index] == 0){

					return index;

				}

			}

			for(index = 0; index < number_avail_cores; index++){

				if(psjf_compare(core_busy[index], new) < 0){

					return index;

				}

			}

			return -1;

		}

	} else if(scheme == PRI){

		return -1;

	} else if(scheme == PPRI){

		if(priqueue_offer(job_queue, new) == 0){

			int index = 0;

			for(index = 0; index < number_avail_cores; index++){

				if(core_busy[index] == 0){

					return index;

				}

			}

			for(index = 0; index < number_avail_cores; index++){

				if(ppri_compare(core_busy[index], new) < 0){

					return index;

				}

			}

			return -1;

		}

	} else if(scheme == RR){

		return -1;

	}

}



/*

 * scheduler_job_finished()

 * Called when a job has completed execution.

 *

 * Returns: If any job should be scheduled to run on the core free'd up by the

 * finished job, return the job_number of the job that should be scheduled to

 * run on core core_id.

 *

 * Otherwise, if the core should remain idle, return -1.

 */

int scheduler_job_finished(int core_id, int job_number, int time)

{

	free(core_busy[core_id]);

	priqueue_t * next_job = (priqueue *)priqueue_poll(job_queue);

	if(next_job == NULL){

		return -1;

	}

	core_busy[core_id] = (job_t *)next_job->data;

	free(next_job);

	return core_busy[core_id]->job_number;

}



/*

 * scheduler_quantum_expired()

 * When the scheme is set to RR, called when the quantum timer has expired

 * on a core.

 *

 * Returns: If any job should be scheduled to run on the core free'd up by

 * the quantum expiration, return the job_number of the job that should be

 * scheduled to run on core core_id.

 *

 * Otherwise, if the core should remain idle, return -1.

 */

int scheduler_quantum_expired(int core_id, int time)

{

	priqueue_offer(job_queue, core_busy[core_id]);

	priqueue_t * next_job = (priqueue *)priqueue_poll(job_queue);

	if(next_job == NULL){

		return -1;

	}

	core_busy[core_id] = (job_t *)next_job->data;

	free(next_job);

	return core_busy[core_id]->job_number;

}



/*

 * scheduler_average_waiting_time()

 * Returns the average waiting time of all jobs scheduled by your scheduler.

 */

float scheduler_average_waiting_time()

{

	return 0.0;

}



/*

 * scheduler_average_turnaround_time()

 * Returns the average turnaround time of all jobs scheduled by

 * your scheduler.

 */

float scheduler_average_turnaround_time()

{

	return 0.0;

}



/*

 * scheduler_average_response_time()

 * Return the average response time of all jobs scheduled by your scheduler.

 */

float scheduler_average_response_time()

{

	return 0.0;

}



/*

 * scheduler_clean_up()

 * Free any memory associated with your scheduler.

 */

void scheduler_clean_up()

{


}



/*

 * scheduler_show_queue()

 * This function may print out any debugging information you choose. This

 * function will be called by the simulator after every call the simulator

 * makes to your scheduler.

 *

 * This function is not required and will not be graded. You may leave it

 * blank if you do not find it useful.

 */

void scheduler_show_queue()

{


}

libscheduler.h:


/*

 * Machine Problem #4

 * CS 241

 * The University of Illinois

 */


#ifndef LIBSCHEDULER_H_

#define LIBSCHEDULER_H_



typedef enum {FCFS = 0, SJF, PSJF, PRI, PPRI, RR} scheme_t;




void  scheduler_start_up               (int cores,  scheme_t scheme);

int   scheduler_new_job                (int job_number, int time, int running_time, int priority);

int   scheduler_job_finished           (int core_id, int job_number, int time);

int   scheduler_quantum_expired        (int core_id, int time);

float scheduler_average_turnaround_time();

float scheduler_average_waiting_time   ();

float scheduler_average_response_time  ();

void  scheduler_clean_up               ();


void  scheduler_show_queue             ();


#endif /* LIBSCHEDULER_H_ */

libpriqueue.c:


/*

 * Machine Problem #4

 * CS 241

 * The University of Illinois

 */


#include <stdlib.h>

#include <stdio.h>


#include "libpriqueue.h"


/*

 * You can find the struct priqueue_t in libpriqueue.h.

 */



/*

 * priqueue_init()

 * Initializes the priqueue_t data structure.

 */

void priqueue_init(priqueue_t *q, int(*comparer)(const void *, const void *))

{

	q = (priqueue_t*)malloc(sizeof(priqueue_t));

	compare_func = comparer;

	q->next = NULL;

	q->data = NULL;

	return void;

}



/*

 * priqueue_offer()

 * Inserts the specified element into this priority queue.

 *

 * Returns: The zero-based index where ptr is stored in the priority queue,

 * where 0 indicates that ptr was stored at the front of the priority queue.

 */

int priqueue_offer(priqueue_t *q, void *ptr)

{

	priqueue_t * current = q;

	priqueue_t * new_node = (priqueue *)malloc(sizeof(priqueue_t));

	priqueue_t * temp = current->next;

	new_node->data = ptr;

	int compare_result;

	int counter = 0

	if(temp == NULL){

		current->next = new_node;

		return counter;

		}

	}

	counter++;

	while(temp != NULL){

		compare_result = compare_func(temp->data, new_node->data);

		if(compare_result < 0){

			new_node->next = temp;

			current->next = new_node;

			return;

		}

		else if(compare_result >= 0){

			counter++;

			current = current->next;

			temp = temp->next;

		}

	}

	//If we get to this point the job must be added to the end of the queue

	temp->next = new_node;

	new_node->next = NULL;

	return counter;

}



/*

 * priqueue_peek()

 * Retrieves, but does not remove, the head of this queue, returning NULL if

 * this queue is empty.

 *

 * Returns: The head of the queue, or NULL if the queue is empty.

 */

void *priqueue_peek(priqueue_t *q)

{

	return q->next;

}



/*

 * priqueue_poll()

 * Retrieves and removes the head of this queue, or NULL if this queue

 * is empty.

 *

 * Returns: The head of this queue, or NULL if this queue is empty.

 */

void *priqueue_poll(priqueue_t *q)

{

	priqueue_t * temp = q->next;

	q->next = temp->next;

	return temp;

}



/*

 * priqueue_at()

 * Returns the element at the specified position in this list, or NULL if

 * the queue does not contain an index'th element.

 *

 * Returns: The index'th element in the queue, or NULL if the queue does

 * not contain in index'th element.

 */

void *priqueue_at(priqueue_t *q, int index)

{

	priqueue_t * temp = q->next;

	for(index; index > 0; index--){

		temp = temp->next;

	}

	return temp;

}



/*

 * priqueue_remove()

 * Removes all instances of ptr from the queue. This function should not

 * use the comparer function, but check if the data contained in each

 * element of the queue is equal (==) to ptr.

 *

 * Returns: The number of entries removed.

 */

int priqueue_remove(priqueue_t *q, void *ptr)

{

	int counter;

	priqueue_t * temp = q->next;

	priqueue_t * lead_temp = q;

	while(temp != NULL){

		if(temp->data == ptr){

			lead_temp->next = temp->next;

			free(temp);

			temp = lead_temp->next;

			counter++;

		}

		temp = temp->next;

		lead_temp = lead_temp->next;

	}

	return counter;

}



/*

 * priqueue_remove_at()

 * Removes the specified index from the queue, moving later elements up

 * a spot in the queue to fill the gap.

 *

 * Returns: The element removed from the queue, or NULL if the specified

 * index does not exist.

 */

void *priqueue_remove_at(priqueue_t *q, int index)

{

	priqueue_t * temp = q->next;

	priqueue_t * lead_temp

	while(temp != NULL && index > 0){

		temp = temp->next;

		lead_temp = lead_temp->next;

		index--;

	}

	if(temp != NULL){

		lead_temp->next = temp->next;

	}

	return temp;

}



/*

 * priqueue_size()

 * Returns the number of elements in the queue.

 *

 * Returns: The number of elements in the queue.

 */

int priqueue_size(priqueue_t *q)

{

	int counter = 0;

	priqueue_t * temp = q->next;

	while(temp != NULL){

		temp = temp->next;

		counter++;

	}

	return counter;

}



/*

 * priqueue_destroy()

 * Destroys and frees all the memory associated with q.

 */

void priqueue_destroy(priqueue_t *q)

{

	priqueue_t * temp = q->next;

	while(temp != NULL){

		free(q);

		q = temp;

		temp = temp->next;

	}

	free(q);

	return void;

}

and libpriqueue.h:

/*

 * Machine Problem #4

 * CS 241

 * The University of Illinois

 */


#ifndef LIBPRIQUEUE_H_

#define LIBPRIQUEUE_H_


typedef struct {

	int (*compare_func)(const void *, const void *);

	struct priqueue_t * next;

	void *data;


} priqueue_t;



void   priqueue_init     (priqueue_t *q, int(*comparer)(const void *, const void *));


int    priqueue_offer    (priqueue_t *q, void *ptr);

void * priqueue_peek     (priqueue_t *q);

void * priqueue_poll     (priqueue_t *q);

void * priqueue_at       (priqueue_t *q, int index);

int    priqueue_remove   (priqueue_t *q, void *ptr);

void * priqueue_remove_at(priqueue_t *q, int index);

int    priqueue_size     (priqueue_t *q);


void   priqueue_destroy  (priqueue_t *q);


int 


#endif /* LIBPQUEUE_H_ */


I know thats a lot of code but I wanted to make sure you had everything to reference. Thanks in advance!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users