Initial programs from intro chapter
This commit is contained in:
18
intro/Makefile
Normal file
18
intro/Makefile
Normal file
@@ -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
|
||||
|
||||
21
intro/common.h
Normal file
21
intro/common.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef __common_h__
|
||||
#define __common_h__
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <sys/stat.h>
|
||||
#include <assert.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
|
||||
}
|
||||
|
||||
#endif // __common_h__
|
||||
33
intro/common_threads.h
Normal file
33
intro/common_threads.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef __common_threads_h__
|
||||
#define __common_threads_h__
|
||||
|
||||
#include <pthread.h>
|
||||
#include <assert.h>
|
||||
#include <sched.h>
|
||||
|
||||
#ifdef __linux__
|
||||
#include <semaphore.h>
|
||||
#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__
|
||||
19
intro/cpu.c
Normal file
19
intro/cpu.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.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) {
|
||||
printf("%s\n", str);
|
||||
Spin(1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
19
intro/io.c
Normal file
19
intro/io.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
23
intro/mem.c
Normal file
23
intro/mem.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "common.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "usage: mem <value>\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;
|
||||
}
|
||||
|
||||
32
intro/threads.c
Normal file
32
intro/threads.c
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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 <loops>\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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user