-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyspawn.c
41 lines (32 loc) · 1015 Bytes
/
myspawn.c
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
#include <stdio.h>
#include <stdlib.h>
#include <spawn.h>
#include <sys/wait.h>
#include <unistd.h>
extern char **environ;
int main() {
pid_t pid;
char *argv[] = {"echo", "Hello from the spawned process!", NULL};
int status;
posix_spawnattr_t attr;
// Initialize spawn attributes
posix_spawnattr_init(&attr);
// Set flags if needed, for example, to specify the scheduling policy
// posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSCHEDULER);
// Spawn a new process
if (posix_spawnp(&pid, "echo", NULL, &attr, argv, environ) != 0) {
perror("spawn failed");
exit(EXIT_FAILURE);
}
// Wait for the spawned process to terminate
if (waitpid(pid, &status, 0) == -1) {
perror("waitpid failed");
exit(EXIT_FAILURE);
}
if (WIFEXITED(status)) {
printf("Spawned process exited with status %d\n", WEXITSTATUS(status));
}
// Destroy spawn attributes
posix_spawnattr_destroy(&attr);
return EXIT_SUCCESS;
}