fork

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

南笙酒味 提交于 2019-12-05 22:32:51
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? You will need to write a small C or C++ program to do the extra fork, setsid, all that stuff. To make your life easier with this, you essentially need to disconnect the application from the shell by closing the input / output streams that are implicitly connected to

shared memory after exec()

匆匆过客 提交于 2019-12-05 21:53:36
How can I share memory between the parent and the child if the child has run exec() to load another program? Is it possible using mmap ? By now parent and child share memory properly using mmap, but not after exec is done You can use shm_open to open a "named" shared memory block which is identified by a file on the filesystem. Example: In the parent: int memFd = shm_open("example_memory", O_CREAT | O_RDWR, S_IRWXU); if (memFd == -1) { perror("Can't open file"); return 1; } int res = ftruncate(memFd, /*size of the memory block you want*/); if (res == -1) { perror("Can't truncate file"); return

Binary Process Tree with fork()

落爺英雄遲暮 提交于 2019-12-05 21:38:48
My first project for my OS class is to create a process tree using fork() that has a depth that the user specifies at the command line. Each leaf level node needs to sort data and pass it back to its parent using named-pipes (FIFOs). I can create an N-depth tree with fork() , each process having 2 children. What I can’t figure out is how to pass a FIFO to each child all the way down the tree and then have this process perform a sort on certain data in the FIFO and then also pass it back up the tree to the top. Here is the pseudo-code of what I have so far for building the tree: void CreateTree

execution of if/else if/else with a fork()

吃可爱长大的小学妹 提交于 2019-12-05 20:42:10
I have tried implementing an os program. Here is the code: #include<sys/types.h> #include<stdio.h> #include<unistd.h> int main() { pid_t pid, pid1; pid = fork(); if(pid<0) { fprintf(stderr,"Fork Failed"); return 1; } else if(pid == 0) /* child process */ { pid1 = getpid(); printf("child: pid = %d\n",pid); printf("child: pid1 = %d\n",pid1); } else /* parent process */ { pid1 = getpid(); printf("parent: pid = %d\n",pid); printf("parent: pid1 = %d\n",pid1); } return 0; } and its o/p: parent: pid = 1836 parent: pid1 = 1835 child: pid = 0 child: pid1 = 1836 can somebody explain me how is it working

Redis持久化

[亡魂溺海] 提交于 2019-12-05 20:15:18
为什么要做持久化存储? 持久化存储是将 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

缺陷的背后(四)---for

陌路散爱 提交于 2019-12-05 20:12:23
导语 业务模块为实现高并发时的更快的处理速度,经常会采用多进程的方式去处理业务。本章就是站在多进程的角度下,理解多进程模式下常见的两种bug:僵尸进程,窜包返回。理解问题出现的原因以及如何避免,如何 有效的测试出这类缺陷。 目录 一:线上缺陷分析 二:多进程概念理解 三:僵尸进程理解 3.1 产生原因 3.2 如何避免 3.3 问题场景 四:窜包 4.1 产生原因 4.2 如何避免 4.3 问题场景 二:多进程概念理解 概念2.1: fork子进程 进程(Process)是计算机中已运行程序的实体,是系统的基本运作单位,是资源分配的最小单位,fork子进程后,子进程会复制父进程的状态(内存空间数据等)。fork 出来的进程和父进程拥有同样的上下文信息、内存数据、与进程关联的文件描述符。 下面结合demo来看看有没有理解这句话。 问题1 :全局变量list1,fork子进程后,在子进程内:打印list1的虚拟地址,修改list1[0]的值,打印list1值,打印list1虚拟地址。 主进程内:打印list1的虚拟地址,待子进程修改后,打印list1值。 子进程和主进程打印的虚拟地址值是一样的吗?打印的list1的值是一样的吗? import osimport timelist1 =[1,2,3,4]print("list1的地址为{0}".format(id(list1))

C - named pipe for multiple forked children

∥☆過路亽.° 提交于 2019-12-05 20:08:15
If you have multiple children created by fork(), and the method of communication with the parent is "named pipes", do you need multiple named pipes? One for each child? Or can you make one and have the parent read from that? Basically, is there anything else you need to do? I understand if several children write to the same named pipe at the same time, it might cause a problem with reading a whole message from a single child. Is there a way to make sure the writes are atomic? You can have several writers with a single pipe. However, as you say communication is between fork()ed children and the

Java equivalent of fork in Java task of Ant?

穿精又带淫゛_ 提交于 2019-12-05 19:58:17
Ant Java task provides fork parameter, which, by definition "if enabled triggers the class execution in another VM" . As we are dealing with a large amount of data, setting this parameter saved us from running out of Java heap space. We want to be able to do the same through a Java class. What would be the best way to achieve the functionality as provided by fork ? Execute another java process. By using ProcessBuilder class, for example. http://java.sun.com/javase/6/docs/api/java/lang/ProcessBuilder.html You can run as many worker processes as you wish. Make them having a separate main class,

How does copy-on-write in fork() handle multiple fork?

扶醉桌前 提交于 2019-12-05 18:28:14
问题 According to wikipedia (which could be wrong) When a fork() system call is issued, a copy of all the pages corresponding to the parent process is created, loaded into a separate memory location by the OS for the child process. But this is not needed in certain cases. Consider the case when a child executes an "exec" system call (which is used to execute any executable file from within a C program) or exits very soon after the fork(). When the child is needed just to execute a command for the

What does this C code do?

こ雲淡風輕ζ 提交于 2019-12-05 18:08:56
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". Anyone know why? Thanks! About fork() : If fork() returns a negative value, the creation of a child