API Flashcards
What does fork() do? What does it return?
It starts a new process at the point it is called. The child process will return 0, while the parent process will return the pid of the child process. It is not deterministic which process will be executed first. Each process will have their own version of the program context.
What does wait() do? What does it return?
Wait() will make the parent process wait for its child to complete before continuing, allowing us to have some deterministic results. We could also use waitpid() to make it wait for a particular child. Wait() returns the pid of the child.
What does exec() do?
You call exec() when you want to run a program which is different from the calling program. A successful exec never returns to where it was called from.
How would you use execvp()?
execvp takes two arguments: A command and a set of arguments. For the command, I would give it a string such as “./test” so that it would execute test.c.
Then, for the arguments, I would give it an array. The first element of the array would be “./test”, the second would be the argument I wished to give to test, for example “Hello\n” and the third element would be NULL.
What does malloc() do?
returns a pointer to an area of memory allocated on the heap.
What does free() do?
frees up memory allocated on the heap?
How might we allocate space for a String using malloc?
malloc(strlen(s) + 1)
So, we might do:
char *src = “Hello”;
char *dst = malloc(strlen(src) + 1);
strcpy(dst, src);
What are some common errors with malloc and free?
Not allocating memory. Not allocating enough memory. Forgetting to initialise memory. Forgetting to free the memory. Freeing memory too soon. Freeing memory twice (undefined results).
Are malloc and free system calls or library calls?
library calls.
What is the brk pointer? How do we move it? How can we use sbrk()?
The brk pointer points to the maximum point on the heap. We can move it to a specific address with brk(void *addr).
If we want to give more memory to the heap, we can use sbrk(). For example, sbrk(4000) will give you 4000 more bytes on the heap, and will return a pointer to the previous brk position.
What will sbrk(0) return?
The current position of the brk pointer.
True or false, brk and sbrk show us addresses in physical memory?
False, they deal with the virtual heap.
How can we use mmap()?
mmap is used to create a new mapping in the virtual address space of a calling process. The length of the argument specifies the length of the mapping.
void mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
What are some differences between sbrk and mmap?
sbrk extends the process heap in an easy way, but it is not easy to hand back this allocated memory.
mmap, which is a POSIX standard, allows you to easily allocate several large areas, and to hand them back easily.
How would you use pthread_create()?
int pthread_create(pthread_t thread, const pthread_attr_t attr, void *(start_routine)(void), void *arg)
In other words, the first argument is a pointer to the thread, the second argument are attributes of this thread, the third argument is the function we want to run, and the last argument is the function’s arguments.