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
Under which reflection(s) is the image of line m also a line?
lubasha [3.4K]

Answer:

c

Explanation:

5 0
3 years ago
Read 2 more answers
Which of the following correctly describes the reason for quality customer service?
Vera_Pavlovna [14]
The correct answer is C. Keep existing customers and clients
Explanation:
Customer service refers to the interaction with customers and support employees in a company provide to customer to guarantee he or she has the best experience when buying one product or service. A high-quality customer service implies providing complete information to customers, and helping them before, during, and even after their process in buying or looking for information in a company. The main purpose of this is that customers feel the support from employees, and are loyal to the company which means they will go back if they need the same service or products. Thus, the reason for quality customer service is "Keep existing customers and clients".
7 0
3 years ago
What term is used in an os gui to describe a specialized file that contains information about the locations of other files?
Ugo [173]
The term "Folder" is used in an OS GUI to describe a specialized file that contains information about the locations of other files. OS stands for Operating System and GUI stands for Graphic User Interface, the interface that allows users to interact with computers. Folder is <span>an icon on a computer screen. By clicking it the user access a directory containing related files or document.</span>
5 0
3 years ago
Thelma is a web developer for a bowling league. She wants visitors to the website to be able to print web pages, such as league
prohojiy [21]

Answer:

"@page" is the correct answer for the above question.

Explanation:

  • "@page" is a syntax of CSS which will affect that page which is converted from the hard copy from the soft copy. It means it affects the page which will render the output.
  • So If the Thelma wants to set the margins on the output page then he can do this by the help of the "@page" tag which is defined in the CSS, and then the margin is set to this tag by the help of margin statement.
8 0
3 years ago
Which of the following is the correct procedure for adding a design theme to a PowerPoint presentation ?
Eva8 [605]

Answer:

B. Design > Theme > choose a theme

Explanation:

-Applying themes-

  • Select the Design tab on the Ribbon, then locate the Themes group. Each image represents a theme.
  • Click the More drop-down arrow to see all available themes.
  • Select the desired theme.
  • The theme will be applied to the entire presentation. To apply a different theme, simply select it from the Design tab.
6 0
2 years ago
Other questions:
  • ____ deals with ensuring that data is protected against unauthorized access, and if the data are accessed by an authorized user,
    10·1 answer
  • Changing the position of the wing will
    9·1 answer
  • What are the importance of switches in our electron device
    15·1 answer
  • Why did artists use pinhole cameras during the renaissance?
    9·1 answer
  • Assume that sentence is a variable that has been associated with a string consisting of words separated by single space characte
    5·1 answer
  • private members of a class are accessible only from _____________ of the same class or from their friends
    12·1 answer
  • Write a string class. To avoid conflicts with other similarly named classes, we will call our version MyString. This object is d
    5·1 answer
  • Cite los botones de edición que existen y determine en que ficha y grupo se encuentran.
    14·1 answer
  • 10 sentences about computer parts.
    5·1 answer
  • What is a computer network?
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!