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
PLEASE ANSWER QUICK
butalik [34]

Answer: C

Explanation:

8 0
3 years ago
Read 2 more answers
__________ contain(s) remnants of word processing documents, e-mails, Internet browsing activity, database entries, and almost a
sergey [27]

<u>Swap files </u>contain(s) remnants of word processing documents, e-mails, Internet browsing activity, database entries, and almost any other work that has occurred during past Windows sessions.

Explanation:

I order to simulate extra space an operating system like windows use hard disk space in order to stimulate extra memory.When a computer system is running low with the memory space the swap file swaps a section of RAM onto the hard disk in order to obtain a free memory space.

This Process at times results in  slowing down of the computer  computer considerably.

<u>The combination of RAM and swap files is known as virtual memory.</u>

It is due to the  use of virtual memory that our computer is able to  run more programs than it could run in RAM alone.

4 0
3 years ago
Write a subclass called SavingsAccount that extends Account and adds an interest rate variable. Write a constructor with 3 argum
AveGali [126]
Okayama sintered Nuni 19371
5 0
3 years ago
Consider the following environment with a local dns caching resolver and a set of authoritative dns name servers
Schach [20]

Answer:

Normally in a DNS server, There are no packet losses•All processing delays are 0 ms

Explanation:

If any packet lost in DNS then lost in the connection information will be displayed in prompt. Once the packet is lost then data is lost. So if the packet is lost then data communications are lost or the receiver will receive data packet in a lost manner, it is useless.

To connect in windows normally it will take 90 ms for re-establish connection it normally delays the time in windows. So that end-user he or she for best performance ping the host or gateway in dos mode so that connection or communication will not be lost. If any packet is dropped DNS will get sent the packet from sender to receiver and the ping rate will more in the network traffics.

7 0
3 years ago
Which of the following are vector graphic file formats? Choose all that apply.
ddd [48]

i need pics to see what it is



5 0
3 years ago
Other questions:
  • What kinds of unstructured data or big data might the BITS corporation want to gather in the future? What kind of devices might
    14·1 answer
  • What does ACCU stand for?
    13·2 answers
  • A classmate is texting a false rumor around school that another student was arrested for shoplifting. This behavior can be descr
    9·2 answers
  • Write a program that will use user input. The program should be called PrintNumberToWordie which prints "ONE", "TWO",..., "FIVE"
    9·1 answer
  • There are _______ pints are in 8 cups.
    10·2 answers
  • (Bible)<br> Sin may be an inward thought or an outward act. True False
    11·2 answers
  • Which can be used to decode a Unicode character encoding into text?
    13·1 answer
  • What is the output of the below Java program?
    11·1 answer
  • Pleaseeeeeeee tellllllllllllllllllllll​
    8·1 answer
  • 1) The critical path of a network is the A) shortest time path through the network. B) path with the fewest activities. C) path
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!