Question

Q1) Including the initial parent process, how many processes are created by the program shown in...

Q1) Including the initial parent process, how many processes are created by the program shown in Figure 1? Give an explanation for your answer. You can include an output statement to test and run the program. A sample output statement could be: printf("Testing......");

#include <stdio.h> #include <unistd.h>

int main() { /*fork a child process*/ fork();

/*fork another child process*/ fork();

/*fork another child process*/ fork(); return 0; } Figure 1a - How many processes are created?

Another version, now displaying the process ids.

#include <stdio.h> #include <unistd.h>

int main() { printf("%d\n",getpid()); fork(); printf("%d\n",getpid());   

fork(); printf("%d\n",getpid());

fork(); printf("%d\n",getpid());

return 0; }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Question 1 - How many processes are created?

Answer: 8

Explanation:

Total Number of Processes = 2n, where n is number of fork system calls.

We have totally 3 fork() calls here. So, 23 = 8 processes.

  

Sample program:

#include <stdio.h>
#include <unistd.h>

int main() {
/*fork a child process*/ fork();
/*fork another child process*/ fork();
/*fork another child process*/ fork();
printf("Testing......\n");
return 0;
}

Sample Output:

Question 2 - with process id

Consider the main process as P0

When the first fork is called, it creates one child(P1) for P0. So we have two process id ( P0 and P1)

When the second fork is called, it creates one child for each process (P0 and P1). So totally it creates two process (P2 for P0 and P3 for P1)

Totally we have 4 processes P0, P1, P2, P3

When the third fork is called, it creates one child for each process (P0, P1, P2, P3). P4 for P0, P5 for P1, P6 for P2 and P7 for P3.

#include <stdio.h> #include <unistd.h>

int main() {
   printf("%d\n",getpid());
   fork();
   printf("%d\n",getpid());   
   fork();
   printf("%d\n",getpid());
   fork();
   printf("%d\n",getpid());

return 0; }

Child process runs concurrently with the parent process. After a new child process is created, both processes will execute the next instructions. So each time fork is getting called, system will have additional child processes which executes the same set of instructions.

So the getpid() printed multiple times as the child processes are executing the codes concurrently as the main process.

Sample Output:

5232  
5232  
5232
5232
5232
5232
5232
5457
5232
5232
5456
5456
5232
5455
5455
5455
5232
5232
5456
5458
5232
5455
5455
5460
5232
5455
5459
5459
5232
5455
5459
5461

Feel free to ask any queries and rate the answer.

Happy Studying !!!

Add a comment
Know the answer?
Add Answer to:
Q1) Including the initial parent process, how many processes are created by the program shown in...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • including the initial parent process, how many processes are created by the following programs.

    including the initial parent process, how many processes are created by the following programs.#include<stdio.h>#include<unistd.h>Main(){Int pid1,pid2;Pid1=fork();Pid2=fork();}

  • Question 3: [2+2] a)What, to whom and how many values a fork system call returns? b)Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in

    Question 3:                  [2+2]a) What, to whom and how many values a fork system call returns? b) Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in which Terminal login must be your arid number.int main(){      pid_t  fork_return;        fork_return = fork(); if (fork_return == 0) {                  execlp("/bin/ls", "ls", NULL);                 printf("Child process ID: %d\n", getpid());      exit(0);        }        else { wait (NULL);                 printf("Parent process ID: %d\n", getpid());         }     return 0;}

  • Question 3: [2+2] a)What, to whom and how many values a fork system call returns? b)Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in

    Arid No is 19-arid-898Question 3:                  [2+2]a) What, to whom and how many values a fork system call returns? b) Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in which Terminal login must be your arid number.int main(){      pid_t  fork_return;        fork_return = fork(); if (fork_return == 0) {                  execlp("/bin/ls", "ls", NULL);                 printf("Child process ID: %d\n", getpid());      exit(0);        }        else { wait (NULL);                 printf("Parent process ID: %d\n",...

  • Problem la Points (20) Draw the process tree for the program shown below. #include<sys/types.h> #include<stdio.h> #include<unistd.h>...

    Problem la Points (20) Draw the process tree for the program shown below. #include<sys/types.h> #include<stdio.h> #include<unistd.h> int value = 5; int main() pid_t pid; pid = forko; if(pid ==0) { */ Child Process */ forkO; value += 15; return 0; else if (pid > 0) {/* Parent process */ forkO; forkO; wait(NULL); printf("Parent: value = %d", value); /* LINE A */ return 0;

  • Write code that forks into two processes: a parent process, and a child process. Your code...

    Write code that forks into two processes: a parent process, and a child process. Your code will be called with command-line arguments consisting of negative integers. Do not worry about bad command-line arguments such as "xyz". Your code will not be tested in this way. The parent process will take the arguments to main(), convert them into ints by calling atoi(), and send those ints one at a time to the child process through a pipe (one call to write()...

  • Write code that forks into two processes: a parent process, and a child process. Same as...

    Write code that forks into two processes: a parent process, and a child process. Same as the Regular version, except that your code must also be able to handle negative integers input from the command-line. If I call your code this way:     a03 -3 5 -7 The parent process should print out: Assignment 3     sum = -5 The sums produced from the test input I use will be in the range [-128 .. 127] -------------------------------------------------------------------------------------------------------------------- // Numbers from...

  • Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork...

    Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork a child process*/ pid = fork(); if (pid<0){ /*error occured*/ fprintf(stderr,"Fork failed\n"); return 1; } else if (pid == 0) { /*child process */ printf("I am the child %d\n",pid); execlp("/bin/ls","ls",NULL); } else { /*parent process */ /*parent will wait for the child to complete*/ printf("I am the parent %d\n",pid); wait(NULL); printf("Child Complete\n"); } return 0; } 1. Write program codes for 3.21 a and...

  • Suppose that you have three programs that are suppose to print a house diagram in a...

    Suppose that you have three programs that are suppose to print a house diagram in a collaborative manner. (i) prog1.c #include <stdio.h>      /* Input/Output */ #include <stdlib.h>     /* General Utilities */ int main() {     printf(“                      ^^^^^^^^^^^^                       \n”);     printf(“           ^^^^^^^^^^^            ^^^^^^^^^^^            \n”);     printf(“^^^^^^^^^^^                                  ^^^^^^^^^^^ \n”);     printf(“     |                                            |      \n”);     printf(“     |                                            |      \n”);     exit(1); } (i) prog2.c #include <stdio.h>      /* Input/Output */ #include <stdlib.h>     /* General Utilities */ int main() {     printf(“     |                                             |    ...

  • Rewrite the program as a set of two programs that utilize a named pipe “ages” in...

    Rewrite the program as a set of two programs that utilize a named pipe “ages” in the current folder for IPC and implement the same function as discussed above. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> int main(void) {         int     fd[2], nbytes;         pid_t   childpid;         int users = 95;         char mystring[80];         char    readbuffer[80];         char digit1,digit2;         digit1 = (char) users%10;         digit2 = (char ) users/10;         mystring[0] = digit1;         mystring[1] = digit2;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT