0
int main()
{
    int count = 0;
    int pid;
    if ( !(pid=fork()))
    {
        while (((count<2) && (pid=fork()) ) {
            count++;
            printf("%d",count)
       }
       if(count>0)
       {
           printf("%d", count);
       }
    }
    if (pid) {
        waitpid(pid,NULL,0);
        count = count<<1;
        printf("%d", count)
    }
}

I'm having trouble reading this piece of code. I can't figure out how many system calls are made or even remotely trace them. This is leading me to not be able to understand how the output works. I am assuming the scheduler can do anything. What are all possible outputs and how many system calls are made?

Aleka
  • 242
  • 1
  • 15
  • I included an image of the code, unless you can't see the imgur link. I will add the code in also. – Aleka Jan 27 '18 at 04:35
  • To count system calls in xv6 you have two options, count them manually, or write a program to do so. For manual counting, a list of available system calls can be found in [user.h](https://github.com/mit-pdos/xv6-public/blob/34f060c3dcf3bf3dde683df9ff9872bc9f1d5d14/user.h#L4). In the code above, `fork` and `wait` are explicit system calls. If you look at the code for `printf`, you will see that it too makes a system call `write`. – Jet Blue Jun 22 '20 at 01:07

1 Answers1

0

Launch with strace -f ./myprogram to trace all system calls.

From looking at the code, about 3 additional processes will be created.

Velkan
  • 7,067
  • 6
  • 43
  • 87
  • Just a note, `strace` is not availble in xv6. OP listed Linux in their question, but not really correct to do so. xv6 is an ultra simple Unix variant. – Jet Blue Jun 22 '20 at 01:01