forklock.c
| 1 | #include <stdio.h> |
|---|---|
| 2 | #include <stdlib.h> |
| 3 | |
| 4 | #include <sys/types.h> |
| 5 | #include <sys/mman.h> |
| 6 | |
| 7 | int
|
| 8 | main(int argc, char *argv[]) |
| 9 | {
|
| 10 | void *ptr;
|
| 11 | pid_t pid; |
| 12 | size_t amt; |
| 13 | int wstat;
|
| 14 | |
| 15 | amt=(size_t) (1 * sizeof(char)); |
| 16 | ptr = malloc(amt); |
| 17 | |
| 18 | printf("pre-lock: %p\n", ptr);
|
| 19 | mlock(ptr,amt); |
| 20 | |
| 21 | if((pid = fork()) == 0) { |
| 22 | sleep(1);
|
| 23 | printf("child: %p\n", ptr);
|
| 24 | return 0; |
| 25 | } |
| 26 | else {
|
| 27 | printf("parent: %p\n", ptr);
|
| 28 | waitpid(pid, &wstat); |
| 29 | } |
| 30 | |
| 31 | return 0; |
| 32 | |
| 33 | } |
| 34 |