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
What is the organizational management practices on planning during a pandemic like corona virus.
eduard

Answer:

wear a mask use hand sanitizer dont get in contact with anyone between 6 feet

Explanation:

3 0
2 years ago
Shannon has been a member of her schools newpaper club for 2 years and attends writing workshops in her free time. Which career
kkurt [141]

D) Web content developer

Explanation:

A content developer writes a well-researched content for websites, publications, and television. They also write sales copy and other marketing material for online and offline marketing. They develop text, graphics, audios, and videos.  

Skills that help web content developer:

  1. Reading news every day  
  2. Write regularly
  3. Study end audience
  4. Develop original and unique content
  5. Research; reading other people's content as well
  6. Provide solutions through your content

Besides these, a web content writer must possess technical skills like front-end web development. They must know basic HTML formatting and search engine optimization.  

7 0
3 years ago
An organization using Robotic Process Automation (RPA) wishes to verify the quality and results of their automated processes to
damaskus [11]

Answer:

Control Center

Explanation:

The element of RPA (Robotic Process Automation) an organization should use to manage and track their automated processes is known as CONTROL CENTER.

Robotic Process Automation has various elements including:

1. Recorder

2. Development Studio

3. Plugin/Extension

4. Bot Runner

5. Control Center

Among these elements are however is the CONTROL CENTER which is considered the most significant element and functions as source control.

It enables the users to plan, manage, control, and measure the movement of a huge amount of digital actions.

4 0
2 years ago
Write the code to produce a for loop that counts down from 20 to 0 by 2’s and prints the number each time it goes through the lo
yulyashka [42]

Explanation:

Count-controlled for loop (Three-expression for loop)

This is by far the most common type. This statement is the one used by C. The header of this kind of for loop consists of a three-parameter loop control expression. Generally it has the form:

for (A; Z; I)

A is the initialisation part, Z determines a termination expression and I is the counting expression, where the loop variable is incremented or dcremented. An example of this kind of loop is the for-loop of the programming language C:

for (i=0; i <= n; i++)

This kind of for loop is not implemented in Python!

Numeric Ranges

This kind of for loop is a simplification of the previous kind. It's a counting or enumerating loop. Starting with a start value and counting up to an end value, like for i = 1 to 100

Python doesn't use this either.

Vectorized for loops

They behave as if all iterations are executed in parallel. This means, for example, that all expressions on the right side of assignment statements get evaluated before the assignments.

Iterator-based for loop

Finally, we come to the one used by Python. This kind of a for loop iterates over an enumeration of a set of items. It is usually characterized by the use of an implicit or explicit iterator. In each iteration step a loop variable is set to a value in a sequence or other data collection. This kind of for loop is known in most Unix and Linux shells and it is the one which is implemented in Python.

Example of a simple for loop in Python:

>>> languages = ["C", "C++", "Perl", "Python"]  

>>> for x in languages:

...     print(x)

...  

C

C++

Perl

Python

>>>

3 0
2 years ago
Define the term editing​
Harrizon [31]

Answer:

editing is a word file mean making changes in the text contain is a file. or a word file is one of the most basic ms office word operation.

8 0
2 years ago
Other questions:
  • Select the correct answer.
    12·2 answers
  • (Analyze scores) Write a program that reads an unspecified number of scores and determines how many scores are above or equal to
    10·1 answer
  • In this part, you have to implement a linked list that maintains a list of integers in sorted order. Thus, if the list contains
    13·1 answer
  • What is the best way to locate where my C program gets into an infinite loop
    5·1 answer
  • What are the 2 things you are not sure about evaluating functions​
    7·2 answers
  • Richard needs to copy information from another slide presentation that uses a different design template. To ensure that the info
    10·1 answer
  • Is Brainly cheating??
    10·2 answers
  • WILL MARK BRAINLIEST!!!!!!!!!!!
    6·2 answers
  • Can development and conservation of environment go hand-in-hand? Explain your point of view
    8·1 answer
  • What is the importance of test documentation?
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!