The code below is a sample provided by the book in my Operating Systems course. When compiling it I get the error shown below it.
JavaScript
x
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
int main(int argc, char *argv[])
{
int i, policy;
pthread_t tid[NUM_THREADS];
pthread_attr_t attr;
pthread_attr_init(&attr);
if(pthread_attr_getschedpolicy(&attr, &policy) != 0)
fprintf(stderr, "Unable to get policy.n");
else{
if(policy == SCHED_OTHER)
printf("SCHED_OTHERn");
else if(policy == SCHED_RR)
printf("SCHED_RRn");
else if(policy == SCHED_FIFO)
printf("SCHED_FIFOn");
}
if(pthread_attr_setschedpolicy(&attr, SCHED_FIFO) != 0)
fprintf(stderr, "Unable to set policy.n");
/* create the threads */
for(i = 0; i < NUM_THREADS; i++)
pthread_create(&tid[i], &attr, runner, NULL);
/* now join on each thread */
for(i = 0; i < NUM_THREADS; i++)
pthread_join(tid[i], NULL);
}
/* Each thread will begin control in this function */
void *runner(void *param)
{
/* do some work... */
pthread_exit(0);
}
I compiled it using this command…
JavaScript
gcc linux_scheduling.c -o scheduling
However, I get this error.
JavaScriptlinux_scheduling.c:32:34: error: 'runner' undeclared (first use in this function)
pthread_create(&tid[i], &attr, runner, NULL);
^
linux_scheduling.c:32:34: note: each undeclared identifier is report only once for each function it appears in
I tried adding -pthread
:
JavaScript
gcc linux_scheduling.c -o scheduling -pthread
but the error remains.
Thanks for your help!
Advertisement
Answer
You have the correct compiling command:
JavaScript
gcc linux_scheduling.c -o scheduling -pthread
but you need to put:
JavaScript
void *runner(void *param);
ahead of the start of main
to declare it:
JavaScript
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *runner(void *param);
int main(int argc, char *argv[])
{