fork

fork with Ruby 1.8 and Windows

人走茶凉 提交于 2019-12-06 15:11:43
I'm using ruby 1.8.7 patchlevel 302 and I'm working on a Windows xp system. I have to start an external process that needs to react on user input. The process doesn't react if I use threads, so I tried using fork. With fork the external process reacts on the user input but it executes more than just the fork block. For example fork do puts 'child' end puts 'parent' Process.wait puts 'done' produces the following output on my machine: parent child parent done done As you can see 'done' and 'parent' is printed twice. What can I do to make the child execute only its block and not more? (I can't

进程控制

家住魔仙堡 提交于 2019-12-06 15:01:47
目录 进程控制 fork后的资源 fork的后续操作 vfork exit 退出状态与终止状态 为什么需要僵死进程 消除这个僵尸进程 子进程先结束,但是父进程不去管也不产生僵尸进程 title: 进程控制 date: 2019/11/30 15:13:40 toc: true --- 进程控制 fork后的资源 缓冲区,比如 printf 等资源没有 fflush ,缓冲区会被复制到子进程中 父进程重定向后,子进程也被重定向.父进程的文件描述符都被复制到子进程,类似dup,共享文件表项 子进程继承的属性 子进程不继承的属性 fork的后续操作 类似网络服务器多进程,子进程对应一个客户端 执行 exec ,执行另外的函数,这种情况下进入 exec 后原来的文件表项我们用不到了,所以一般会在文件描述符设置标志 close-on-exec ,也就是这个文件描述符在进入 exec 后被关闭,但在 fork 和 exec 间依然可用 vfork https://www.cnblogs.com/1932238825qq/p/7373443.html vfork 的诞生就是给 exec 使用,也就是创建一个全新的进程用的 fork 是 创建一个子进程,并把父进程的内存数据copy到子进程中。 vfork 是 创建一个子进程,并和父进程的内存数据share一起用。保证子进程先执行

source

扶醉桌前 提交于 2019-12-06 14:25:35
你是否被下面的几个问题困扰过,甚至至今无法真正理解? 什么是 export ,什么时候用export,为什么有时用了export还要 source ? 为什么用 env 来设置环境变量,不用export,有什么好处? source 和 exec 有什么区别? 本文试图通过普及unix进程、环境变量等概念,让读者真真理解这些shell命令的本质,知道这些命令的使用场合。 首先,先对这些命令做一个解释,如果读者能完全理解,那么本文也许对你帮助不大。 set 设置了当前shell进程的 本地变量 ,本地变量只在当前shell的进程内有效,不会被子进程继承和传递。 env 仅为将要执行的子进程设置 环境变量 。 export 将一个shell本地变量提升为当前shell进程的 环境变量 ,从而被子进程自动继承,但是export的变量无法改变父进程的环境变量。 source 运行脚本的时候,不会启用一个新的shell进程,而是在当前shell进程环境中运行脚本。 exec 运行脚本或命令的时候,不会启用一个新的shell进程,并且exec后续的脚本内容不会得到执行,即当前shell进程结束了。 在这些表述中,反复提到 进程 和 环境变量 的概念。如果希望深入理解其中的含义,还必须理解进程的相关概念。 进程和环境变量 进程是一个程序执行的上下文集合,这个集合包括程序代码、数据段、堆栈、环境变量

如何写一个daemon程序

扶醉桌前 提交于 2019-12-06 13:56:17
在实际的服务器后台程序开发时,有时需要将某个服务 daemon 化来完成一些定时任务,比如往 KV 系统中刷新数据。 有两个问题需要提前弄清楚: daemon 程序中的 pid file 有什么作用? 为什么要二次fork,只fork一次可以吗? Daemon class 这是一个 Daemon class,继承此 class 的类需要 override 里面的 run 方法。 #!/usr/bin/env python import sys, os, time, atexit from signal import SIGTERM class Daemon: def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): self.stdin = stdin self.stdout = stdout self.stderr = stderr self.pidfile = pidfile def daemonize(self): try: pid = os.fork() if pid > 0: # exit first parent sys.exit(0) except OSError, e: sys.stderr.write("fork #1 failed: %d (%s

Checking fork behaviour in python multiprocessing on Linux systems

