KaanB
KaanB

Reputation: 133

unix fork command can return only one child?

Im sorry for such a noobie question but Im not familiar with Unix(system calls). Im trying to understand the fork command and I know fork command returns pid 0 if its child process or returns pid for child id. I want to know a process can have only one child at a time and returns its pid or can have multiple child processes?( actually I cant imagine if it has more than one child, how to return pid and which pid?)

Upvotes: 0

Views: 1993

Answers (3)

Fork is not a Unix command, but a syscall documented in the fork(2) Posix man page. The Linux man page is fork(2) -linux- here.

On success, the fork syscall returns twice: once in the (calling) parent process (where it gives the pid of the child process), and once in the newly created child process (where it gives 0). Otherwise, the two process are clones (and some Linux implementations have a fork implemented above the Linux specific clone(2) syscall) and are executing the same program with nearly the same state (actually two nearly identical copies). The two (parent and child) processes are running simultaneously (as tasks scheduled by the kernel, perhaps in parallel on different processor cores).

The fork(2) syscall might (rarely) fail (e.g. to avoid fork bombs), e.g. when the RLIMIT_NPROC limit set with setrlimit(2) syscall has been reached. When it does fail, the fork syscall return -1 and you should check errno. It is good practice to check the failure of every syscall, and fork is not an exception.

Fork is the usual way to start processes, all [but some rare exceptions] the user processes are started by some fork syscall (or a variant like vfork(2), or clone(2)). In particular, when you type a command (e.g. ls or date) in a shell (e.g. /bin/bash perhaps running in a terminal emulator), the shell is very often fork-ing itself, then execve-ing the required program (exceptions are shell builtins like cd).

Notice that nearly all processes on a Linux system have been created by fork. The exceptions are kernel-started processes like /sbin/init or /sbin/modprobe ...

You can call fork many times (a typical shell implementation does that for every command, except the builtin ones like cd; you might want to study the source code of free software shells like sash or bash). You should eventually wait for the child processes using the wait(2) or waitpid(2) syscall. A process can change its executable file and address space with the execve(2) syscall. Its address space can change using mmap(2) and related syscalls.

You might want to use strace -f to understand what syscalls are made, e.g. by some shell.

I strongly recommend reading the Advanced Unix Programming book and the Advanced Linux programming book (the later being available on line with some free license).

Upvotes: 4

Jens
Jens

Reputation: 72657

Same answer as others with a twist: If each process could create only one child (use fork() only once), what would that mean for the lineage of processes? It would mean that there is exactly one line, say, init -> login -> shell -> ls. Would you like such a system? What is your conclusion?

Upvotes: 2

Joachim Isaksson
Joachim Isaksson

Reputation: 180927

Each call to fork() creates a single child process and only returns that single process' PID, but there is nothing preventing you from calling fork() multiple times to create multiple child processes.

Don't forget to wait() or waitpid() for your forked child processes to exit, or you may get zombie processes hanging around.

Upvotes: 4

Related Questions