I follow the book Operating Systems: Three Easy Pieces, the code in introduction chapter,
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <assert.h>
#include "common.h"
int
main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "usage: cpu <string>\n");
exit(1);
}
char *str = argv[1];
while(1)
{
Spin(1);
printf("%s\n", str);
}
return 0;
}
When I try gcc -o cpu cpu.c -Wall
,
The error came out: fatal error: 'common.h' file not found,
I have tried download common.h from this link, and put this file with cpu.c, but it doesn't work, error message:
cpu.c:8:1: error: conflicting types for 'main'
main(int argc, char *argv[])
^
./common.h:86:13: note: previous declaration is here
extern int main(int, char **, char **);
^
cpu.c:18:5: warning: implicit declaration of function 'Spin' is invalid in C99 [-Wimplicit-function-declaration]
Spin(1);
^
1 warning and 1 error generated.
How to fix the error? Thanks.
The "common.h"
header for this problem is linked as a tgz bundle in the table of contents for the course next to chapter 1: http://pages.cs.wisc.edu/~remzi/OSTEP/
Drop it next to your source file and try compiling again.
Here is what you are looking for. All the other answers don't understand that you were working from an OS book. Please refer to the link provided on the site.
http://pages.cs.wisc.edu/~remzi/OSTEP/Code/code.intro.tgz
#ifndef __common_h__
#define __common_h__
#include <sys/time.h>
#include <assert.h>
#include <pthread.h>
double GetTime() {
struct timeval t;
int rc = gettimeofday(&t, NULL);
assert(rc == 0);
return (double)t.tv_sec + (double)t.tv_usec/1e6;
}
void Spin(int howlong) {
double t = GetTime();
while ((GetTime() - t) < (double)howlong)
; // do nothing in loop
}
void Pthread_create(pthread_t *t, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg) {
int rc = pthread_create(t, attr, start_routine, arg);
assert(rc == 0);
}
void Pthread_join(pthread_t thread, void **value_ptr) {
int rc = pthread_join(thread, value_ptr);
assert(rc == 0);
}
void Pthread_mutex_lock(pthread_mutex_t *mutex) {
int rc = pthread_mutex_lock(mutex);
assert(rc == 0);
}
void Pthread_mutex_unlock(pthread_mutex_t *mutex) {
int rc = pthread_mutex_unlock(mutex);
assert(rc == 0);
}
void Pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr) {
int rc = pthread_mutex_init(mutex, attr);
assert(rc == 0);
}
#endif // __common_h__
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With