时光毁灭记忆、已成空白 提交于 2019-12-06 13:41:11
I have to access a set of large and not pickable python objects from many processes. Therefore, I would like to ensure that these objects are not copied completely. According to comments in this and this post, objects are not copied (on unix systems) unless they are changed. However, referencing an object will change its reference count, which in turn will then be copied. Is this correct so far? Since my concern is due to the size of my large objects, I do not have a problem, if small parts of these objects are copied. To ensure that I understood everything correctly and that nothing

Communicating between a parent and its children

倾然丶 夕夏残阳落幕 提交于 2019-12-06 13:37:21
Newbie question: On Unix, in a program with a parent and some children: - How can the parent alert the children efficiently to do some work.. ? - Or how can the children wait for parent signal to start doing some work? EDIT: This program tries to do a complex computation in parallel, I have already used shared memory as a common workspaces for all children to update results and for data transfer. What I need now is the parent say "start" efficiently to all its children...(called many times) Thanks Your ipc tag says it all. You need to look into inter-process communuication: Shared memory.

C++, linux, fork, execvp, waitpid and SIGTSP

萝らか妹 提交于 2019-12-06 13:25:31
I'm implementing a Terminal for a Home Work. I almost finished, I just need to implement a bg ( Background ) and a fg ( Foreground ) commands. my code looks like this: void run(){ string command[] = parseMyInput( getInput() ); int fork_result = fork(); if( -1 == fork_result ) //handle error else if( 0 == fork_result ){ // child setpgrp(); // I don't want the children to get the signals if( -1 == execvp( command[0], makeArgs(command) ) ) //handle error } else { // parent if( command[ length - 1 ] != "&" ){ int status; waitpid( fork_result, &status, 0 ); //continue after child is finished //(

Redis持久化存储详解

半世苍凉 提交于 2019-12-06 12:32:44
为什么要做持久化存储? 持久化存储是将 Redis 存储在内存中的数据存储在硬盘中,实现数据的永久保存。我们都知道 Redis 是一个基于内存的 nosql 数据库,内存存储很容易造成数据的丢失,因为当服务器关机等一些异常情况都会导致存储在内存中的数据丢失。 持久化存储分类 在 Redis 中,持久化存储分为两种。一种是 aof 日志追加的方式,另外一种是 rdb 数据快照的方式。 RDB持久化存储 什么是RDB持久化存储 RDB持久化存储即是将redis存在内存中的数据以快照的形式保存在本地磁盘中。 .RDB持久化存储分为自动备份和手动备份 1.手动备份通过 save 命令和 bgsave 命令。save是同步阻塞,而 bgsave 是非阻塞(阻塞实际发生在 fork 的子进程中)。因此,在我们实际过程中大多是使用bgsave命令实现备份. redis> SAVE OK redis> BGSAVE Background saving started 2.自动备份 a.修改配置项 save m n即表示在 m 秒内执行了 n 次命令则进行备份. b.当Redis 从服务器项主服务器发送复制请求时,主服务器则会使用 bgsave命令生成 rbd 文件,然后传输给从服务器. c.当执行 debug reload 命令时也会使用 save 命令生成rdb文件. d.当使用

Share a file descriptor between parent and child after fork and exec

有些话、适合烂在心里 提交于 2019-12-06 12:27:28
I have two processes on Linux, A & B. I want to share the file descriptor from process A with process B, now I just serialize it to a char* and pass it to the execl parameters, but that doesn't work. A.c looks like this: union descriptor{ char c[sizeof(int)]; int i; } fd; pid_t pid; fd.i = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Perform other socket functions pid = fork(); if(pid == 0){ // Read data from socket if(execl("./B", fd.c, NULL) < 0){ exit(EXIT_FAILURE); }else( exit(EXIT_SUCCESS); } }else if(pid < 0){ exit(EXIT_FAILURE); }else{ waitpid(pid, NULL, 0); } B.c looks like this:

If I fork() and then do an execv(), who owns the console?

泪湿孤枕 提交于 2019-12-06 12:19:19
问题 I am writing a Linux application. What happens if I call fork() and then run an application that takes console input? Consider the code below: int process_id = fork(); if (process_id != 0) { /* this is the parent process */ error = execv("../my_other_app", "parameter1", NULL); if (error < 0) { printf("error!"); } } else { /* this is the child process. Wait for my_other_app to set up */ sleep(3); /* now continue */ } printf("########## press ENTER to stop ##########\n"); getchar(); exit(0);