Question

Write a C program called test that takes one command line argument, an integer N. When...

Write a C program called test that takes one command line argument, an integer N. When we run test:

./test N

the program will do this:

the parent process forks N child processes
each child process prints its process ID, exits
the parent process waits for all child processes to exit, then exits
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hii.. Please check the below ...

Test.c

#include<stdio.h>
int main(int argc, char** argv)
{
  
int n;
sscanf(argv[1], "%d", &n);
for(int i=0;i<n;i++)
{
if(fork() == 0)
{
printf("child[%d] pid %d from [parent] pid %d\n",i,getpid(),getppid());
exit(0);
}
}
for(int i=0;i<n;i++)
wait(NULL);
  
}

Output:
child[1] pid 14452 from [parent] pid 14450
child[0] pid 14451 from [parent] pid 14450
child[2] pid 14453 from [parent] pid 14450
child[3] pid 14454 from [parent] pid 14450


Please check the above and let me know any issues. Thank you. All the best.

Add a comment
Answer #2

#include<stdio.h>
int main()
{

int n;
printf("Please enter the number of child processes: ");
scanf("%d",&n);
// loop will run n times
for(int x=0;x<n;x++)
{

if(fork() == 0)
{

printf("[child] process id %d from [parent] process id %d\n",getpid(),getppid());
exit(0);

}

}

// loop will run n times
for(int i=0;i<n;i++) wait(NULL);

}

OUTPUT:-

UT10. J y ! Thompatible implicit declaration of built-in function exit' main.c:14:13: note: include <stdlib.h>' or provide a declaration of 'exit' main.c:18:5: warning: implicit declaration of function 'wait' [-Wimplicit-function-declaration) Please enter the number of child processes: 6 (child) pid 32327 from [parent] pid 32326 (child) pid 32328 from [parent] pid 32326 (child) pid 32329 from [parent] pid 32326 (child) pid 32330 from [parent] pid 32326 (child) pid 32331 from [parent] pid 32326 (child) pid 32332 from [parent] pid 32326

Add a comment
Know the answer?
Add Answer to:
Write a C program called test that takes one command line argument, an integer N. When...
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
  • Part 1: Write a C program that takes an integer command line argument n, spawns n...

    Part 1: Write a C program that takes an integer command line argument n, spawns n processes that will each generate a random numbers between -100 and 100, and then computes and prints out the sum of these random numbers. Each process needs to print out the random number it generates. name the program 003_2.c

  • You are required to write a C program on Unix/Linux in which the parent process creates...

    You are required to write a C program on Unix/Linux in which the parent process creates three child processes, lets them run concurrently, and waits for them to return and prints their exit status. The three child processes are assigned different tasks. Child one is to calculate and display the highest mark of a class of ten students for a unit. Child one is required to get the marks from the standard input (i.e. the keyboard). Child two is to...

  • Need help writing four short recursive programs: Write a program called Permutations.java that takes an integer...

    Need help writing four short recursive programs: Write a program called Permutations.java that takes an integer command-line argument, n, and enumerates all permutations of the first n letters in the English alphabet. Be sure to handle exceptions of the types: i) bad user input (i.e., double, string), ii) n > 26 or n < 0. Write a program called PermutationsK.java that takes two integers, n and k, as command-line arguments and prints out all P(n,k) permutations that contain exactly k...

  • Q4) Write a C program named "hw3q4.c” that takes three string inputs from the command line...

    Q4) Write a C program named "hw3q4.c” that takes three string inputs from the command line arguments that represent file names. The first and second files contain four sorted integer numbers that represent set elements. The main process creates a child process that shares the three files. The child process determines the intersection set of two sets and saves the line: "Child process PID: xxxx Intersection of (x, x, x, x) and (y, y, y, y) (z, z, z, z)"...

  • Write a C program for Linux called pipes.c that does the following: In the main() function,...

    Write a C program for Linux called pipes.c that does the following: In the main() function, it creates a pipe using the pipe() function, then creates two child processes with fork(). Child 1 redirects stdout to the write end of the pipe and then executes with execlp() the "ps -aux" command. Child 2 redirects its input from stdin to the read end of the pipe, then it executes the "sort -r -n -k 5" command. After creating both children, the...

  • In C write a program that must accept one argument from the command line. The argument...

    In C write a program that must accept one argument from the command line. The argument is the name of the file containing the processes (for example: processes.csv).This file is comma-separated with three columns (process ID, arrival time and burst time) with each row for an individual process. You can assume that this file will have a maximum of 10 processes. For example Input: processes.csv ProcessID,Arrival Time,Burst Time 0,1,3 1,0,5 2,9,8 3,10,6 Where the first element is processes, the second...

  • Java!!! Write a program that takes a file name as its command line argument. It assumes...

    Java!!! Write a program that takes a file name as its command line argument. It assumes the file is a binary data file and checks the first 4 bytes of the file to see whether they contain the integer −889275714. It outputs “yes” or “no” or an error message if there was an IOException generated. (Trivia question: Why test for that particular value? Hint: Try it on several .class files.)

  • 1) Write a complete C or C++ program to print Hello World Greetings. 2) Using the...

    1) Write a complete C or C++ program to print Hello World Greetings. 2) Using the command line, compile and generate the executable for the above program. Let’s call helloWorld the target executable. 3) Write a C program that does the following: a) forks a child to execute helloWorld b) waits for the child execution to end 4) Reuse the above program to try a different variant of exec family of system calls. OPTIONAL: 1) write a program main that...

  • Please I need help with this problem Write a c++ program that will accept the names...

    Please I need help with this problem Write a c++ program that will accept the names of 3 processes as command-line arguments. Each of these processes will run for as many seconds as (PID%10)*3+5 and terminate. After those 3 children terminated, the parent process will reschedule each child. When all children have been rescheduled 3 times, the parent will terminate. Each child must print out its process id every time it runs.

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

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