Continue PHP execution after sending HTTP response

I had this snippet in my “special scripts” toolbox, but it got lost (clouds were not common back then), so I was searching for it and came up with this question, surprised to see that it’s missing, I searched more and came back here to post it: <?php ob_end_clean(); header(“Connection: close”); ignore_user_abort(); // optional ob_start(); … Read more

Differences between fork and exec

The use of fork and exec exemplifies the spirit of UNIX in that it provides a very simple way to start new processes. The fork call basically makes a duplicate of the current process, identical in almost every way. Not everything is copied over (for example, resource limits in some implementations) but the idea is … Read more

How to share memory between processes created by fork()?

You can use shared memory (shm_open(), shm_unlink(), mmap(), etc.). #include <stdio.h> #include <stdlib.h> #include <sys/mman.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> static int *glob_var; int main(void) { glob_var = mmap(NULL, sizeof *glob_var, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); *glob_var = 1; if (fork() == 0) { *glob_var = 5; exit(EXIT_SUCCESS); } else { … Read more

How to use shared memory with Linux in C

There are two approaches: shmget and mmap. I’ll talk about mmap, since it’s more modern and flexible, but you can take a look at man shmget (or this tutorial) if you’d rather use the old-style tools. The mmap() function can be used to allocate memory buffers with highly customizable parameters to control access and permissions, … Read more

printf anomaly after “fork()”

I note that <system.h> is a non-standard header; I replaced it with <unistd.h> and the code compiled cleanly. When the output of your program is going to a terminal (screen), it is line buffered. When the output of your program goes to a pipe, it is fully buffered. You can control the buffering mode by … Read more

Piping and forking in python

The immediate problem is that your child has an infinite loop, reading num1 over and over forever (or, rather, reading it twice and then blocking forever on a third input that will never come) before doing anything. Fix that by moving more of the code into the while loop, like this: def child(pipein): while True: … Read more