fork

Calling 'ls' with execv

做~自己de王妃 提交于 2020-01-04 04:08:25
问题 I am new to system calls and C programming and am working on my university assignment. I want to call the 'ls' command and have it print the directory. What I have: (I have added comments in so you can see what I see coming through each variable. int execute( command* cmd ){ char full_path[50]; find_fullP(full_path, p_cmd); //find_fullP successfully updates full_path to /bin/ls char* args[p_cmd->argc]; args[0] = p_cmd->name; int i; for(i = 1; i < p_cmd->argc; i++){ args[i] = p_cmd->argv[i]; }

What's the proper way to fork() in FastCGI?

半世苍凉 提交于 2020-01-04 02:48:10
问题 I have an app running under Catalyst+FastCGI. And I want it to fork() to do some work in background. I used this code for plain CGI long ago (and it worked): defined(my $pid = fork) or die "Can't fork: $!"; if ($pid) { # produce some response exit 0; } die "Can't start a new session: $!" if setsid == -1; close STDIN or die $!; close STDOUT or die $!; close STDERR or die $!; # do some work in background I tried some variations on this under FastCGI but with no success. How should forking be

fork + wait gets EINTR on OSX

折月煮酒 提交于 2020-01-03 18:50:53
问题 I fail at fork 101. I expect this to fork a child process, and output both child and parent printfs: pid_t fpid; if ((fpid = fork()) < 0) { printf("fork: %s\n", strerror(errno)); exit(-1); } if (0 == fpid) // child { printf("\nI am the child\n"); } else { printf("\nI am the parent\n"); pid_t wpid; while ((wpid = waitpid(WAIT_ANY, NULL, 0))) { if (errno == ECHILD) break; else if (wpid < 0) printf("wait: %s\n", strerror(errno)); } } Instead, I get this output: I am the parent wait: Interrupted

PHP 实现守护进程

扶醉桌前 提交于 2020-01-03 15:50:13
守护进程 守护进程作为一种常驻进程服务,很常见,例如 PHP-FPM, NGINX,REDIS,都需要一个父进程来支持整个服务。但是用 PHP 编写守护进程不多见,今天就来用 PHP 来实现一下。 步骤 ● fork 子进程 ● 父进程退出 ● 设置新的会话 ● 重置文件掩码 ● 关闭标准输入输出 实现 我们对着以上的步骤来实现,在这之前需要 pcntl 和 posix 扩展,请确保安装了。 function daemon() { $pid = pcntl_fork(); // fork 失败 if ($pid < 0) { exit('fork failed'); } else if ($pid > 0) { // 退出父进程 exit(0); } // 设置新的会员 // setsid 有几个注意点 // 不能是进程组的组长调用 // 对于进程组组员调用会产生新的会话和进程组,并成为该进程组的唯一成员,调用的进程将脱离终端 if (posix_setsid() < 0) { exit('set sid failed'); } // 重置文件掩码 umask(0); // 切换工作目录 chdir('/'); // 关闭标准输入输出 fclose(STDIN); fclose(STDOUT); fclose(STDERR); } 细节 // 获取进程ID var_dump

Input redirection problem while using execvp?

≯℡__Kan透↙ 提交于 2020-01-03 00:53:13
问题 I have Implemented a simple program which simulates $ls -l | wc -c command execution using simple pipes and execvp calls. Now After redirecting stdin and stdout ,when executes the program , shell prompt disappears and it waits for the enter key to be pressed. Any way of solving this issue . Plz criticize my code also.. Thanks /* Create pipe */ ret_val=pipe(pipe_fd); /*Error check */ if(ERROR==ret_val) { perror("Pipe creation error \n"); _exit(FAILURE); } /*Fork First Child */ pid_one = fork()

Can't fork more than 200 processes, sometimes, less, depending on memory, cpu usage

南笙酒味 提交于 2020-01-03 00:32:32
问题 Here's the guts of the program using Parallel::ForkManager. It seems to stop at 200 proccesses, sometimes its around 30, depending on the size of the pgsql query that collects URLs to send to Mojo::UserAgent. There seems to be some hard limits somewhere? Is there a better way to write this so that I don't run into those limits? The machine its running on has 16 CPUs and 128GB of memory, so it can certainly run more than 200 proccesses that will die after the Mojo::UserAgent timeout, which is

process from pcntl_fork not terminating

久未见 提交于 2020-01-02 21:20:53
问题 I am running a web service which involved with daemons by php+apache2. So I tried pcntl_fork function. But there is a question that the child process are not terminating even I used exit(0) in the child process's code which result in a lot of apache2 processes. I'm wondering if there is a way to shutdown those useless apache2 processes? PS: because I'm not very aware of the mechanism of signal, so I tried to make daemon by a single call to a agent script which will exit as soon as the child

node child_process模块

跟風遠走 提交于 2020-01-02 21:00:06
NodeJs是一个单进程的语言,不能像Java那样可以创建多线程来并发执行。当然在大部分情况下,NodeJs是不需要并发执行的,因为它是事件驱动性永不阻塞。但单进程也有个问题就是不能充分利用CPU的多核机制,根据前人的经验,可以通过创建多个进程来充分利用CPU多核,并且Node通过了child_process模块来创建完成多进程的操作。 child_process模块给予node任意创建子进程的能力,node官方文档对于child_proces模块给出了四种方法,映射到操作系统其实都是创建子进程。但对于开发者而已,这几种方法的api有点不同 child_process.exec(command[, options][, callback]) 启动 子进程来执行shell命令,可以通过回调参数来获取脚本shell执行结果 const { exec } = require('child_process'); exec('cat *.js bad_file | wc -l', (error, stdout, stderr) => { if (error) { console.error(`exec error: ${error}`); return; } console.log(`stdout: ${stdout}`); console.log(`stderr: ${stderr}`);

How does one use the wait() function when forking multiple processes?

落爺英雄遲暮 提交于 2020-01-02 17:29:17
问题 Learning to use the fork() command and how to pipe data between a parent and it's children. I am currently trying to write a simple program to test how the fork and pipe functions work. My problem seems to be the correct use/placement of the wait function. I want the parent to wait for both of its children to finish processing. Here is the code I have so far: int main(void) { int n, fd1[2], fd2[2]; pid_t pid; char line[100]; if (pipe(fd1) < 0 || pipe(fd2) < 0) { printf("Pipe error\n"); return

An error in one job contaminates others with mclapply

落花浮王杯 提交于 2020-01-02 06:36:48
问题 When mclapply(X, FUN) encounters errors for some of the values of X , the errors propagate to some (but not all) of the other values of X : require(parallel) test <- function(x) if(x == 3) stop() else x mclapply(1:3, test, mc.cores = 2) #[[1]] #[1] "Error in FUN(c(1L, 3L)[[2L]], ...[cut] # #[[2]] #[1] 2 # #[[3]] #[1] "Error in FUN(c(1L, 3L)[[2L]], ... [cut] #Warning message: #In mclapply(1:3, test, mc.cores = 2) : # scheduled core 1 encountered error in user code, all values of the job will