1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
max2010maxim [7]
3 years ago
12

Write a parent program to fork(2) n processes where n is passed tothe program on the command line, and n can be from 1 to 10. Al

l output should bemade with fprintf to the stdout or stderr (followed immediately by an fflush). Follow these directions exactly!
Flag ALL errors, including those returned from any functions with output tothe stderr, or with a call to perror and then exit with the exit(3) function (ifapplicable).In the forked (child) program (recall fork(2) returns 0 to the child process),get the process ID, and current time; using the current time generate arandom wait time between 1 and 10 (seconds). Instead of srand(3) andrand(3), use srandom(3) and random(3) to generate these random numbers,using a seed equal to the current time (type time_t) * the process ID (typepid_t). This guarantees that the seed is different in every child. The child thensleeps for the random number of seconds (see sleep(3)), and returns usingthe exit(3) call with the exit status equal to the number of seconds slept.In the parent (recall fork(2) returns to the parent the process id of the child)save the process IDs in the order returned in an array of process IDs. Theparent then loops through the process ID array and waits for each process IDto exit (using waitpid(2)) in the order in which they were started.To get and display the current time use time(3) with a NULL parameter, andthen call ctime(3) with the value returned by the time(3) call.Use the WIFEXITED and WEXITSTATUS macros on the status returned bywaitpid(2) to determine the return status of children with normal returns(see the text section 8.6). Do NOT use the WNOHANG option with the waitpidcall.Both the parent and child processes will output information to the stdout andstderr.
Computers and Technology
1 answer:
MrRissso [65]3 years ago
6 0

Answer:

Complete code is given below:

Explanation:

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

#include <time.h>

#include <sys/wait.h>

int main(int argc , char *argv[]){

pid_t mypid, childpid;

int status;

int i,n;

time_t t;

     

mypid = getpid();

printf("pid is %d.\n", mypid);

childpid = fork();

if ( childpid == -1 ) {

perror("Cannot proceed. fork() error");

return 1;

}

 

 

if (childpid == 0) {

 

 

mypid = getpid();

 

 

childpid = fork();

if ( childpid == -1 ) {

perror("Cannot proceed. fork() error");

return 1;

}

 

if (childpid == 0) {

 

mypid = getpid();

printf("Child pid is %d.\n", getppid());

 

childpid = fork();

 

 

if ( childpid == -1 ) {

perror("Cannot proceed. fork() error");

return 1;

}

random((unsigned) time(&t));

printf(" parent id = %d : Random = %d\n",mypid , (int)(random()% 100 +1));

printf("child id = %d : Random = %d\n",getpid , (int)(random()% 100 +1));

 

if (childpid == 0) {

 

printf("Child 2: I hinerited my parent's PID as %d.\n", mypid);

 

mypid = getpid();

printf("Child 2: getppid() tells my parent is %d. My own pid instead is %d.\n", getppid(), mypid);

 

sleep(30);

return 12;

} else return 15;

} else {

 

while ( waitpid(childpid, &status,0) == 0 ) sleep(1);

 

if ( WIFEXITED(status) ) printf("Child 1 exited with exit status %d.\n", WEXITSTATUS(status));

else printf("Child 1: child has not terminated correctly.\n");

}

} else {

printf(" fork() is ok and child pid is %d\n", childpid);

wait(&status);

 

if ( WIFEXITED(status) ) printf(" child has exited with status %d.\n", WEXITSTATUS(status));

else printf(" child has not terminated normally.\n");

}

 

return 0;

}

Output:

pid is 24503.

Child   pid  is 24565.

parent id = 24566 : Random = 87

child id = 1849900480 : Random = 78

pid is 24503.

Child 1  exited with exit status 15.

pid is 24503.

fork() is ok and child pid is  24565

child has exited with status 0.

You might be interested in
You’ve received a tarball called data79.tar from a colleague, but you want to check the names of the files it contains before ex
Nina [5.8K]

Answer:

D. tar rvf data79.tar

