Compare commits

...

10 Commits

Author SHA1 Message Date
Remzi Arpaci-Dusseau
ba628e3dd9 Update README.md
fixed to match filenames.
2023-11-09 14:49:36 -06:00
Remzi Arpaci-Dusseau
0cd7a8be1a removed CRs 2021-08-23 07:13:51 -05:00
Remzi Arpaci-Dusseau
bf8bf14d43 push comment added back 2021-08-16 07:30:40 -05:00
Remzi Arpaci-Dusseau
ec258eef6a fixed comment 2021-08-15 11:08:07 -07:00
Remzi Arpaci-Dusseau
9be6d209fa removing extra file 2021-08-14 14:46:04 -07:00
Remzi Arpaci-Dusseau
cf38b849dd pstack updated 2021-08-14 14:39:59 -07:00
Remzi Arpaci
a140f0bc0f updated version 2021-08-12 22:07:53 -05:00
Remzi Arpaci-Dusseau
82967c6024 initial cut of mmap'd stack code 2021-08-10 13:08:35 -05:00
Remzi Arpaci-Dusseau
03c28cec36 Merge pull request #15 from timgates42/bugfix_typo_permanent
docs: fix simple typo, permament -> permanent
2020-12-07 14:21:09 -06:00
Tim Gates
ad66eafc48 docs: fix simple typo, permament -> permanent
There is a small typo in intro/README.md.

Should read `permanent` rather than `permament`.
2020-12-08 07:11:17 +11:00
3 changed files with 77 additions and 3 deletions

74
file-intro/pstack.c Normal file
View File

@@ -0,0 +1,74 @@
// Persistent stack example of how mmap() naturally creates a software abstraction of persistent memory.
// Author: Terence Kelly
// tpkelly @ { acm.org, cs.princeton.edu, eecs.umich.edu }
// Note: Use 'truncate' to make the initial (empty) backing file,
// whose size should be a multiple of the system page size:
//
// prompt> getconf PAGESIZE
// 4096
// prompt> truncate -s 4096 ps.img
//
// makes a 4096-byte empty file. The first sizeof(size_t) bytes will
// be interprted as the number of items on the stack; the remainder
// of the file will contain the integers contained in the stack.
// Now you can run the program:
//
// prompt> ./pstack 7 13 47 pop
// 47
// prompt> ./pstack pop pop 99
// 13
// 7
// prompt> ./pstack pop
// 99
//
// Notice that items push'd in one invocation of the program persist
// until the next invocation: The stack is persistent.
//
// You can use hexdump to examine the contents of the backing file:
//
// prompt> hexdump ps.img
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
typedef struct {
size_t n;
int stack[]; // zero-length per C99
} pstack_t;
int main(int argc, char *argv[]) {
int fd, rc;
struct stat s;
size_t file_size;
pstack_t *p;
fd = open("ps.img", O_RDWR);
assert(fd > -1);
rc = fstat(fd, &s);
assert(rc == 0);
file_size = (size_t) s.st_size;
assert(file_size >= sizeof(pstack_t) && file_size % sizeof(int) == 0);
p = (pstack_t *) mmap(NULL, file_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
assert(p != MAP_FAILED);
for (int i = 1; i < argc; i++)
if (strcmp(argv[i], "pop") == 0) {
if (p->n > 0) // stack not empty
printf("%d\n", p->stack[--p->n]);
} else { // push
if (sizeof(pstack_t) + (1 + p->n) * sizeof(int) <= file_size) // stack not full
p->stack[p->n++] = atoi(argv[i]);
}
(void) close(fd);
return 0;
}

View File

@@ -48,7 +48,7 @@ Under Linux you can disable ASLR, without using a debugger, in (at least) two w
bash, with which I can execute examples, like this: bash, with which I can execute examples, like this:
`setarch $(uname --machine) --addr-no-randomize /bin/bash` `setarch $(uname --machine) --addr-no-randomize /bin/bash`
* Writing 0 into `/proc/sys/kernel/randomize_va_space`; you need to be * Writing 0 into `/proc/sys/kernel/randomize_va_space`; you need to be
root to do this and this change has (a non-permament) effect on the root to do this and this change has (a non-permanent) effect on the
whole system, which is something you probably don't want. I use this whole system, which is something you probably don't want. I use this
one only inside VMs. one only inside VMs.

View File

@@ -51,8 +51,8 @@ Code in `rwlock.c`. Build via `make`, run via `rwlock`.
The dining philosophers example from the text is found herein, in a few The dining philosophers example from the text is found herein, in a few
different forms: different forms:
- `dining_philosophers.c`: code with deadlock - `dining_philosophers_deadlock.c`: code with deadlock
- `dining_philosophers_print.c`: code with deadlock, and some useful printing - `dining_philosophers_deadlock_print.c`: code with deadlock, and some useful printing
- `dining_philosophers_no_deadlock.c`: code without deadlock - `dining_philosophers_no_deadlock.c`: code without deadlock
- `dining_philosophers_no_deadlock_print.c`: code without deadlock, and some useful printing - `dining_philosophers_no_deadlock_print.c`: code without deadlock, and some useful printing