-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec1.c
More file actions
57 lines (47 loc) · 1.6 KB
/
exec1.c
File metadata and controls
57 lines (47 loc) · 1.6 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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int main( int argc, char *argv[], char *env[] )
{
pid_t my_pid, parent_pid, child_pid;
int status;
/* get and print my pid and my parent's pid. */
my_pid = getpid(); parent_pid = getppid();
printf("\n Parent: my pid is %d\n\n", my_pid);
printf("Parent: my parent's pid is %d\n\n", parent_pid);
/* print error message if fork() fails */
if((child_pid = fork()) < 0 )
{
perror("fork failure");
exit(1);
}
/* fork() == 0 for child process */
if(child_pid == 0)
{ printf("\nChild: I am a new-born process!\n\n");
my_pid = getpid(); parent_pid = getppid();
printf("Child: my pid is: %d\n\n", my_pid);
printf("Child: my parent's pid is: %d\n\n", parent_pid);
printf("Child: I will sleep 3 seconds and then execute - date - command \n\n");
sleep(3);
printf("Child: Now, I woke up and am executing date command \n\n");
execl("/bin/date", "date", 0, 0);
perror("execl() failure!\n\n");
printf("This print is after execl() and should not have been executed if execl were successful! \n\n");
_exit(1);
}
/*
* parent process
*/
else
{
printf("\nParent: I created a child process.\n\n");
printf("Parent: my child's pid is: %d\n\n", child_pid);
system("ps -acefl | grep ercal"); printf("\n \n");
wait(&status); /* can use wait(NULL) since exit status
from child is not used. */
printf("\n Parent: my child is dead. I am going to leave.\n \n ");
}
return 0;
}