最近在看OSTEP,这里主要写一些阅读时遇到的问题与解决办法,以及阅读时手敲的代码,作备忘用。
最开始我是用的VBox上虚拟的Ubuntu,感觉还是太麻烦了,后来就换成了Visual Studio 2022 + WSL2 + Makefile,感觉还是挺爽的。
#makefile
.PHONY: clean run
CC = gcc
CFLAGS = -W -Wall -pthread
RM = -rm -rf
object = threads.c #根据具体文件名更改
BIN_NAME = $(subst .c,,$(object)) #可执行文件的名称
$(BIN_NAME): $(object)
$(CC) $(CFLAGS) -o $@ $(object)
%.o: %.cc
$(CC) $(CFLAGS) -c $< -o $@
run:
./$(BIN_NAME)
clean:
$(RM) *.o $(BIN_NAME)
//cpu.c
#include
#include
#include
#include
#include "common.h"
int main(int argc, char* argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: cpu \n" );
exit(1);
}
char* str = argv[1];
while (1)
{
Spin(1);
printf("%s\n", str);
}
return 0;
}
首先sys/time.h头文件显示了这是个运行在Linux上的代码,然后我把这段代码CV过去,一编译,缺少头文件= =,然后又是Spin函数未定义,查了下资料,原来是我不知道看漏了哪里,这里应该有个common.h的!!!代码如下:
#ifndef __common_h__
#define __common_h__
#include
#include
#include
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__
我粘贴进去之后再编译,发现报了这个错:
/usr/bin/ld: /tmp/ccjTj0Sq.o: in function
Pthread_create': cpu.c:(.text+0x101): undefined reference to
pthread_create’
/usr/bin/ld: /tmp/ccjTj0Sq.o: in functionPthread_join': cpu.c:(.text+0x153): undefined reference to
pthread_join’
collect2: error: ld returned 1 exit status
再查了一下资料后发现原来需要给编译选项加一个-pthread
这下编译通过了:
同时如预期运行了:
紧接着我把中文版的图2.2的命令
敲了进去,发现:
bash: 未预期的符号“;”附近有语法错误
然后我顺便看了一下原文:
看书下面说这个分号是有用的,可能是因为系统版本原因吧?
改成./cpu A & ./cpu B & ./cpu C & ./cpu D &
后顺利运行:
//mem.c
#include
#include
#include
#include "common.h"
int main(int argc, char* argv[])
{
int* p = malloc(sizeof(int));
assert(p != NULL);
printf("(%d) memory address of p: %08x\n",
getpid(), (unsigned)p);
*p = 0;
while (1)
{
Spin(1);
*p = *p + 1;
printf("(%d) p: %d\n", getpid(), *p);
}
return 0;
}
//threads.c
#include
#include
#include "common.h"
volatile int counter = 0;
int loops;
void* worker(void* arg) {
int i;
for (i = 0; i < loops; i++)
{
counter++;
}
return NULL;
}
int main(int argc, char* argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: threads \n" );
exit(1);
}
loops = atoi(argv[1]);
pthread_t p1, p2;
printf("Initial value : %d\n", counter);
Pthread_create(&p1, NULL, worker, NULL);
Pthread_create(&p2, NULL, worker, NULL);
Pthread_join(p1, NULL);
Pthread_join(p2, NULL);
printf("Final value : %d\n", counter);
return 0;
}
//p1.c
#include
#include
#include
int main(int argc, char* argv[])
{
printf("hello world (pid:%d)\n", (int)getpid());
int rc = fork();
if (rc < 0) {
fprintf(stderr, "fork failed\n");
exit(1);
}
else if (rc == 0) {
printf("hello, I am child (pid:%d)\n", (int)getpid());
}
else {
printf("hello, I am parent of %d (pid:%d)\n", rc, (int)getpid());
}
return 0;
}