以下代码是本书在我的操作系统课程中提供的示例。 编译时,我得到下面显示的错误。
#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_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 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);
}
我用这个命令编译了它......
gcc linux_scheduling.c -o scheduling
但是,我收到了这个错误。
linux_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
我尝试添加-pthread
:
gcc linux_scheduling.c -o scheduling -pthread
但错误仍然存在。
感谢您的帮助!
答案 0 :(得分:1)
您拥有正确的编译命令:
[.data.table
但你需要把:
gcc linux_scheduling.c -o scheduling -pthread
在void *runner(void *param);
开始之前宣布:
main
答案 1 :(得分:0)
声明跑步者的原型,或者如果你不想声明然后在main之前定义该函数。这是因为main引用了该函数并为您提供了这样的错误