4 0
3 years ago
A(n) __________ is a common list operation used in programming. its purpose is to iterate through a list of items, one item at a
Nataliya [291]
I would say C- Priority List.
I hope this helps! :)
5 0
3 years ago
Define a function begins_with_line that consumes a string and returns a boolean indicating whether the string begins with a dash
Ksju [112]

Answer:

The program written in python is as follows:

def begins_with_line(userinut):

     while userinut[0] == '-' or userinut[0] == '_':

           print(bool(True))

           break;

     else:

           print(bool(not True))

userinput = input("Enter a string: ")

begins_with_line(userinput)

Explanation:

The program makes use of no comments; However, the line by line explanation is as follows

This line defines the function begins_with_line with parameter userinut

def begins_with_line(userinut):

The following italicized lines checks if the first character of user input is dash (-) or underscore ( _)

<em>      while userinut[0] == '-' or userinut[0] == '_': </em>

<em>            print(bool(True))  </em><em>->The function returns True</em>

<em>            break; </em>

<em>However, the following italicized lines is executed if the first character of user input is neither dash (-) nor underscore ( _)</em>

<em>      else: </em>

<em>            print(bool(not True))  </em><em>-> This returns false</em>

The main starts here

The first line prompts user for input

userinput = input("Enter a string: ")

The next line calls the defined function

begins_with_line(userinput)

<u><em>NB: The program does not make use of if statement</em></u>

8 0
3 years ago
Write a program whose input is two integers and whose output is the two integers swapped.
Tpy6a [65]

Answer:

The program to this question can be given as:

Program:

#include <iostream>  //header file

using namespace std;   //using namespace.

void SwapValues(int* userVal1, int* userVal2); //function declaration.

void SwapValues(int* userVal1, int* userVal2) //function definition.

{ //function body.

//perform swapping

   int t = *userVal1;  

   *userVal1 = *userVal2;

   *userVal2 = t;

}

int main() //main method  

{

int n1, n2; //define variable

cout<<"Enter first number :"; //message

cin>>n1; //input by user.

cout<<"Enter second number :"; //message  

cin>>n2; //input by user.

SwapValues(&n1,&n2); //calling function.

cout<<"Swapped values"<<endl;

cout<<"first number is :"<<n1<<endl; //print value

cout<<"second number is:"<<n2<<endl; //print value

return 0;

}

Output:

Enter first number :3

Enter second number :8

Swapped values

first number is :8

second number is :3

Explanation:

The description of the above C++ language program can be given as:

  • In the program, firstly we include the header file. Then we declare and define a function that is "SwapValues()" function in the function we pass two integer variable that is "userVal1 and userVal2" inside a function, we define an integer variable that is "t" and perform swapping.  
  • Then we define the main function in the main function we define two variables that is "n1 and n2" this variable is used to take value-form user. then we pass this value to function and print the function values.

5 0
3 years ago
Whoever helps me with these questions i will help them with the same number of questions in any subject: 1. As the operations ma
mezya [45]
I'm going have to go with with 1.b and 2.d hope this helps and I don't need help right now.
6 0
3 years ago
Other questions:
  • Write a unit test for addInventory(). Call redSweater.addInventory() with parameter sweaterShipment. Print the shown error if th
    9·2 answers
  • In cell F29, use an IF function to display the correct Shipping Charge, based on the amount of the Discounted Total. If the Disc
    9·1 answer
  • When you use the Filter feature, what appears in each column label
    11·1 answer
  • Achieves integration through several different types of software that sit between and provide connectivity for two or more softw
    11·1 answer
  • To create nested lists within each of these topical areas that would contain links to specific pages on the AllStyles website. I
    15·1 answer
  • #5 Multiple Select Which of the following describes a hardware error? Select 3 options.
    7·2 answers
  • Alcohol first impairs your ____?
    12·2 answers
  • 1. The Law of Superposition – the age of an object may be determined by the depth at which it is found, the deeper the object is
    14·1 answer
  • Pls answer dis question when i'm not connected and i open bluestacks 5 it will be working but if i am connected it wil not work
    14·1 answer
  • Nearly all social software systems include a(n) ________, which helps control your information flow.
    6·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!