fork

Fork-join框架

爱⌒轻易说出口 提交于 2019-12-20 07:03:35
这是一个JDK7引入的并行框架,它把流程划分成fork(分解)+join(合并)两个步骤(怎么那么像MapReduce?),传统线程池来实现一个并行任务的时候,经常需要花费大量的时间去等待其他线程执行任务的完成,但是fork-join框架使用work stealing技术缓解了这个问题: 每个工作线程都有一个双端队列,当分给每个任务一个线程去执行的时候,这个任务会放到这个队列的头部; 当这个任务执行完毕,需要和另外一个任务的结果执行合并操作,可是那个任务却没有执行的时候,不会干等,而是把另一个任务放到队列的头部去,让它尽快执行; 当工作线程的队列为空,它会尝试从其他线程的队列尾部偷一个任务过来; 取得的任务可以被进一步分解。 ForkJoinPool.class,ForkJoin框架的任务池,ExecutorService的实现类 ForkJoinTask.class,Future的子类,框架任务的抽象 ForkJoinWorkerThread.class,工作线程 RecursiveTask.class,ForkJoinTask的实现类,compute方法有返回值,下文中有例子 RecursiveAction.class,ForkJoinTask的实现类,compute方法无返回值,只需要覆写compute方法,对于可继续分解的子任务,调用coInvoke方法完成

C Shell hanging when dealing with piping

亡梦爱人 提交于 2019-12-20 06:28:06
问题 I'm working on a C shell and am having trouble with getting an arbitrary amount of pipes to work. When I run the shell, it hangs on any piping. For some reason, when I do ls -la | sort , it hangs on the sort until I enter stuff and hit Ctrl + D . I know it has something to do with a pipe not closing, but the print statements show that pipes 3,4,5 all get closed in both the parent and child. I've been at this for a few hours and don't know why this doesn't work. Any help would be much

how fork works with logical operators

一曲冷凌霜 提交于 2019-12-20 06:16:01
问题 main() { if (fork() || (fork() && fork())) printf("AA\n"); else if (!fork()) printf("BB\n"); else printf("CC\n"); } I have run the following code and get the results AA AA CC BB CC BB. While I understand how fork works, I don't understand what it does with logical operators. The teacher in our class wants us to give the answers for this homework. While I can easily run this program, I would like to know what happens exactly. Can anyone explain or direct me to a website to what happens when

How to get the exit status of a child process?

给你一囗甜甜゛ 提交于 2019-12-20 05:48:11
问题 Two example outputs (provided by my professor) are (these are inputted in the Linux terminal): ibrahim@ibrahim-latech:~$ ./prog2 . Current working directory: /home/ibrahim Executing ls . --all -l --human-readable total 24M drwxr-xr-x 74 ibrahim ibrahim 20K Oct 26 16:08 . drwxr-xr-x 6 root root 4.0K Apr 10 2014 .. -rw-r--r-- 1 ibrahim ibrahim 4.1K Sep 13 12:09 .bashrc drwxr-xr-x 7 ibrahim ibrahim 4.0K Oct 11 14:51 Desktop/ ...snip... Exit status: 0 so the Exit status is 0 if the code works but

Executing a command-line pipeline in C with fork() and exec()

情到浓时终转凉″ 提交于 2019-12-20 05:47:26
问题 I asked about my code and the answer was that it is incorrect. perror usage in this case Now I wonder how I can adjust and improve so that I no longer will have these errors? execvp does not return, except if an error occurs, so if all works, the enclosing function will never return. the value 'i' is already past the end of the array in 'cmd' due to the prior loop, so 'cmd[i].argv[0] is not correct. cmd is not an array, of struct command, so should not be indexed the first entry in cmd.argv

vfork() atexit assertion failed

老子叫甜甜 提交于 2019-12-20 05:39:28
问题 I am trying to understand the following piece of code #include<stdio.h> #include<unistd.h> #include<sys/types.h> int main() { pid_t pid ; unsigned int i=0; pid=vfork(); switch(pid) { case -1: // some sort of error puts("fork error"); break; case 0: // the child process while(i<100) { printf("%d\n", i); i++; } break; default: //parent while(i<1000) { printf("%d\n", i); i++; } break; } // _exit(0); } And please don't tell me that vfork() is bad and these kind of things .I know it is , but what

fork() execution in for loop

倾然丶 夕夏残阳落幕 提交于 2019-12-20 05:19:14
问题 int main(int argc, char** argv) { int i = 0; while (i < 2) { fork(); system("ps -o pid,ppid,comm,stat"); i++; } return (EXIT_SUCCESS); } Can anyone tell me how many times ps command is executed with an explanation? 回答1: I believe the answer is 6. in the first iteration, fork() is called, splitting the process in 2, thus calling ps twice. in the second iteration, fork is called again in each process, so you now have 4 processes running ps. total calls to ps: 2+4=6. 回答2: 6 times. It creates a

Improving HTML scraper efficiency with pcntl_fork()

南笙酒味 提交于 2019-12-20 03:18:30
问题 With the help from two previous questions, I now have a working HTML scraper that feeds product information into a database. What I am now trying to do is improve efficiently by wrapping my brain around with getting my scraper working with pcntl_fork. If I split my php5-cli script into 10 separate chunks, I improve total runtime by a large factor so I know I am not i/o or cpu bound but just limited by the linear nature of my scraping functions. Using code I've cobbled together from multiple

fork() failed (1: Operation not permitted)

笑着哭i 提交于 2019-12-20 02:34:07
问题 I'm new to ios. I'm facing a problem about permission. fork() failed (1: Operation not permitted) I want to build an open source library implemented in c for ios. Can "fork()" function be used on ios?? If It could, how can I solve that problem?? 回答1: No. Every iOS application is a separate process. You can create new threads, not a new process. Also see Porting an application with fork() to pthread_create() 回答2: fork() can only be used on jailbroken phones. 来源: https://stackoverflow.com

Multiprocessing and Pipes in C

折月煮酒 提交于 2019-12-20 01:39:53
问题 I'm trying to learn how to work with fork() to create new processes and pipes to communicate with each process. Let's say I have a list that contains 20 words, and I create 3 processes. Now, I need to distribute the words between the processes using pipes, and the each process will sort the list of words it receives. The way I want to achieve this is like this: Word1 => Process1 Word2 => Process2 Word3 => Process3 Word4 => Process1 Word5 => Process2 Word6 => Process3 . . . So each process