fork

Reading from stdin by multiple processes

僤鯓⒐⒋嵵緔 提交于 2020-01-24 20:23:26
问题 I am writing a program in which the parent process uses fork() to create N child processes (N is provided as an argument), so that every child is directly forked by this one parent. Every child process needs to read a line from stdin , and print on to the screen. After compiling and executing the program, the text is provided through a text file like this: ./prog1 3 < fileWithText.txt My problem is, that I am expecting to see every child "fight" for reading the input, however what I actually

scanf in while loop

岁酱吖の 提交于 2020-01-24 13:54:06
问题 In this code, scanf works only once. What am I doing wrong? #include <stdio.h> #include <unistd.h> int main() { int i = 1; if (! fork()) { while(i) { printf("Enter i"); scanf("%d", &i); fflush(stdin); fflush(stdout); } } else { printf("Parent\n"); } return(0); } 回答1: It has already been recommended that you not use scanf. If you feel you must use scanf , you really should be checking the return value to determine if an input error occurred prior to the conversion. It has also been noted that

pyrhon多进程操作初探

≯℡__Kan透↙ 提交于 2020-01-24 08:55:26
linux系统中提供了fork函数进行进程的创建,这个接口在函数返回上比较特殊,有两个返回值,一个是子进程返回值为0,一个是父进程返回值,值大于0,表是子进程的ID.如果小于0.则表示接口出错. python做为一个跨平台的语言,在os包中也提供了fork接口,并且沿袭了linux系统中fork接口的传统,接口有两个返回值,含义和linux系统中相似.具体使用如下: 注意事项:多进程在win系统中可能会报错,因为win系统中没有fork接口,所以在学习python的时候还是推荐使用linux或者mac系统,mac系统为上选. 一、使用os.fork #!/usr/bin/python #coding:utf-8 import os print "proess id is %d" % os.getpid() p = os.fork() if(p==0): print "this is a child process" elif p>0: print "this is a pp proccess %d" % p 运行结果: proess id is 27048 this is a pp proccess 27053 this is a child process 二、使用process Process在multiproccessing包中,功能是创建进程,包含的方法主要有:is

How does this other version of the bash fork bomb work?

你离开我真会死。 提交于 2020-01-24 06:44:50
问题 I get the general idea of how this common version :(){ :|:& };: of the bash fork bomb works. However, I've seen another version (for bash in particular) #!/bin/bash $0 & $0 & on the Wikipedia fork bomb article and in an SO answer to a closed duplicate of the original fork bomb question I mentioned above. I'm looking for an explanation of how the second (perhaps less common) version of the fork bomb works. I've commented the code below with my current understanding of what it does, but I don't

How does this other version of the bash fork bomb work?

孤街醉人 提交于 2020-01-24 06:42:08
问题 I get the general idea of how this common version :(){ :|:& };: of the bash fork bomb works. However, I've seen another version (for bash in particular) #!/bin/bash $0 & $0 & on the Wikipedia fork bomb article and in an SO answer to a closed duplicate of the original fork bomb question I mentioned above. I'm looking for an explanation of how the second (perhaps less common) version of the fork bomb works. I've commented the code below with my current understanding of what it does, but I don't

Printing the sequence in breadth first order using fork()

不想你离开。 提交于 2020-01-24 01:20:08
问题 Given a process tree A / \ / \ B C / \ / \ D E F G I am asked to print the sequence in BFS order, i.e A-B-C-D-E-F-G using fork() system call, where each node represents a process with the same parent-child construct as shown in tree(i.e A is parent of B and C, B is parent of D and E like that). I figured out this solution, but I really don't understand how to make it print recursively. static int count; char *msg[] = {"A", "B", "C", "D", "E", "F", "G"}; main(){ if(!fork()){ //Child 1 printf("

C Fork and Pipe closing ends

孤街浪徒 提交于 2020-01-24 00:22:31
问题 I am building an application that requires two way communication with a few child processes. My parent is like a query engine constantly reading words from stdin and passes it to each child process. The child processes perform their processing and writes back to the parent on their exclusive pipes. This is theoretically how it should work however I am stuck on the implementation details. The first issue is do I create 2 pipes before forking the child? When I fork, I know the child is going to

redis持久化实现原理

爱⌒轻易说出口 提交于 2020-01-23 22:09:56
RDB rdb持久化原理: 会涉及到操作系统底层的fork调用,详情查看: https://zhangxueliang.blog.csdn.net/article/details/104076571 会fork出一个子进程用于持久化。 当redis主进程发生数据修改的时候,会触发内核级别的写时复制操作,写数据到持久化文件是子进程来完成的,数据的增删改是在父进程中进行的,所以redis的持久化是fork+copy on write来实现的。 比如8点fork出一个子进程用于持久化操作,此时子进程拷贝的是8点时的数据,父子进程的数据修改,彼此都不可见。假如10点redis数据发生了修改,此时会由内核的写时复制机制触发数据复制操作,将引用指向新的数据,此时子进程的引用还是指向旧数据。写时复制不是为了数据同步,而是数据隔离。 fork出来的子进程会一直等到数据持久化做完后才销毁。每次持久化开始时都会fork出一个子进程。每次拍快照(持久化)都是当前时间点的全量数据覆盖之前的快照数据,如果快照采用增量更新的方式的话,需要在内存中判断哪些数据有更新哪些没更新,反而消耗CPU资源。 拷贝引用的成本比拷贝数据的成本低很多,因为一个引用的大小是4个字节,但引用指向的数据可能是一个数组几百个字节。 redis RDB持久化配置方式: 如果想关闭持久化,只需在配置文件redis.conf中配置:

Linux fork within class on heap

有些话、适合烂在心里 提交于 2020-01-23 08:42:11
问题 What happens when I have the following situation: class A: holds on to dynamically allocated object B's. It will create and destroy these. class B: has an execute function called by A. Execute will fork() and the child will use execvp to run another process. BUT, a flag can be set so that the parent will not wait for the child (allows it to run in the background). My question is, what is fork doing in this case? I know that the child has a complete copy of the process of the parent, but I'm a

Linux fork within class on heap

半城伤御伤魂 提交于 2020-01-23 08:41:25
问题 What happens when I have the following situation: class A: holds on to dynamically allocated object B's. It will create and destroy these. class B: has an execute function called by A. Execute will fork() and the child will use execvp to run another process. BUT, a flag can be set so that the parent will not wait for the child (allows it to run in the background). My question is, what is fork doing in this case? I know that the child has a complete copy of the process of the parent, but I'm a