You are on page 1of 2

Operating Systems Course: 320202

Jacobs University Bremen Date: 2012-02-15


Dr. Jürgen Schönwälder Time: 10 min.

Quiz Sheet #1

Problem 1.1: function calls and system calls (2+2 = 4 points)

a) Consider the following C function:

int wordcount(const char *s)


{
int i = 0, w = 0;

if (s == NULL) {
return 0;
}

while (s[i]) {
while (s[i] && isspace(s[i])) i++;
if (s[i]) w++;
while (s[i] && !isspace(s[i])) i++;
}

return w;
}

Depict the structure of the stack frame being used when this function is called on a 32-bit Intel
x86 processor.
b) Explain differences between a function call and a system call.

Solution:

a) The stack frame has the following structure:

s (4 byte)

instruction pointer (4 byte)

frame pointer (4 byte)

i (4 byte)

w (4 byte)

b) A system call causes a change of privileges. As such, a system call requires special machine
instructions (e.g. int 80 in Intel x86 processors) and argument passing is usually also different
for system calls.
System calls are commonly identified by a number and a system call table residing in the
kernel is then used to lookup the function implementing the system call (e.g., by indexing into
the system call table with the system call number).
During the execution of the kernel functions implementing a system call, a separate stack is
commonly used.
(any correct statement counts as a 1pt, 2pt maximum)
Problem 1.2: fork() (2+2+2 = 6 points)

Consider the program shown below and try to figure out the number of processes it creates during
its execution. Recall that argv[0] contains the program name while arguments are stored in
argv[1], argv[2], . . . , argv[argc-1]. You can assume that all function and system calls succeed
at runtime.

#include <unistd.h>

int main(int argc, char *argv[])


{
int c;

for (c = 0; c < argc; c++) {


(void) fork();
}

return 0;
}

First consider the structure of the resulting process tree if the program is called without any argu-
ments - how many processes do you get? Then consider the structure of the resulting process tree
if the program is called with one argument - how many processes do you get in this case? Then
consider two arguments - how many processes do you get now and what is the growth rate if you
increase the number of arguments?

Solution:

1. If the program is called without any arguments (argc has the value of 1), the following hap-
pens: The body of the for loop is executed once and hence a single child process is created.
As a result, zero arguments lead to 2 processes (including the initial process).

2. If the program is called with a single argument (argc has the value of 2), the following hap-
pens: The body of the for loop is executed twice and two child processes are created. The
first forked child process will continue executing the loop on its own and create another child
process.
As a result, one argument leads to 4 processes (including the initial process).

3. If the program is called with two arguments (argc has the value of 3), the following happens:
The body of the for loop is executed three times. The first created child will create two
more child processes and the second created child with create one more child process.
Furthermore, the first grandchild process will execute the loop once more and create another
child process.
As a result, two arguments lead to 8 processes (including the initial process).
4. Similarly, if the program is called with three arguments (argc has the value of 4), 16 processes
(including the initial process) will be created.
5. If the program is called with N arguments (argc has the value of N + 1), 2(N +1) processes
(including the initial process) will be created.

You might also like