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
Intel Centrino Technology is the combinatin of wirelesstechnology with the previous HT technology.
Kay [80]

Answer:

<u>False</u>

Explanation:

Great question, it is always good to ask away and get rid of any doubts that you may be having.

The Intel Centrino Technology is a product marketed and sold by the tech giant Intel, which offers a combination of their Wi-Fi and WiMax technologies in a single personal laptop. It does not offer Hyper threading Technology (HT) like some of the other Intel branded CPU's. Some of the CPU's that include hyper threading are the Core I3 series.

Therefore, based on the information stated above, we can see that the statement in the question in <u>False</u>.

I hope this answered your question. If you have any more questions feel free to ask away at Brainly.

7 0
2 years ago
compared to other data structures, this dbms structure is more flexible and stores data as well as instructions to manipulate th
Nuetrik [128]

Object-oriented databases is more flexible and stores data as well as instructions to manipulate the data.

A system known as an object database uses object-oriented programming to represent data in the form of objects. Different from relational databases, which are table-oriented, are object-oriented databases.

The concept of object-oriented programming languages, which is currently popular, is the foundation of the object-oriented data model. Overloading, polymorphism, and inheritance Object-oriented programming fundamentals that have found use in data modelling include object-identity, encapsulation, and information hiding with methods to provide an interface to objects. The rich type system, which includes collection and structured types, is supported by the object-oriented data model as well.

In contrast to relational databases, which can only handle one type of data, object databases can handle multiple data types. Unlike conventional databases like hierarchical, network, or relational, object-oriented databases can handle a variety of data types, including text, numbers, images, voice, and video.

Also, Reusable code, realistic modelling, and increased dependability and flexibility are all provided by object-oriented databases.

To learn more about Object-oriented databases click here:

brainly.com/question/29441658

#SPJ4

7 0
1 year ago
Write a complete Java program called Rooter that gets a positive integer called "start" from the user at the command line and th
Nataly_w [17]

Answer:

The program in Java is as follows:

import java.util.*;

import java.lang.Math;

public class Rooter{

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

 int start;

 System.out.print("Start: ");

 start = input.nextInt();

 while(start<=0){

     System.out.print("Number must be positive\nStart: ");

     start = input.nextInt();  }

 while(start>=0){

     System.out.println(Math.sqrt(start));

     start--;  }

}

}

Explanation:

This declares start as integer

 int start;

This prompts the user for input

 System.out.print("Start: ");

This gets input for start

 start = input.nextInt();

The following is repeated until the user input is valid i.e. positive

<em>  while(start<=0){</em>

<em>      System.out.print("Number must be positive\nStart: ");</em>

<em>      start = input.nextInt();  }</em>

The following while loop prints the square root of each number till 0

<em>  while(start>=0){</em>

<em>      System.out.println(Math.sqrt(start));</em>

<em>      start--;  }</em>

4 0
2 years ago
Match the column.<br> Description Shortcut key<br> Move to next cell in row page up
Musya8 [376]

The matchup are:

1) move to next cell in row - tab

2) move to previous cell in row - shift+tab

3) up one screen - page up

4) down one screen - page down

5) move to next worksheet - ctrl + page down

6) move to previous worksheet - ctrl + page up

7) go to first cell in data region - ctrl+ home

8) go to last cell in data region - ctrl + end

<h3>What are computer shortcut keys?</h3>

A computer shortcut is known to be some group of one or a lot of keys that brings about a command in software or in any kind of operating system.

Hence, The matchup are:

1) move to next cell in row - tab

2) move to previous cell in row - shift+tab

3) up one screen - page up

4) down one screen - page down

5) move to next worksheet - ctrl + page down

6) move to previous worksheet - ctrl + page up

7) go to first cell in data region - ctrl+ home

8) go to last cell in data region - ctrl + end

Learn more about computer shortcut from

brainly.com/question/12531147

#SPJ1

4 0
1 year ago
Are there strategies or better solution that you can suggest besides paying for bags, which could reduce plastic bag pollution
astraxan [27]
The most obvious one would be recycling, another one would be to use plastic bags to hold things in your house, instead of boxes.  I run a business, and my business requires me to ship a lot.  I often use plastic bags as packaging material.  Hope this helps!
8 0
3 years ago
Other questions:
  • true or false: for most queries, the user location changes our understanding of the query and user intent.
    13·1 answer
  • Help me Please?!! I will put you as brainliest.<br>I hope I spelled that right.
    5·2 answers
  • Which of the following is not a commodity?<br>A. Crude oil<br>B. Corn dogs<br>C. Cocoa<br>D. Coffee​
    10·2 answers
  • How to transfer photos from iphone to computer
    14·1 answer
  • The icon below represents the ____________.
    8·1 answer
  • A(n) __________ item is a hardware or software item that is to be modified and revised throughout its life cycle
    10·1 answer
  • Subscript numbering always starts at what value?
    14·1 answer
  • Management information system by different outhors<br>​
    10·1 answer
  • What are three ways a person may use a computer without realizing it?
    9·1 answer
  • I am having trouble with this python question. I am in a beginner level class also so any solution that isn't too complex would
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!