fork

C program to perform a pipe on three commands

梦想与她 提交于 2019-12-08 02:34:30
问题 I have to write a program that will perform the same operation that du | sort | head in the command line would do, but I'm stuck, and my program is not working. The output right now is 112 . and the program doesn't terminate. Please help, I don't know what to do! int main(void) { int fd[2]; int fd1[2]; int pid; if (pipe(fd) == -1) { perror("Pipe"); exit(1); } switch (fork()) { case -1: perror("Fork"); exit(2); case 0: dup2(fd[1], STDOUT_FILENO); close(fd[0]); close(fd[1]); execl("/usr/bin/du"

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

跟風遠走 提交于 2019-12-08 02:01:20
问题 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

For ruby/webrick, I need windows to recognize shebang (#!) notation

岁酱吖の 提交于 2019-12-08 01:56:41
问题 (Bear with me, I promise this gets to shebang and windows.) I have about the simplest of WEBRick servers put together: require 'webrick' include WEBrick s = HTTPServer.new(:Port=>2000, :DocumentRoot=>Dir::pwd) s.start Couldn't be simpler. This basic server does accept http connections (firefox, internet exploder, wget, TELENT) and deals with them appropriately, as long as I'm just fetching static documents. If, however, I set one of the files in the directory to have a .cgi extension, I get a

Pipe for multiple processes

落花浮王杯 提交于 2019-12-07 23:55:49
问题 Currently working on some homework and having a hard time. The goal is to generate 100,000 numbers and add them all together by dividing the work into 10 processes (10,000 numbers each) I think I've figured out how to fork processes (hopefully), but using Pipe() to relay the subtotals from each child process is not working... the program below returns 44901 for each child process and 449010 for the running total. I'm struggling hard but I feel like this is something simple I should be able to

Communicating between a parent and its children

倾然丶 夕夏残阳落幕 提交于 2019-12-07 20:43:20
问题 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)

C synchronize processes using signal

隐身守侯 提交于 2019-12-07 19:44:49
问题 Okay so I am trying to teach myself on how to do signalling, and I came across a hiccup and I can't figure out what I'm doing wrong. What is going on right now is: it is executing the parent then goes to child and then back to parent.. It's not doing what I want it to do which is execute the parent (which the user defines the amount of time it runs) then kills it then go to child and run itself at the same amount of time. #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include

Monitoring and restarting child process when fails/exits

做~自己de王妃 提交于 2019-12-07 18:11:15
问题 I've created a rudimentary example of monitoring a child process and restarting it when it fails or exits. What is the preferred/more robust method of doing this? Is it good practice to continuously poll for a change in status? My understanding is that I should utilize something like SIGCHLD but have been unable to find any good examples. I'm an embedded C coder mainly and this is my first attempt at trying to understand fork() .The purpose of this code will eventually be to monitor a call to

系统调用的前因后果

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 16:34:39
系统调用就是系统提供给你的一些函数你可以直接使用的最基础的函数 比如open fock read等 那么系统调用是什么我们要和电脑打交道当然你想要打开一个文件 在vs上打开文件都是通过fopen当然也可以直接调用系统调用如果没有系统调用open 那么就打不开文件 学过c++和c语言的就应该知道printf 这个函数 这个是库里面的函数 vs上不开源但是本质上来说也就是一个系统调用 或者是几个系统调用组合起来 由于window底下不开源 那么我们研究一下linux底下的系统调用 首先得知道我们运行一个程序需要将代码加载到物理内存上(一般来说这个物理内存是4G) 但是实际上我们每一个进程都有4G大小的虚拟内存 其实也就是这样 既然已经知道虚拟地址空间是个怎么样的了那么就来说一下一个系统调用是如何执行的 在linux下比如我们执行fork函数 那么首先我们得包含头文件unistd.h 我们研究的是linux 2.6 版本的 (为什么要说版本应为可能在不同版本下可能会有一些差距但是思想是不变的) 另外可能会有人看到这个头文件了之后直接来找sys_fork 但是实际上系统不是这样做的 而是通过一个宏 再这样把宏给放进去 如果看不懂可以这样理解name就是——NR_fork type就是定义的2 int 0x80 这个代表的一种中断 是指切换到内核的中断

Proper fork() and pipe() use for a single parent having multiple children. How do I do this right?

北战南征 提交于 2019-12-07 16:31:08
问题 So, my PREVIOUS POST was flagged for being off topic, too vague, and asking for opinions and recommendations for useful code. It did those things, so I am re-posting as a question on code I am working on. Thanks to those in the previous post, I was able to piece what I have here from reviewing what you guys said. The focus here is the Parent/Child relationship and the use of fork() and pipe() to get the desired affect. The project is a POSIX card game where the parent (dealer) forks into 5

I/O重定向和管道——《Unix/Linux编程实践教程》读书笔记(第10章)

拈花ヽ惹草 提交于 2019-12-07 16:23:53
1、I/O重定向的概念与原因 及 标准输入、输出的标准错误的定义 所以的Unix I/O重定向都基于标准数据流的原理。三个数据了分别如下: 1)标准输入——需要处理的数据流 2)标准输出——结果数据流 3)标准错误输出——错误消息流 概念:所以的Unix工具都使用文件描述符0、1和2。标准输入文件的描述符是0,标准输出的文件描述符是1,而标准错误输出的文件描述符则是2。Unix假设文件描述符0、1、2已经被打开,可以分别进行读写操作。 通常通过shell命令行运行Unix系统工具时,stdin、stdout、stderr连接在终端上。因此,工具从键盘读取数据并且把输出和错误消息写到屏幕。 大部分的Unix工具处理从文件或标准输入读入的数据。如果在命令行上给出了文件名,工具将从文件读取数据。若无文件名,程序则从标准输入读取数据。从另一方面说,大多数程序并不接收输出文件名;它们总是将结果写到文件描述符1,并将错误消息写到文件描述符2。如果希望将进程的输出写道文件或另一个进程的输入去,就必须重定向相应的文件描述符。 重定向I/O的是shell而不是程序 最低可用文件描述符(lowest-available-fd)原则:文件描述符是一个数组的索引号。每个进程都有其打开的一组文件。这些打开的文件被保持在一个数组中。文件描述符即为某文件在此数组中的索引。当打开文件时