Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1 |
both |
#define _POSIX_SOURCE
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
For more information about these elements, see times() — Get process and child process times.
In all other respects, the child is identical to the parent. Because the child is a duplicate, it contains the same call to fork() that was in the parent. Execution begins with this fork() call, which returns a value of 0; the child then proceeds with normal execution.
If the parent process is multithreaded, it is the responsibility of the application to ensure that the application data is in a consistent state when the fork() occurs. For example, mutexes that are used to serialize updates to application data may need to be locked before the fork() and unlocked afterwards.
For more information on fork(), refer to z/OS UNIX System Services Programming: Assembler Callable Services Reference.
You can use MVS™ memory files from a z/OS® UNIX program. However, use of the fork() function from the program removes access from a hiperspace memory file for the child process. Use of an exec function from the program clears a memory file when the process address space is cleared.
The child process that results from a fork() in a multithreaded environment can only invoke async-signal-safe functions.
An async-signal-safe function is defined as a function that may be invoked, without restriction, from signal-catching functions. All supported async-signal-safe functions are listed in Table 1.
abort() | fpathconf() | raise() | sigpending() |
accept() | fstat() | read() | sigprocmask() |
access() | fsync() | readlink() | sigqueue() |
aio_error() | ftruncate() | recv() | sigset() |
aio_return() | getegid() | recvfrom() | sigsuspend() |
aio_suspend() | geteuid() | recvmsg() | socket() |
alarm() | getgid() | rename() | socketpair() |
bind() | getgroups() | rmdir() | stat() |
cfgetispeed() | getpeername() | select() | symlink() |
cfgetospeed() | getpgrp() | send() | sysconf() |
cfsetispeed() | getpid() | sendmsg() | tcdrain() |
cfsetospeed() | getppid() | sendto() | tcflow() |
chdir() | getsockname() | setgid() | tcflush() |
chmod() | getsockopt() | setpgid() | tcgetattr() |
chown() | getuid() | setsid() | tcgetpgrp() |
close() | kill() | setsockopt() | tcsendbreak() |
connect() | link() | setuid() | tcsetattr() |
creat() | listen() | shutdown() | tcsetpgrp() |
dup() | lseek() | sigaction() | time() |
dup2() | lstat() | sigaddset() | times() |
execle() | mkdir() | sigdelset() | umask() |
execve() | mkfifo() | sigemptyset() | uname() |
_Exit() | open() | sigfillset() | unlink() |
_exit() | pathconf() | sigismember() | utime() |
fchmod() | pause() | sleep() | wait() |
fchown() | pipe() | signal() | waitpid() |
fcntl() | poll() | sigpause() | write() |
fork() |
Interoperability restriction: For POSIX resources, fork() behaves as just described. But in general, MVS resources that existed in the parent do not exist in the child. This is true for open streams in MVS data sets and assembler-accessed MVS facilities, such as STIMERS. In addition, MVS allocations (through JCL, SVC99, or ALLOCATE) are not passed to the child process.
If successful, fork() returns 0 to the child process and the process ID of the newly created child to the parent process.
⁄* CELEBF27
This example creates a new child process.
*⁄
#define _POSIX_SOURCE
#include <sys⁄types.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys⁄wait.h>
main() {
pid_t pid;
int status;
if ((pid = fork()) < 0)
perror("fork() error");
else if (pid == 0) {
puts("This is the child.");
printf("Child's pid is %d and my parent's is %d\n",
(int) getpid(), (int) getppid());
exit(42);
}
else {
puts("This is the parent.");
printf("Parent's pid is %d and my child's is %d\n",
(int) getpid(), (int) pid);
puts("I'm waiting for my child to complete.");
if (wait(&status) == -1)
perror("wait() error");
else if (WIFEXITED(status))
printf("The child exited with status of %d\n",
WEXITSTATUS(status));
else
puts("The child did not exit successfully");
}
}
This is the parent.
This is the child.
Child's pid is 1114120 and my parent's is 2293766
Parent's pid is 2293766 and my child's is 1114120
I'm waiting for my child to complete.
The child exited with status of 42