the code below sample provided book in operating systems course. when compiling error shown below it.
#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 policy.\n"); else{ if(policy == sched_other) printf("sched_other\n"); else if(policy == sched_rr) printf("sched_rr\n"); else if(policy == sched_fifo) printf("sched_fifo\n"); } if(pthread_attr_setschedpolicy(&attr, sched_fifo) != 0) fprintf(stderr, "unable set policy.\n"); /* create threads */ for(i = 0; < num_threads; i++) pthread_create(&tid[i], &attr, runner, null); /* join on each thread */ for(i = 0; < num_threads; i++) pthread_join(tid[i], null); } /* each thread begin control in function */ void *runner(void *param) { /* work... */ pthread_exit(0); }
i compiled using command...
gcc linux_scheduling.c -o scheduling
however, error.
linux_scheduling.c:32:34: error: 'runner' undeclared (first use in function) pthread_create(&tid[i], &attr, runner, null); ^ linux_scheduling.c:32:34: note: each undeclared identifier report once each function appears in
i tried adding -pthread
:
gcc linux_scheduling.c -o scheduling -pthread
but error remains.
thanks help!
you have correct compiling command:
gcc linux_scheduling.c -o scheduling -pthread
but need put:
void *runner(void *param);
ahead of start of main
declare it:
#include <pthread.h> #include <stdio.h> #define num_threads 5 void *runner(void *param); int main(int argc, char *argv[]) { ...
Comments
Post a Comment