-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwait2.c
More file actions
67 lines (61 loc) · 1.35 KB
/
Copy pathwait2.c
File metadata and controls
67 lines (61 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#define DELAY 3
/**
* main - function that shows how parent pauses until child finishes
*
* Return: always 0
*/
int main(void)
{
int newpid;
void childfxn(), parentfxn();
printf("Before fork(), my code is %d\n", getpid());
newpid = fork();
if (newpid == -1)
{
perror("Error: ");
return (1);
}
else if (newpid == 0)
{
childfxn(DELAY);
}
else
{
parentfxn(newpid);
}
/* since their return_type is void, they wont return anything.
* chidfxn and parentfxn are user defined functions that already been declared at the beginning of this code
* The next is to define each Function. that is, providing the fxn body*/
return (0);
}
void childfxn(int delay)
/**
* chidfxn - the new process takes a nap and exit
* @delay: number of seconds
*
* Return: returns void
*/
{
printf("Hey! Child process(%d) is here, will sleep for %d seconds\n", getpid(), delay);
sleep(delay);
printf("I am done, about to exit\n");
exit(EXIT_SUCCESS);
}
void parentfxn(int newpid)
/**
* parentfxn - parent wait for child and then prints a message
* @newpid: the pid of the child process
*
* Return: returns void
*/
{
int wait_rv;
/* return value of wait */
wait_rv = wait(NULL);
printf("Parent process resumed.\n Done waiting for %d. Wait returned: %d\n", newpid, wait_rv);
}