The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Hot answers tagged

93

When outputting to standard output using the C library's printf() function, the output is usually buffered. The buffer is not flushed until you output a newline, call fflush(stdout) or exit the program (not through calling _exit() though). The standard output stream is by default line-buffered in this way when it's connected to a TTY. When you fork the ...


86

You probably have a Linux distro that uses systemd. Systemd creates a cgroup for each user, and all processes of a user belong to the same cgroup. Cgroups is a Linux mechanism to set limits on system resources like max number of processes, CPU cycles, RAM usage, etc. This is a different, more modern, layer of resource limiting than ulimit (which uses the ...


68

When a process exits, all its children also die (unless you use NOHUP in which case they get back to init). This is wrong. Dead wrong. The person saying that was either mistaken, or confused a particular situation with the the general case. There are two ways in which the death of a process can indirectly cause the death of its children. They are related ...


61

The short answer is, fork is in Unix because it was easy to fit into the existing system at the time, and because a predecessor system at Berkeley had used the concept of forks. From The Evolution of the Unix Time-sharing System (relevant text has been highlighted): Process control in its modern form was designed and implemented within a couple of days. ...


57

It's to simplify the interface. The alternative to fork and exec would be something like Windows' CreateProcess function. Notice how many parameters CreateProcess has, and many of them are structs with even more parameters. This is because everything you might want to control about the new process has to be passed to CreateProcess. In fact, CreateProcess ...


51

fork() was the original UNIX system call. It can only be used to create new processes, not threads. Also, it is portable. In Linux, clone() is a new, versatile system call which can be used to create a new thread of execution. Depending on the options passed, the new thread of execution can adhere to the semantics of a UNIX process, a POSIX thread, ...


46

The problem is caused by the TasksMax systemd attribute. It was introduced in systemd 228 and makes use of the cgroups pid subsystem, which was introduced in the linux kernel 4.3. A task limit of 512 is thus enabled in systemd if kernel 4.3 or newer is running. The feature is announced here and was introduced in this pull request and the default values were ...


34

[I'll repeat part of my answer from here.] Why not just have a command that creates a new process from scratch? Isn't it absurd and inefficient to copy one that is only going to be replaced right away? In fact, that would probably not be as efficient for a few reasons: The "copy" produced by fork() is a bit of an abstraction, since the kernel uses a copy-...


33

For a daemon, what you want is a process that has no tie to anything. At the very least, you want it to be in its own session, not be attached to a terminal, not have any file descriptor inherited from the parent open to anything, not have a parent caring for you (other than init) have the current directory in / so as not to prevent a umount... To detach ...


32

First of all, every time you execute a command, you shell will fork a new process, regardless of whether you run it with & or not. & only means you're running it in the background. Note this is not very accurate. Some commands, like cd are shell functions and will usually not fork a new process. type cmd will usually tell you whether cmd is an ...


31

The traditional way of daemonizing is: fork() setsid() close(0) /* and /dev/null as fd 0, 1 and 2 */ close(1) close(2) fork() This ensures that the process is no longer in the same process group as the terminal and thus won't be killed together with it. The IO redirection is to make output not appear on the terminal.


26

Process arguments are visible to all users, but the environment is only visible to the same user (at least on Linux, and I think on every modern unix variant). So passing a password through an environment variable is safe. If someone can read your environment variables, they can execute processes as you, so it's game over already. The contents of the ...


26

Piping doesn't require that the first instance finishes before the other one starts. Actually, all it is really doing is redirecting the stdout of the first instance to the stdin of the second one, so they can be running simultaneously (as they have to for the fork bomb to work). Well, What exactly is the output of : ? what is being passed to the other : ?...


24

The Linux kernel does implement Copy-on-Write when fork() is called. When the syscall is executed, the pages that the parent and child share are marked read-only. If a write is performed on the read-only page, it is then copied, as the memory is no longer identical between the two processes. Therefore, if only read-operations are being performed, the pages ...


23

This fork bomb always reminds me of the something an AI programming teacher said on one of the first lessons I attended "To understand recursion, first you must understand recursion". At it's core, this bomb is a recursive function. In essence, you create a function, which calls itself, which calls itself, which calls itself.... until system resources are ...


22

So when a command is fired from a shell, fork() inherits a child process of it and exec() loads the child process to the memory and executes. Not quite. fork() clones the current process, creating an identical child. exec() loads a new program into the current process, replacing the existing one. My qs is: If the child process contains all the ...


21

When a child is forked then it inherits parent's file descriptors, if child closes the file descriptor what will happen ? It inherits a copy of the file descriptor. So closing the descriptor in the child will close it for the child, but not the parent, and vice versa. If child starts writing what shall happen to the file at the parent's end ? Who ...


21

Sure! A common pattern in "wrapper" programs is to do various things and then replace itself with some other program with only an exec call (no fork) #!/bin/sh export BLAH_API_KEY=blub ... exec /the/thus/wrapped/program "$@" A real-life example of this is GIT_SSH (though git(1) does also offer GIT_SSH_COMMAND if you do not want to do the above wrapper ...


19

The entirety of fork() is implemented using mmap / copy on write. This not only affects the heap, but also shared libraries, stack, BSS areas. Which, incidentally, means that fork is a extremely lightweight operation, until the resulting 2 processes (parent and child) actually start writing to memory ranges. This feature is a major contributor to the ...


19

This could be due to some resource limit, either on the server itself (or) specific to your user account. Limits in your shell could be checked via ulimit -a. Esp check for ulimit -u max user processes, if you have reached max processes, fork is unable to create any new and failing with that error. This could also be due to swap/memory resource issue


18

Nothing particular happens. All processes are sharing the same set of pages and each one gets its own private copy when it wants to modify a page.


18

The answer is more or less that ls is an external executable. You can see its location by running type -p ls. Why isn't ls built into the shell, then? Well, why should it be? The job of a shell is not to encompass every available command, but to provide an environment capable of running them. Some modern shells have echo, printf, and their ilk as builtins, ...


18

Why don't we try it out and see? Here's a trivial program using signal(3) to trap SIGINT in both the parent and child process and print out a message identifying the process when it arrives. #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> void parent_trap(int sig) {fprintf(stderr, "They got back ...


17

You should try setsid(1). Use it like you'd use nohup: setsid command_which_takes_time input > output This (as per the setsid(2) manpage), does a fork(2), an _exit(2) of the parent process, then the child process calls setsid(2) to create a new process group (session). You can't kill that by logging out, and it's not part of the Bash job control ...


17

It does not affect the fork in any way. In the first case, you end up with 8 processes with nothing to write, because the output buffer was emptied already (due to the \n). In the second case you still have 8 processes, each one with a buffer containing "Hello world..." and the buffer is written at process end.


16

Is it ever useful to do nohup ... &? Yes. If you just start a process "in the background" with &, that new process still has membership in the original shell's "process group". If that shell or the process group gets certain signals (SIGHUP, for example), by default they exit. This means that if you run a process with & from a shell started by ...


16

It's not that difficult to decipher in fact. This piece of code just defines a function named : which calls two instances of itself in a pipeline: :|:&. After the definition an instance of this function is started. This leads to a fast increasing number of subshell processes. Unprotected systems (systems without a process number limit per user) will be ...


15

The Bash Reference Manual states: Builtin commands are necessary to implement functionality impossible or inconvenient to obtain with separate utilities. That is, shells are designed to only include built-in commands if: Required by the POSIX standard Commands that require access to the shell itself, such as job control built-ins Commands that are very ...


14

It is not something new. It dates way back to 1970's when it got introduced. Quoting from here, One of the earliest accounts of a fork bomb was at the University of Washington on a Burroughs 5500 in 1969. It is described as a "hack" named RABBITS that would make two copies of itself when it was run, and these two would generate two more copies ...


Only top voted, non community-wiki answers of a minimum length are eligible