diff --git a/intro/Makefile b/intro/Makefile new file mode 100644 index 0000000..b5c1fe7 --- /dev/null +++ b/intro/Makefile @@ -0,0 +1,18 @@ + +all: cpu mem threads io + +clean: + rm -f cpu mem threads io + +cpu: cpu.c common.h + gcc -o cpu cpu.c -Wall + +mem: mem.c common.h + gcc -o mem mem.c -Wall + +threads: threads.c common.h common_threads.h + gcc -o threads threads.c -Wall -pthread + +io: io.c common.h + gcc -o io io.c -Wall + diff --git a/intro/common.h b/intro/common.h new file mode 100644 index 0000000..1202765 --- /dev/null +++ b/intro/common.h @@ -0,0 +1,21 @@ +#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 +} + +#endif // __common_h__ diff --git a/intro/common_threads.h b/intro/common_threads.h new file mode 100644 index 0000000..a587122 --- /dev/null +++ b/intro/common_threads.h @@ -0,0 +1,33 @@ +#ifndef __common_threads_h__ +#define __common_threads_h__ + +#include +#include +#include + +#ifdef __linux__ +#include +#endif + +#define Pthread_create(thread, attr, start_routine, arg) assert(pthread_create(thread, attr, start_routine, arg) == 0); +#define Pthread_join(thread, value_ptr) assert(pthread_join(thread, value_ptr) == 0); + +#define Pthread_mutex_lock(m) assert(pthread_mutex_lock(m) == 0); +#define Pthread_mutex_unlock(m) assert(pthread_mutex_unlock(m) == 0); +#define Pthread_cond_signal(cond) assert(pthread_cond_signal(cond) == 0); +#define Pthread_cond_wait(cond, mutex) assert(pthread_cond_wait(cond, mutex) == 0); + +#define Mutex_init(m) assert(pthread_mutex_init(m, NULL) == 0); +#define Mutex_lock(m) assert(pthread_mutex_lock(m) == 0); +#define Mutex_unlock(m) assert(pthread_mutex_unlock(m) == 0); +#define Cond_init(cond) assert(pthread_cond_init(cond, NULL) == 0); +#define Cond_signal(cond) assert(pthread_cond_signal(cond) == 0); +#define Cond_wait(cond, mutex) assert(pthread_cond_wait(cond, mutex) == 0); + +#ifdef __linux__ +#define Sem_init(sem, value) assert(sem_init(sem, 0, value) == 0); +#define Sem_wait(sem) assert(sem_wait(sem) == 0); +#define Sem_post(sem) assert(sem_post(sem) == 0); +#endif // __linux__ + +#endif // __common_threads_h__ diff --git a/intro/cpu.c b/intro/cpu.c new file mode 100644 index 0000000..3154456 --- /dev/null +++ b/intro/cpu.c @@ -0,0 +1,19 @@ +#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) { + printf("%s\n", str); + Spin(1); + } + return 0; +} + diff --git a/intro/io.c b/intro/io.c new file mode 100644 index 0000000..617fd4d --- /dev/null +++ b/intro/io.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + int fd = open("/tmp/file", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); + assert(fd >= 0); + char buffer[20]; + sprintf(buffer, "hello world\n"); + int rc = write(fd, buffer, strlen(buffer)); + assert(rc == (strlen(buffer))); + fsync(fd); + close(fd); + return 0; +} + diff --git a/intro/mem.c b/intro/mem.c new file mode 100644 index 0000000..a82d34b --- /dev/null +++ b/intro/mem.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include "common.h" + +int main(int argc, char *argv[]) { + if (argc != 2) { + fprintf(stderr, "usage: mem \n"); + exit(1); + } + int *p; + p = malloc(sizeof(int)); + assert(p != NULL); + printf("(%d) addr pointed to by p: %p\n", (int) getpid(), p); + *p = atoi(argv[1]); // assign value to addr stored in p + while (1) { + Spin(1); + *p = *p + 1; + printf("(%d) value of p: %d\n", getpid(), *p); + } + return 0; +} + diff --git a/intro/threads.c b/intro/threads.c new file mode 100644 index 0000000..5c7e044 --- /dev/null +++ b/intro/threads.c @@ -0,0 +1,32 @@ +#include +#include +#include "common.h" +#include "common_threads.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; +} +