Question

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

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

Solution:

#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
void main()
{
   int n,pid,stat;


   printf("Enter the value of n:");
   scanf("%d",&n);
   pid=fork();
  
   if(pid==0)
   {
       int i,sum=0;
       for(i=1;i<=n;i++)
           sum=sum+i;
       printf("Sum of first %d numbers is %d\n",n,sum);
   }
   else
   {
       wait(&stat);
       printf("Child process's pid is %d\n",pid);
   }
}

Output:

Add a comment
Know the answer?
Add Answer to:
Write a program using the fork() system call to do thefollowing. The parent process (main program)...
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 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...

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

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

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

  • The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .......

    The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... It is defined by the following mathematical expression, with X0 & X1 being 0 and 1, respectively: Xn = Xn-1 + Xn-2 Write a C program using the fork() system call that generates and prints the Fibonacci sequence in the child process. The number of members in the sequence will be determined by a user provided as a user prompted input. Make the parent...

  • 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 a program that opens a file (with the open() system call) and then calls fork)...

    Write a program that opens a file (with the open() system call) and then calls fork) to create a new process. Can both the child and parent access the file descriptor returned by open() ? What happens when they are writing to the file concurrently, i.e., at the same time?

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

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

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