API Flashcards

1
Q

What does fork() do? What does it return?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does wait() do? What does it return?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does exec() do?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How would you use execvp()?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does malloc() do?

A

returns a pointer to an area of memory allocated on the heap.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does free() do?

A

frees up memory allocated on the heap?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How might we allocate space for a String using malloc?

A

malloc(strlen(s) + 1)

So, we might do:
char *src = “Hello”;
char *dst = malloc(strlen(src) + 1);
strcpy(dst, src);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are some common errors with malloc and free?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Are malloc and free system calls or library calls?

A

library calls.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the brk pointer? How do we move it? How can we use sbrk()?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What will sbrk(0) return?

A

The current position of the brk pointer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

True or false, brk and sbrk show us addresses in physical memory?

A

False, they deal with the virtual heap.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can we use mmap()?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are some differences between sbrk and mmap?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How would you use pthread_create()?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How would you use pthread_join()? What is something to avoid here?

A

The first argument of pthread_join is the thread we are waiting to complete, and the second argument is a pointer to the value you expect to get back. Don’t return a value from a thread which was created on the thread’s stack!

17
Q

What does dup2() do?

A

dup2(old fd, new fd) will redirect the old directory into the new directory. So for example, if we had opened a file hello.txt, and we performed dup2(hello.txt, 1), this would redirect anything printed to standard output to hello.txt

18
Q

What is the pid of -bash and /init ?

A

7 and 6

19
Q

What are groups and sessions of processes, and how can we find these out?

A

Parents and child processes belong to the same group, that of the parent. Sessions are collections of groups, for example when we open a terminal a session is created, in which we could create groups.

We could find our pid with getpid()
We could find our parent pid with getppid()
We could find our parent’s group id with getpgid(child_pid)
We could find out session id with getsid(child_pid)

20
Q

How do you initialise a mutex or a conditional variable?

A

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

21
Q

How do we use a pthread lock?

A

pthread_mutex_lock(&mutex);
// critical section
pthread_mutex_unlock(&mutex);

22
Q

How do we use a pthread conditional variable?

A

pthread_mutex_lock(&mutex);
condition = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);

////// somewhere else

pthread_mutex_lock(&mutex);
if(condition == 0)
{
pthread_cond_wait(&cond);
}
printf("Complete\n");
pthread_mutex_unlock(&mutex);
23
Q

What are the three main functions when using semaphores?

A

sem_init()
sem_wait()
sem_post()

24
Q

How do you initialise a semaphore?

A

sem_init(&sem, 0, value)

where 0 indicates that the semaphore is being shared between threads, and value is the value we initialise the semaphore at.
If we are implementing a lock, we initialise value to 1, but if we are implementing a conditional variable, we set value to 0