dup

fork, pipe exec and dub2

核能气质少年 提交于 2019-12-13 02:54:20
问题 This code is supposed to print "Output from 'ls -l':" and append the result of 'ls -l', but it doesn't... Does anyone has a clue whats wrong with this? #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> void readStringFromFile (int file, char * readbuffer) { int nbytes = read(file, readbuffer, sizeof(readbuffer)); readbuffer[nbytes] = 0; } int main(int argc, char const *argv[]) { int fd[2]; pipe(fd); if (fork()==0)//child process { close(fd[0]); dup2(fd[1],1);

Duplicate a rails object with associations and paperclip attachments

你。 提交于 2019-12-12 18:07:26
问题 I have an object with several associations. Some of these associated objects have paperclip-attachments stored at S3. If I duplicate the object and the associations it works fine but the attachments are not duplicated. This here works without getting the images: copy_salon = @salon.dup copy_salon.about_us_versions = @salon.about_us_versions.collect{|about_us| about_us.dup} I tried to get the image link like this: copy_salon = @salon.dup copy_salon.about_us_versions = @salon.about_us_versions

pipe & dup functions in UNIX [duplicate]

青春壹個敷衍的年華 提交于 2019-12-11 11:11:41
问题 This question already has answers here : Why should you close a pipe in linux? (2 answers) Closed 3 years ago . I have a suspicious point from the code written below. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(void){ int pid; int i,j; char c; int pfd[2]; if(pipe(pfd) == -1){ perror("pipe"); exit(1); } // pfd[0] : process read from pfd[0] // pfd[1] : process write to pfd[1] pid = fork(); if(pid == -1){ perror("pid error\n"); exit(1); } else if(pid =

Is closing a pipe necessary when followed by execlp()?

牧云@^-^@ 提交于 2019-12-11 05:43:40
问题 Before stating my question, I have read several related questions on stack overflow, such as pipe & dup functions in UNIX, and several others,but didn't clarify my confusion. First, the code, which is an example code from 'Beginning Linux Programming', 4th edition, Chapter 13: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main() { int data_processed; int file_pipes[2]; const char some_data[] = "123"; pid_t fork_result; if (pipe(file_pipes) == 0) { fork

What is best practice in Ruby to avoid misusing assignment “=”?

人盡茶涼 提交于 2019-12-07 18:14:38
问题 I've been bitten a couple of times by forgetting that x = y in Ruby makes x refer to the same object as y; I'm too used to languages where it means, in Ruby terms, x = y.dup . Forgetting this, I inadvertently change y when I think it's safe on the right side of the assignment. I can see that it would make sense to avoid simple x = y assignments without a special reason, but the same thing can be lurking in other places such as name = (person.last_name.blank? ? 'unknown' : person.last_name)

I/O stream redirection in a Linux shell. How does the shell process a command with redirection?

风格不统一 提交于 2019-12-04 03:51:43
问题 Currently I'm coding a small shell (redirection, pipes, exec, etc.). Been trying to figure out the steps the Linux shell takes in addressing I/O redirection. Some questions on what I need help on: In which direction does the shell read from the command line when it is looking for the redirection? Left to right or the opposite? Uses recursion? What are the cases the shell needs to look for? (not sure if there are many or just a couple that can encompass a lot of variation) Anyways, some I can

Duplicating a Ruby array of strings

荒凉一梦 提交于 2019-12-03 19:56:12
问题 arr = ["red","green","yellow"] arr2 = arr.clone arr2[0].replace("blue") puts arr.inspect puts arr2.inspect produces: ["blue", "green", "yellow"] ["blue", "green", "yellow"] Is there anyway to do a deep copy of an array of strings, other than using Marshal as i understand that is a hack. I could do: arr2 = [] arr.each do |e| arr2 << e.clone end but it doesn't seem very elegant, or efficient. Thanks 回答1: Your second solution can be shortened to arr2 = arr.map do |e| e.dup end (unless you

Can someone explain what dup() in C does?

可紊 提交于 2019-12-03 02:45:59
问题 I know that dup, dup2, dup3 " create a copy of the file descriptor oldfd "(from man pages). However I can't digest it. As I know file descriptors are just numbers to keep track of file locations and their direction(input/output). Wouldn't it be easier to just fd=fd2; Whenever we want to duplicate a file descriptor? And something else.. dup() uses the lowest-numbered unused descriptor for the new descriptor. Does that mean that it can also take as value stdin , stdout or stderr if we assume

Tcp Dup ACK--又是数据库的问题

匿名 (未验证) 提交于 2019-12-03 00:05:01
最近,值班的同学反映,有个程序定时会在凌晨4点的时候挂起,重启后恢复,让开发的同事 查了一下,有可能挂起的地方只有在查询数据库,后来gdb也证明了是挂在otl的fetch函数, 这个问题让我想起了 之前的问题 ,也是同个程序,也是在查询数据库,所以就叫同事捉了个 包,一打开,比较奇怪: 04:17:19 向数据库发起数据,然后回了个ACK,再发了个数据,再回个ACK。紧接着就不停地重发那个ACK了。 会发重复ACK(Dup ACK)理论上是收到数据了,再会回ACK,而回重复ACK应该是收到的是乱序的包,有些 数据丢了,或者是服务器不停地重发同一个包。由于捉包的同事只捉了dst端口,所以来的数据包看不到,今晚 再捉一次,希望能看到真相。 Dup ACK的间隔分别是: 3s, 6s, 12s, 24s, 48s, 64s, 64s,64s 64s,64s,64s 2014-10-30 发现这个超时时间竟然跟TCP/IP详解的说明完全对得上,今天才发现数据库主机是AIX,原来unix的实现还是 这样,Linux做了不少改动。 从这两天发的包已经可以确认,是应用向数据发起请求,数据库收到了,回了数据,应用回了ACK,这个回得 到,接着应用再请求数据,这时数据库发了一个大包,应用只收到第一个包,回了ACK,这个ACK丢了,数据 库不停地重发那个包,应用是收到了,但回了ACK数据库一直收不到了

重读APUE(3)-dup与文件表项

喜欢而已 提交于 2019-12-02 21:13:04
下图为调用dup之后的文件指针状态,包含如下信息: 1. dup选择了一个最小的未使用的描述符3; 2. dup(1)之后,描述符1和描述符3指向同一个文件表项; 3. dup(1)的返回值即为复制得到的描述符3; 4. 补充dup2,dup2可以指定复制的目标描述符,如果该描述符与被复制的描述符相同,会直接返回该描述符;如果不同,则会先关闭描述符,然后进行复制;注意,dup2的关闭和复制操作是原子的; 5. 补充open,进程每一次open调用都会打开一个新的文件表项,一个新的描述符指向这个文件表项,当open同一个文件多次也会新建描述符和文件表项;如果重复打开同一个文件,则文件表项中的node节点指向同一个inode; 下图为多次open同一个文件的情况(fd1,fd3);其中fd2=dup(fd1); 图片来自:APUE 来源: https://www.cnblogs.com/wanpengcoder/p/11762720.html