Question

2. Calculate number of times hello is printed. #include <stdio.h> #include <sys/types.h> int main() { fork();...

2. Calculate number of times hello is printed.


#include <stdio.h>

#include <sys/types.h>

int main() {
fork();

fork();

fork();
printf("hello\n");

return 0;
}

Homework Answers

Answer #1

The image above shows how the fork() calls will be executed in this case, and how many copies of each process will be made. Clearly, as shown above, there are a total of 8 calls made at the end. The printf() statement exists right at the end of the program hence it executes AFTER all the child processes have been created.

A brief summary:

  1. The parent process makes one fork() call, which means we now have two processes.
  2. Now, each process now makes a fork() call, which means we have two copies of the two processes ie 4 processes.
  3. The final fork() call is made, then we have two copies of the four processes i.e. 8 processes.

When we execute the code above, we get:

Which establishes that the number of times the printf() statement is called is indeed 8.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please explain what will be printed out at LINE1 and LINE2? #include <stdio.h> #include <types.h> int...
Please explain what will be printed out at LINE1 and LINE2? #include <stdio.h> #include <types.h> int data[4] = {1,3,5,7}; void *runner (void *param); int main(int argc,char *argv[]) { pid_t pid; pthread_t tid; pid = fork(); if (pid==0){ pthread_create(&tid,NULL,runner,NULL); pthread_join(tid,NULL); for(int i=0,i<4,i++) printf("from child process, the values at i is %d",data[i]); } else if (pid>0){ wait(NULL); for(int i=0;i<4,i++) printf("from parent process, the values at i is %d",data[i]); } } void *runner(void *param){ for(int i=0;i<4,i++) data[i]+=3*i; pthread_exit(0); } } } } }
CS4315 Operating Systems Lab 2: Linux Processes This lab assignment contains three questions. To submit this...
CS4315 Operating Systems Lab 2: Linux Processes This lab assignment contains three questions. To submit this lab assignment, please use a Word document to include the screenshots and write your answer. 1. Run the following C program, and submit a screenshot of the result. #include <sys/types.h> #include <stdio.h> #include <unistd.h> int main( ) { pid_t pid; if ( (pid = fork()) == 0 ) { printf (“I am the child, my pid = %d and my parent pid = %d\n”,...
What is output? #include <stdio.h> void CheckValue(int* pointVar1, int* pointVar2) {       if (pointVar1 == NULL...
What is output? #include <stdio.h> void CheckValue(int* pointVar1, int* pointVar2) {       if (pointVar1 == NULL && pointVar2 == NULL) {       printf("Pointer is null\n");    }    else if (*pointVar2 > *pointVar1) {       printf("%p\n", *pointVar2);    }    else if (*pointVar1 > *pointVar2) {       printf("%p\n", *pointVar1);    } } int main() {    int num1 = 5;    int num2 = 9;       CheckValue(&num1, &num2);           return 0; } a. 0 b. 5 c. 9 d. Pointer is null e....
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int...
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int array[] = { 1, 1, 1, 3 }; unsigned long long r = sum(array, sizeof(array) / sizeof(*array)); printf("Result is: %llu (%s)\n", r, r == 6 ? "correct" : "incorrect"); return 0; } Complete the code for the line that calls the sum function. Write the sum function.
Using the program below, identify the values of pid at lines A, B, C, and D....
Using the program below, identify the values of pid at lines A, B, C, and D. (Assume that the actual pids of the parent and child are 2600 and 2603, respectively.)   #include <sys/types.h>   #include <stdio.h>   #include <unistd.h>      int main()   {   pid_t pid, pid1;        /* fork a child process */     pid = fork();        if (pid lt; 0) { /* error occurred */       fprintf(stderr, "Fork Failed");       return 1;     }     else if (pid == 0) { /* child process */       pid1 = getpid();       ...
Run the below code and shift binary left and right and understand the output. #include <stdio.h>...
Run the below code and shift binary left and right and understand the output. #include <stdio.h> int main() {     int num=212, i;     for (i=0; i<=2; i++)         printf("Right shift by %d: %d\n", i, num>>i);      printf("\n");      for (i=0; i<=2; i++)         printf("Left shift by %d: %d\n", i, num<<i);             return 0; } Output:
Compile and run the following code and presuming your executable is in a.out, run this command...
Compile and run the following code and presuming your executable is in a.out, run this command from the shell prompt: ./a.out ; echo "Parent Terminated Normally with a return value of $?" Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Hint: This has everything to do with how processes “communicate” their exit...
What would appear in the console window after the code below executes? #include <stdio.h> int main()...
What would appear in the console window after the code below executes? #include <stdio.h> int main() { char myLetter = 'A'; printf("The value of A is %d", myLetter); }
#include <stdio.h> #include <stdlib.h> int main () { int k; int aux; int size = 8;...
#include <stdio.h> #include <stdlib.h> int main () { int k; int aux; int size = 8; int x[8] = {2, 3, 5, 7, 11, 13, 17, 19}; scanf("%d",&k); aux = x[k]; for (int i = k; i < size - 1; i++) x[i] = x[ i + 1]; x[ size - 1] = aux;   for (int i = 0; i < size; i++) printf("%d ", x[i]); } change this program to Write a program to remove an element from an...
#include <stdio.h> /* Returns the number of occurrences of string t in s * * Example:...
#include <stdio.h> /* Returns the number of occurrences of string t in s * * Example: * substring_n("hello", "el") => 1 * substring_n("", "ab") => 0 * substring_n("utilities", "ti") => 2 * */ int substring_n(char *s, char *t) { return -1;
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT