And how can one find out whether any of them are occuring, and leading to an error returned by fork() or system()? In other words, if fork() or system() returns with an err
nproc in /etc/security/limits.conf can limit the number of processes per user.
You can check for failure by examining the return from fork. A 0 means you are in the child, a positive number is the pid of the child and means you are in the parent, and a negative number means the fork failed. When fork fails it sets the external variable errno. You can use the functions in errno.h to examine it. I normally just use perror to print the error (with some text prepended to it) to stderr.
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
int main(int argc, char** argv) {
pid_t pid;
pid = fork();
if (pid == -1) {
perror("Could not fork: ");
return 1;
} else if (pid == 0) {
printf("in child\n");
return 0;
};
printf("in parent, child is %d\n", pid);
return 0;
}
And how can one find out whether any of them are occuring?
Check the errno value if the result (return value) is -1
From the man page on Linux:
RETURN VALUE
On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.ERRORS
EAGAIN
fork() cannot allocate sufficient memory to copy the parent's page tables and allocate a task structure for the child.
EAGAIN
It was not possible to create a new process because the caller's RLIMIT_NPROC resource limit was encountered. To exceed this limit, the process must have either the CAP_SYS_ADMIN or the CAP_SYS_RESOURCE capability.
ENOMEM
fork() failed to allocate the necessary kernel structures because memory is tight.CONFORMING TO SVr4, 4.3BSD, POSIX.1-2001.