fork

几个例子,看懂poll和epoll

筅森魡賤 提交于 2019-12-07 15:39:59
1. poll的例子 #include <stdio.h> #include <sys/eventfd.h> #include <poll.h> int main() { uint64_t value = 2; int event_fd = eventfd(0, EFD_NONBLOCK); struct pollfd pfd; int status = 0; uint64_t check_value = 0; printf("event_fd=%d\n", event_fd); write(event_fd, &value, sizeof(value)); write(event_fd, &value, sizeof(value)); write(event_fd, &value, sizeof(value)); read(event_fd, &check_value, sizeof(check_value)); printf("read, value=%ld\n", check_value); int ret = fork(); if (ret < 0) { printf("fork error\n"); } if (ret == 0) { pfd.fd = event_fd; pfd.events = POLLIN; printf("__FILE: %s, __LINE:

Is it possible to double-fork a process in Java?

放肆的年华 提交于 2019-12-07 12:19:49
问题 I need to double-fork vmware so it doesn't inherit the terminal ID (TTY/pts). This is what I have so far, but I can't get access to the runtime.exec process to fork another process (which removes the terminal ID). Runtime runtime = Runtime.getRuntime(); Process proc = runtime.exec("vmware"); Is there a way to "daemonize" in Java? 回答1: You will need to write a small C or C++ program to do the extra fork, setsid, all that stuff. 回答2: To make your life easier with this, you essentially need to

What does this C code do?

巧了我就是萌 提交于 2019-12-07 11:37:25
问题 I'm really new to C programming, although I have done quite a bit of other types of programming. I was wondering if someone could explain to me why this program outputs 10. #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #include <stdlib.h> int value = 10; int main() { pid_t pid; pid = fork(); if(pid == 0){ value += 10; } else if(pid > 0){ wait(NULL); printf("parent: value = %d\n", value); //Line A exit(0); } } I know the output is "parent: value = 10".

perl background process

陌路散爱 提交于 2019-12-07 10:23:13
问题 I am trying to run a background process in perl. I create a child process, which is used to call another perl script. I want to run few lines of code parallely with this child process. And after the child process is done.I want to print a line of code. Main script #!/usr/bin/perl $|=1; print "before the child process\n"; my $pid = fork(); if (defined $pid) { system("perl testing.pl"); } print "before wait command\n"; wait(); print "after 20 secs of waiting\n"; testing.pl #!/usr/bin/perl print

Why does Process.fork make stuff slower in Ruby on OS X?

ぐ巨炮叔叔 提交于 2019-12-07 10:14:23
问题 Can someone explain to me why Process.fork makes stuff so much slower in Ruby? I'm using Ruby 2.3.1 on OS X El Capitan. require 'time' require 'benchmark' def do_stuff 50000.times { Time.parse(Time.utc(2016).iso8601) } end puts Benchmark.measure { do_stuff } # => 1.660000 0.010000 1.670000 ( 1.675466) Process.fork do puts Benchmark.measure { do_stuff } # => 3.170000 6.250000 9.420000 ( 9.508235) end EDIT: Just noticed that running that code on Linux (tested Debian or Ubuntu) does not result

cat/Xargs/command VS for/bash/command

心已入冬 提交于 2019-12-07 07:36:57
问题 The page 38 of the book Linux 101 Hacks suggests: cat url-list.txt | xargs wget –c I usually do: for i in `cat url-list.txt` do wget -c $i done Is there some thing, other than length, where the xargs-technique is superior to the old good for-loop-technique in bash? Added The C source code seems to have only one fork. In contrast, how many forks have the bash-combo? Please, elaborate on the issue. 回答1: From the Rationale section of a UNIX manpage for xargs. (Interestingly this section doesn't

waiting for all pids to exit in php

你离开我真会死。 提交于 2019-12-07 05:54:05
问题 My issue is this. I am forking a process so that I can speed up access time to files on disk. I store any data from these files in a tmp file on local desk. ideally, after all processes have finished, I need to access that tmp file and get that data into an array. I then unlink the tmp file as it is no longer needed. My problem is that it would seem that pcntl_wait() does not acutally wait until all child processes are done before moving on to the final set of operations. So I end up

Manually set 'forked from' to GitHub project

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 05:54:03
问题 My project A (hosted on GitHub) somehow (not sure how) forgot that it was originally forked from another open-source project B. By 'forgot', I mean, when creating a pull request, I cannot choose B as a target for sending the pull request. Is there some way I tell GitHub that A is a fork of B? (I can create a PR by forking B into A', then merging A into A' and sending the PR from A' to B which naturally works but, naturally, I don't like it) 回答1: No, there is no way to turn an existing repo

multiprocessing on tee'd generators

喜欢而已 提交于 2019-12-07 04:13:46
问题 Consider the following script in which I test two ways of performing some calculations on generators obtained by itertools.tee : #!/usr/bin/env python3 from sys import argv from itertools import tee from multiprocessing import Process def my_generator(): for i in range(5): print(i) yield i def double(x): return 2 * x def compute_double_sum(iterable): s = sum(map(double, iterable)) print(s) def square(x): return x * x def compute_square_sum(iterable): s = sum(map(square, iterable)) print(s) g1

What happens if a child process won't close the pipe from writing, while reading?

删除回忆录丶 提交于 2019-12-07 04:01:27
问题 Given the following code: int main(int argc, char *argv[]) { int pipefd[2]; pid_t cpid; char buf; if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(EXIT_FAILURE); } if (pipe(pipefd) == -1) { perror("pipe"); exit(EXIT_FAILURE); } cpid = fork(); if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (cpid == 0) { /* Child reads from pipe */ close(pipefd[1]); /* Close unused write end */ while (read(pipefd[0], &buf, 1) > 0) write(STDOUT_FILENO, &buf, 1); write(STDOUT_FILENO, "