pipe

Why do I need to close fds when reading and writing to the pipe?

半腔热情 提交于 2019-12-29 18:03:43
问题 Here is an example to illustrate what I mean: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main(void) { int fd[2], nbytes; pid_t childpid; char string[] = "Hello, world!\n"; char readbuffer[80]; pipe(fd); if((childpid = fork()) == -1) { perror("fork"); exit(1); } if(childpid == 0) { /* Child process closes up input side of pipe */ close(fd[0]); /* Send "string" through the output side of pipe */ write(fd[1], string, (strlen(string)+1)); exit(0); } else { /* Parent process

Program based on pipe in Linux

佐手、 提交于 2019-12-29 09:41:47
问题 Write a program for this------>>>>>> One program will open a pipe, write a number to pipe. Other program will open the same pipe, will read the number and print them. Close both the pipes. how can i write a program based on this any one knows it then please help me...!!!! 回答1: I think what you're looking for is: echo <number you want to use> (or output from program) | <program you want to pipe into> For instance: echo 5 | more which will simply display: 5 The "|" is your pipe; it redirects

Building a process pipe with ProcessBuilder in Java 7

為{幸葍}努か 提交于 2019-12-29 08:33:15
问题 I've been trying to figure out how to pipe a few processes in Java using the new ProcessBuilder . I can't find a suitable example of what I want to do and when I try to do it myself the process just hangs. I would appreciate a very simple example of some code that runs the equivalent of cat test.txt | wc , but not through a shell. --Update-- OK, just to clarify. I know there are ways to simulate a pipe by reading and writing streams. I'm wondering if that's done in some automatic way by the

assign a value to a variable in a loop

偶尔善良 提交于 2019-12-29 08:27:18
问题 There are 2 pieces of code here, and the value in $1 is the name of a file which contains 3 lines of text. Now, I have a problem. In the first piece of the code, I can't get the "right" value out of the loop, but in the second piece of the code, I can get the right result. I don't know why. How can I make the first piece of the code get the right result? #!/bin/bash count=0 cat "$1" | while read line do count=$[ $count + 1 ] done echo "$count line(s) in all." #--------------------------------

常见Python中调用Linux命令

隐身守侯 提交于 2019-12-29 07:26:43
有时候我们需要从Python中执行Linux命令,下面就介绍几种方法? 1. os 模块:   1.1 os.system 函数:     system方法会创建子进程运行外部程序,方法只返回外部程序的运行结果。这个方法比较适用于外部程序 没有输出结果的情况 。 >>> os.system('echo \ "Hello World\"') # 直接使用os.system调用一个echo命令 Hello World 0 >>> os.system("cat /proc/meminfo") MemTotal: 486640 kB MemFree: 301184 kB Buffers: 10304 kB Cached: 58316 kB SwapCached: 0 kB Active: 43748 kB Inactive: 45336 kB Active(anon): 20476 kB Inactive(anon): 520 kB Active(file): 23272 kB Inactive(file): 44816 kB     注意:上面说了,此方法只会外部程序的结果,也就是os.system的结果,所以如果你想接收命令的返回值,接着向下看   1.2 os模块的popen方法     当需要得到外部程序的输出结果时,本方法非常有用, 返回一个类文件对象 ,调用该对象的 r ead

How to chain write stream, immediately with a read stream in Node.js 0.10?

心不动则不痛 提交于 2019-12-29 06:44:10
问题 The following line will download an image file from a specified url variable: var filename = path.join(__dirname, url.replace(/^.*[\\\/]/, '')); request(url).pipe(fs.createWriteStream(filename)); And these lines will take that image and save to MongoDB GridFS: var gfs = Grid(mongoose.connection.db, mongoose.mongo); var writestream = gfs.createWriteStream({ filename: filename }); fs.createReadStream(filename).pipe(writestream); Chaining pipe like this throws Error: 500 Cannot Pipe. Not

How do you specify filenames within a zip when creating it on the command line from a pipe?

二次信任 提交于 2019-12-29 03:35:39
问题 I'm trying to create a zip file from file contents which are being piped in, e.g. mysql [params and query] | zip -q output.zip - This writes the zip correctly, but when you open the zip, the file within it is called "-". Is there any way of specifying what the filename of the piped in data should be within the zip? 回答1: From what i can gather you cannot do both with the zip command, i mean you cannot both specify the filenames and pipe the content. You can either pipe the contents and the

Python管道进行数据的吞吐处理

主宰稳场 提交于 2019-12-28 11:07:30
import multiprocessing import random import time import datetime import struct import os import getFile # 76(28) + (2048 + 16) * 512 + 4 frame_flag_0 = 0x0000000000000001 # 8 byte frame_flag_1 = 0x0000000000000002 # 8 byte frame_flag_2 = 0x0000000000000003 # 8 byte frame_flag_9 = 0x00000001 # 4 byte dir_name = 'Z:/' file_out = 'Z:/312.dat' def producer(pipe): print('start time: ' + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) linestr = getFile.getFileList(dir_name, '_312_') for i in range(0, len(linestr)): file_in = linestr[i] f_in = open(file_in, 'rb') f_in.seek(109) while True:

What's the differences between system and backticks and pipes in Perl?

大憨熊 提交于 2019-12-28 02:58:26
问题 Perl supports three ways (that I know of) of running external programs: system : system PROGRAM LIST as in: system "abc"; backticks as in: `abc`; running it through a pipe as in: open ABC, "abc|"; What are the differences between them? Here's what I know: You can use backticks and pipes to get the output of the command easily. that's it (more in future edits?) 回答1: system(): runs command and returns command's exit status backticks: runs command and returns the command's output pipes : runs

Left side of pipe is the subshell?

一笑奈何 提交于 2019-12-28 02:53:07
问题 Edit: My comment below regarding sed 's@^@ @' <(f1) is incorrect While $BASH_SUBSHELL indicates that we are in the same level as the launch, the variables are lost in the main script. based on Gordons answer I tested f1 > >(sed 's@^@ @') instead and that seems to work correctly. Still, shouldn't BASH_SUBSHELL should be 1 and not 0 for the first form? Consider this small test #!/bin/bash declare -i i=0 function f1() { let i++ echo "In f1, SUBSHELL: $BASH_SUBSHELL, i=$i" >&2 } f1 f1 | sed 's@^@