Question

Write a “C” program that creates a child process that prints out its pid and new...

Write a “C” program that creates a child process that prints out its pid and new line and then calls pause() system call. Once it returns from the pause system call the child program must exit with code 5. The parent program must wait for the child process and print out the pid of the child process and the exit status with the following format: “childpid=%d,exitstatus=%d\n”.

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

Note: This program generates exit code randomly.It is not possible to set exit code 5 because exit code 5 is I/O error status code. since code runs successfully exit code is randomly generated by the process and greater than 127. I have tried my best to get it done.

C code:

#include<sys/wait.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>


int main()
{
   int s;
pid_t cpid;

   if (fork() == 0)
   {
   cpid = getpid();
   printf("child process pid is %d\n",cpid);
   int pause(void);
   exit(10);
}
  
else
{
waitpid(cpid,&s,0);
cpid=wait(NULL);
       printf("Exit status: %d\n",WEXITSTATUS(s));
       printf("Printing child process pid in parent process is %d\n",cpid);
}
   return 0;
}

Execution screenshot:

Shortcuts S Reset Copy 1 #include<sys/wait.h> 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<unistd.h> 7 8- int main() iTime(sec): 0 Memory(MB): 1.4360018423462 Output: Copy child process pid is 30433 Exit status: 1 Printing child process pid in

Add a comment
Know the answer?
Add Answer to:
Write a “C” program that creates a child process that prints out its pid and new...
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
  • a) Write a C-program that creates a chain of 10 processes and prints out their process...

    a) Write a C-program that creates a chain of 10 processes and prints out their process ids and relationships. For example, process 1 is the parent of process 2, process 2 is the parent of process 3, process 3 is the parent of 4 and so on. Each child has to print out all her ancestors identified by the process ids. b) Write a C-program that creates a fan of 10 processes. That is, process 1 is the parent of...

  • 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()...

  • 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...

  • Part 2. System Programming and Process Management You are asked to develop basic system programmi...

    Can someone help me create this program for Linux. Part 2. System Programming and Process Management You are asked to develop basic system programming including process creation and termination on a Linux platform in this part of programming project. You are asked to create multiple children processes to work under one parent process. In theory, children processes can do their own work separately or cooperatively to accomplish a task. In this assignment, these children processes simply print out a "hello"...

  • Write code that forks into two processes: a parent process, and a child process. Same as...

    Write code that forks into two processes: a parent process, and a child process. Same as the Regular version, except that your code must also be able to handle negative integers input from the command-line. If I call your code this way:     a03 -3 5 -7 The parent process should print out: Assignment 3     sum = -5 The sums produced from the test input I use will be in the range [-128 .. 127] -------------------------------------------------------------------------------------------------------------------- // Numbers from...

  • Write a C program to create a parent and child process. Display the pid of the parent process. In the child process, display the pid and compute the Fibonacci series for 10 numbers

    Write a C program to create a parent and child process. Display the pid of the parent process. In the child process, display the pid and compute the Fibonacci series for 10 numbers

  • Write code that forks into two processes: a parent process, and a child process. Your code...

    Write code that forks into two processes: a parent process, and a child process. Your code will be called with command-line arguments consisting of negative integers. Do not worry about bad command-line arguments such as "xyz". Your code will not be tested in this way. The parent process will take the arguments to main(), convert them into ints by calling atoi(), and send those ints one at a time to the child process through a pipe (one call to write()...

  • You are required to write a C program on Unix/Linux in which the parent process creates...

    You are required to write a C program on Unix/Linux in which the parent process creates three child processes, lets them run concurrently, and waits for them to return and prints their exit status. The three child processes are assigned different tasks. Child one is to calculate and display the highest mark of a class of ten students for a unit. Child one is required to get the marks from the standard input (i.e. the keyboard). Child two is to...

  • Write a program in C using the fork() system call to do the following. The parent process (main program) forks a process...

    Write a program in C using the fork() system call to do the following. The parent process (main program) forks a process (CHILD 1) to compute and print the sum of first n integers where n is a variable shared between the parent and CHILD 1. It also forks another process (CHILD 2) that finds the sum of squares of the first n numbers where n is a variable it shares with the parent. Let CHILD 1 print “The sum...

  • 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...

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