The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 2 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Programming problem 3.21(1st part of the questions is the 3.21 png)
|
#include <sys/types.h> #include <stdio.h> #include <unistd.h> int main() { pid_t pid, pid1; Â Â Â /* fork a child process */ Â Â Â pid = fork(); Â Â Â if (pid < 0) { /* error occurred */ Â Â Â Â Â Â Â fprintf(stderr, "Fork Failed"); return 1; Â Â Â Â }else if (pid == 0) { /* child process */ pid1 = getpid(); Â Â Â Â Â Â Â printf("child: pid = %dn",pid); /* A */ Â Â Â Â Â Â Â printf("child: pid1 = %dnn",pid1); /* B */ Â Â Â }else { /* parent process */ Â Â Â Â Â Â Â pid1 = getpid(); Â Â Â Â Â Â Â printf("parent: pid = %dn",pid); Â Â Â Â Â Â Â /* C */ printf("parent: pid1 = %d n",pid1); /* D */ Â Â Â Â Â Â Â wait(NULL); Â Â Â } Â Â Â return 0; } |
1. Besides requirements in the Problem, your program should also display the process IDs of both the parent and child processes. Follow the code in Figure 3.34 to structure your program and to print the process IDs. Capture a screenshot after running your program. A sample screenshot is provided below (the output of your program should have the same structure as in the sample screenshot).
2.A skeleton of the program is provided, which has code to obtain user’s input number from commandline. Add your own code on top of the existing code
|
#include int main(int argc, char *argv[]) Â Â if (argc == 1) { Â Â Â Â Â return -1; Â Â n = atoi(argv[1]); Â Â /* add your code below, following the code structure of Figure 3.34 (page 152) */ Â Â return 0; Â |
3.You can use the gcc command to compile the program. See the sample screenshot below. (Example 1 and 2)
Attachments:
-----------