Programming Help/Understanding

10 #include <sys/wait.h>
11 #include <sys/types.h>
12 #include <signal.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stddef.h>
17 #include <string.h>
18 #include <errno.h>
19 int main()
20 {
21 int status = 0;
22 int x = 0;
23 int nsecs = 0;
24 int cpid = fork();
25 printf("Start of Fork\n");
26
27 pid_t pid = fork();
28 /* the return value is a process ID, that ALWAYS needs to be tested
29 */
30 switch (pid) {
31
32 case -1:
33 /* error: fork was unsuccessful */
34 fprintf(stderr, "Error: Fork was unsuccessful %d\n", errno);
35 break;
36 case 0:
37 /* this is the child process */
38 /* no process ID */
39 /* ... do something ... */
40
41 if (cpid == 0) { /* CHILD */
42
43 alarm(nsecs); /* set alarm to kill yourself in 0 seconds */
44 pause();
45 }
46
47
48 for (x; x < 12; x++) /*x will count up to 12*/
49 {
50
51 printf("Child(X): %d PID: %d\n", x, getpid());
52
53 }
54
55 break;
56 default:
57 /* this is the parent process */
58
59 // pid = waitpid(pid, &status, 0);
60 sleep(nsecs);
61 wait(&status);
62 sleep(nsecs);
63
64 for (x; x < 12; x++)
65 {
66 printf("Parent(Y): %d PID: %d\n", x, getpid());
67 }
68 /* pid=process ID of the child */
69 /* ... */ }
71 /* both processes continue here */
72 printf("End of Program\n");
73
74 return 0;

So the question that eludes me is this:
What status is being printed when the child terminates before the parent, but the parentdoes not wait (use: ps ajx | grep fork) ?

I know the answer is <defunt> but I need it to appear in my unix system. Where is the error in my program and can you explain and show me what I am missing or my errors? Thank you
Last edited on
Topic archived. No new replies allowed.