Question

In: Computer Science

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

Solutions

Expert Solution

Code:

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

// function to work with fork
void printer(int n)
{
    // forks n processes
    for(int i=0; i<n; i++)
    {
      int pid = fork();
      // if it is child then print it's id
      if(pid == 0)
      {
        printf("%d ", getpid());
        exit(0);
      }
      // if parent then wait until the child executes
      else
        wait(0);
    }
    // execution of all processes completes
    // Note: This line is optional
    printf("\nAll childs execution completed!!");
}
int main(int argc, char const *argv[])
{
    // print invalid argument if no.of argumets are not 2 and given value is not number
    if(argc != 2 && !isdigit(argv[argc-1]))
    {
        printf("invalid argumets");
        return 0;
    }
    // since, first argumets is string we have to convert it to integer using atoi function
    // then pass it to the printer function
   printer(atoi(argv[argc-1]));
   return 0;
}

Command line argument:

10

Output:

Hope your problem solved and you are appreciating this solution.

Feel free to comment doubts in comment section .


Related Solutions

Write a program that takes an integer N from the command line and uses StdRandom.uniform() to...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to generate a random sequence of integers be- tween 0 and N – 1. Run experiments to validate the hypothesis that the number of integers generated before the first repeated value is found is ~√?N/2.
Write a C or C++ program A6p2.c(pp) that accepts one command line argument which is an integer n between 2 and 4 inclusi...
Write a C or C++ program A6p2.c(pp) that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 49 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12];). Use pthread to create n threads to square all 60 array elements. You should divide this update task among the n threads as evenly as possible. Print the array both before and after the...
Python Write a program that takes a text filename as command line argument, and prints number...
Python Write a program that takes a text filename as command line argument, and prints number of times each letter occurred in this file.
Write a C program that accepts a port number as a command line argument, and starts...
Write a C program that accepts a port number as a command line argument, and starts an HTTP server. This server should constantly accept() connections, read requests of the form: GET /path HTTP/1.1\r\n\r\n read the file indicated by /path, and send it over the "connect" file descriptor returned by the call to accept().
Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
A C program that accepts a single command line argument and converts it in to binary...
A C program that accepts a single command line argument and converts it in to binary with array length of 16 bits. The array should contain the binary of the int argument. the program should also convert negative numbers. Side note the command line arg is a valid signed int.
program c Write a program called filesearch that accepts two command-line arguments: A string A filename...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename If the user did not supply both arguments, the program should display an error message and exit. The program opens the given filename. Each line that contains the given string is displayed. Use the strstr function to search each line for the string. You may assume no line is longer than 255 characters. The matching lines are displayed to standard output (normally the screen).
Write a program that takes two command line arguments at the time the program is executed....
Write a program that takes two command line arguments at the time the program is executed. You may assume the user enters only decimal numeric characters. The input must be fully qualified, and the user should be notified of any value out of range for a 23-bit unsigned integer. The first argument is to be considered a data field. This data field is to be is operated upon by a mask defined by the second argument. The program should display...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and returns a double. When the variable argument phone contains the character a or A print the word Apple and return 1099.99 When phone contains anything else return a 0.0
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT