Why does the child runtime come up as negative?

I did a little test to see how much longer the child process execution takes with fork() compared to the parent. However, running this program, I get the execution for child to somehow come up as negative. #include <stdio.h> #include <unis...
more »

2017-05-06 13:05 (2) Answers

printf newline has line buffer?

Language: C, OS: Linux Code: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main(void) { fork(); printf("hello world\n"); fork(); printf("bye\n"); return 0; } Output: hello world bye hell...
more »

2017-03-27 19:03 (1) Answers

Ending child process with return 0

Is return 0 in case 0 the correct choice? In other words, does return 0 call the _exit() function? Or maybe it calls exit(), hence this code is unsafe? #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys...
more »

2017-03-20 00:03 (1) Answers

Why does this program with fork print twice?

According to my understanding of fork(), this should print "Hello, World!" once, since there is only one process before fork is called. However, it prints twice. Curiously, if I add a \n to the string or use puts() instead of printf(), it only prints...
more »

2017-03-09 09:03 (4) Answers

Unable to read from stdin in the child after fork?

A regular C program, an implementation of some shell program, which uses standard input to get commands and executes them, in which main() is declared as: int main(int argc, char *argv[]), works normally. It's a small shell program that prints a pro...
more »

2017-03-04 12:03 (0) Answers

3 forks and 6 Pipes

I'm writing my frist fork-pipe code in order to learn how it works. The point is that I want to create 3 childs that will comunicate with his dad and each child will perform diferent tasks. This is the code i'm working on: #include <stdio.h>...
more »

2017-03-04 01:03 (2) Answers

Weird behaviour using fork();

I have this tiny program in C main(int argc, char **argv) { forkit(4); } void forkit(int n) { if(n > 0) { fork(); printf("%d \n", n); forkit(n-1); } } which prints 4 4 3 3 3 3 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1...
more »

2017-03-03 01:03 (1) Answers

How to test if calling process is a fork

Is there a method or C api in Linux/POSIX to determine if the current process is a fork? I.e. if it has been created by calling fork() without a subsequent call to the execve() family? I do not control the code leading up to this point (it runs with...
more »

2017-02-11 15:02 (4) Answers

Async signal safety of fork()

According to Oracle's Multithreaded Programming Guide, fork() should be safe-to-use inside signal handlers. But my process got stuck inside signal handler with to following back trace: #0 __lll_lock_wait_private () at ../nptl/sysdeps/unix/sysv/...
more »

2016-12-29 13:12 (0) Answers

How fork() and scanf() work together?

I tried to see what happens if I read something from keyboard while I have multiple processes with fork() (in my case there are two children and a parent) and I discovered the following problem: I need to tell the parent to wait for children's proce...
more »

2016-12-27 18:12 (2) Answers

How to fork a process of another module

TL;DR : How does one fork a process that is located outside of the current running process? I'm trying to use child_process of Nodejs in order to start another nodejs process on the parent's process exit. I successfully executed the process with th...
more »

2016-12-18 16:12 (1) Answers

Fork : concurrency or parallelism

I was recently doing some experiments with the fork function and I was concerned by a "simple" (short) question: Does fork uses concurrency or parallelism (if more than one core) mechanisms? Or, is it the OS which makes the best choice? Thanks f...
more »

2016-10-28 12:10 (4) Answers

Forcing the parent process to run first

On my system(Opensuse) by default the child process always executes first, after fork. There are also ways to force the child process to run first. I would like to know if there is any way to force the parent process to run first? ...
more »

2016-10-23 08:10 (1) Answers

Fork() Child Chain

I have the following code, which creates the defined number of child threads using fork(): #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h&g...
more »

2016-10-19 17:10 (3) Answers


I'm playing around with signals: SIGSTOP and SIGCONT in particular. Here is a test program I wrote. The idea is to create a chain of N + 1 processes (including the main process). Each one has to wait for its child to stop, then stop itself. The main ...
more »

2016-10-13 21:10 (1) Answers

Time waste of execv() and fork()

I am currently learning about fork() and execv() and I had a question regarding the efficiency of the combination. I was shown the following standard code: pid = fork(); if(pid < 0){ //handle fork error } else if (pid == 0){ execv("son_p...
more »

2016-10-05 17:10 (5) Answers

child process prints wrong ppid()

In this program, why is child process printing wrong ppid()? #include<stdio.h> #include<stdlib.h> #include<unistd.h> void childprocess() { printf("Child: Hi I am the child process\n"); printf("Child: My process id is %d\n", ge...
more »

2016-10-02 11:10 (2) Answers

Forked processes order of execution

I know there's another thread with the same name, but this is actually a different question. When a process forks multiple times, does the parent finish executing before the children? Vice versa? Concurrently? Here's an example. Lets say I have a f...
more »

2016-09-28 21:09 (3) Answers