Question

I wrote the following code to create a child to do something and return. The return...

I wrote the following code to create a child to do something and return.

The return value is to be caught by the parent.

Can you see any problem with the code that I wrote?

How will you fix it?

pid_t pid = fork();

if ( pid < 0 ) exit ( 1 );

if ( pid == 0 ) wait();

exit ( 0 );

(6 points, Operating System question)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Your code require a slight modifications to know whether the required operation is done or not.

The changes are done in wait() function.

Here the wait() suspends the parent that is calling proces until the child returns something.

To know the status returned by child and make the parent process to display the status returned by child, so that the requirement of parent process receiving the child process status.

Add :

printf(" status of child received by parent process is: %d", WIFEXITED(status));

To display the status of child by parent process as per requirements.

____________________

Kindly comment for queries if any, upvote if you like it.

Thankyou.

Add a comment
Know the answer?
Add Answer to:
I wrote the following code to create a child to do something and return. The return...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork...

    Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork a child process*/ pid = fork(); if (pid<0){ /*error occured*/ fprintf(stderr,"Fork failed\n"); return 1; } else if (pid == 0) { /*child process */ printf("I am the child %d\n",pid); execlp("/bin/ls","ls",NULL); } else { /*parent process */ /*parent will wait for the child to complete*/ printf("I am the parent %d\n",pid); wait(NULL); printf("Child Complete\n"); } return 0; } 1. Write program codes for 3.21 a and...

  • The following code is meant to capture the output of an executable in a file and...

    The following code is meant to capture the output of an executable in a file and then read in and print out the first 100 bytes of that file. Select all of the bugs in the code. Assume that all system calls succeed and that sizeof(char)-1. pid_t pid = fork(); if (pid == 0) { int in-open("file.txt", READONLY) dup2(STDOUT FILENO, in); execl("./something", "something", "hello!"); l else char buffer[100] int in = open("file.txt'. READ-ONLY): read(buffer, in, 100); The parent does not...

  • Linux & C code help with creating a child process and kills itself with SIGNAL. I...

    Linux & C code help with creating a child process and kills itself with SIGNAL. I provided most of the code, I need some help to finish. Please provide output screen shot as well. Thank you! A Process creates a child process. Child process will print out "hello 0" "hello 1" ..... 1 line per second. After a certain number of seconds(user input), it kills itself WITH signal mechanism ONLY, don't use exit() on the child. You can use alarm()...

  • Complete the following C code by filling all “???”sto write a C program to create a...

    Complete the following C code by filling all “???”sto write a C program to create a child process. Before the child process is created, an integer variable A is defined and assigned value 10. Then in the child process, A is reduced by 5. After the child process completes, the parent process increases A by 5. Then what will A’s value be just before the parent process completes? Why? Write the code and run it to verify your answer. #include...

  • System Programming in C

    Explain what the problem is within the program. Fix the problem when you create a child process per column. The code is given below. So basically, after the child processes have successfully excuted their code, the final print statement does not give the correct values. It prints the original matrix rather than the multiplied matrix.#include  #include  #include  #include  int main(int argc, char *argv[]){ int row = 0; int column = 0; row = atoi(argv[1]); column = atoi(argv[2]); int *A = ...

  • I have the following code in C: void sighandler(int sig) {      printf("exiting child..."); } int...

    I have the following code in C: void sighandler(int sig) {      printf("exiting child..."); } int main(int argc,char* argv[]) {      if(argc > 1)      {            char* commandName;            for(int i=2;i            {                   forkChild = fork();                   signal(SIGINT,sighandler);                   if(forkChild == 0)                   {                          execlp(commandName,commandName,argv[i],NULL);                          exit(0);                   }                   else                   {                          wait(NULL);                   }       } My problem is that I would like to kill the child with ^C but leave the parent running....

  • Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this...

    Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this problem. You can still read/write from/to a file. You must use ( kill() and pause() ) system calls. rewrite code below to do kill and pause system calls #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() { int pid; // pid: used to keep track of the child process int x...

  • I have the following code....from the previous lab....the above needs to be added to what is...

    I have the following code....from the previous lab....the above needs to be added to what is already existing. ALSO MODIFY SEMAPHORES TO USE pthreads instead of the pipe constructs P() & V() #include <stdio.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #include <stdlib.h> #include <sys/stat.h> void printStat(char *filename); //Main int main(int argc, char *argv[]) { //Process Id (storing)    pid_t pid;    int j;    //printf("Welcome to Project Three\n”);    // For loop*/    for (j = 1; j...

  • plz help me

    #include <sys/types.h> #include <stdio.h> #include <unistd.h> int value = 10; int main() { pid_t pid;  pid = fork();  if (pid == 0) {  value = value + 100;  } else if (pid > 0) { value = value -100;  printf("PARENT: value= %d \n", value); //Line A  wait (NULL); } }  (i) What will be the output in Line A? Justify your answer. (ii) Do you think there is synchronization problem in updating the variable value? Justify your answer.

  • C program To develop a C program to implement a process system call. PROBLEM You are...

    C program To develop a C program to implement a process system call. PROBLEM You are to use the Ubuntu operating system to write a C program that creates a process to determine the identification of the current user of your computer. I mean if joe is the login of the current user of your computer your solution should return joe. Your solution must also provide the pid of both the parent and the child processes. You may use the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT