Question

Write a program in C using the fork() system call to do the following. The parent process (main program) forks a process...

Write a program in C using the fork() system call to do the following. The parent process (main program) forks a process (CHILD 1) to compute and print the sum of first n integers where n is a variable shared between the parent and CHILD 1. It also forks another process (CHILD 2) that finds the sum of squares of the first n numbers where n is a variable it shares with the parent. Let CHILD 1 print “The sum of the first ** integers is: ****”.
Let CHILD 2 print “The sum of the square of the first ** integers is: ****”. Have the parent invoke the wait () call to wait for both the child processes to complete before exiting the program.

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

SOURCE CODE

#include <unistd.h>
#include <stdio.h>
#include<sys/wait.h>

int main()
{
   int n,i,sum=0,ssum=0;
   printf("\nEnter the value of n :");
   scanf("%d",&n);
          
   int c1 = fork();   // First Child
   int c2 = fork();    // Second Child

   if (c1 > 0 && c2 > 0 )   // PARENT PROCESS
   {
       wait(NULL);
   }
   else if (c1 == 0 && c2 > 0) //First Child Process
{
  
for(i=1;i<=n;i++)
   sum=sum+i;
printf("Sum of first %d integers is %d \n",n,sum);
  
}
else if (c1 > 0 && c2 == 0)   //Second Child Process
{
   for(i=1;i<=n;i++)
   ssum=ssum+(i*i);
printf("Sum of the square of the first %d integers is %d \n",n,ssum);
  
}
   return 0;
}

OUTPUT

Enter the value of n :7
Sum of first 7 integers is 28
Sum of the square of the first 7 integers is 140

please give a upvote if u feel helpful.

Add a comment
Answer #2
Write a program in which child take the input from user and parent is required to print that input?
answered by: rezevaco
Add a comment
Know the answer?
Add Answer to:
Write a program in C using the fork() system call to do the following. The parent process (main program) forks a process...
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
  • Write a program using the fork() system call to do thefollowing. The parent process (main program)...

    Write a program using the fork() system call to do thefollowing. The parent process (main program) forks a process(CHILD 1) to compute and print the sum of first n integers where n is a variable shared between the parent and CHILD 1. Have the parent invoke the wait () call to wait for both the child processesto complete before exiting the program. IN A C program

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

  • Assignment: Using the Fork System Call The Collatz conjecture concerns what happens when we take any...

    Assignment: Using the Fork System Call The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorthm: n={n / 2 , if n is even 3 * n + 1 , if n is odd The conjecture states that when this algorithm is continually applied, all positive integers will eventually reach 1. For example, if n = 35, the sequence is:  35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2,...

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

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

  • Complete the following C code by filling all “???”sto write a C program to create a...

    Complete the following C code by filling all “???”sto write a C program to create a child process. Before the child process is created, an integer variable A is defined and assigned value 10. Then in the child process, A is reduced by 5. After the child process completes, the parent process increases A by 5. Then what will A’s value be just before the parent process completes? Why? Write the code and run it to verify your answer. #include...

  • The Collatz conjecture concerns what happens when we take any positive integer n and apply the...

    The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorithm: if n is even 3 xn+1, if n is odd The conjecture states that when this algorithm is continually applied, all positive integers will eventually reach 1. For example, if n- 35, the sequence 1S 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4,2,'1 Write a C program using the fork) system call that generates this sequence in the...

  • 1. Solve synchronization problems inherent in inter-process communication and multi-threaded applications.

    1. Process control system calls: The demonstration of fork, execve and wait system calls along with zombie and orphan states.a. Implement the C program in which main program accepts the integers to besorted. Main program uses the fork system call to create a new process called a child process. Parent process sorts the integers using merge sort and waits for child process using wait system call to sort the integers using quick sort. Also demonstrate zombie and orphan states.b. Implement...

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

  • CSIT 345 Lab 2 Process Programming Your lab requirement is to write program codes for 3.21 and sh...

    *Help Please with the code** CSIT 345 Lab 2 Process Programming Your lab requirement is to write program codes for 3.21 and shared memory program for producer and consumer as shown in the following. You can start with the code provided in the virtual machine in the virtual box you installed. The code can be found in /home/oscreader/osc9e-src/ch3 a. For 3.21, you can start with the newprocposix.c and modify the code to meet your requirement. Then type: gcc neypCOCROSİS.c to...

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