fork

C programming - handling stdout and stdin using pipes

浪子不回头ぞ 提交于 2019-12-11 02:47:12
问题 I am writting a C program formed by a parent and his child (using fork). They comunicate through a pipe. Parent writes into the pipe through the standard output and child reads from the pipe through the standard input. Once they are connected, parent writes "hello world" into the pipe, and son calls exec. My code looks like this: int main(int argc, char *argv[]) { int p, a; char buf[1024]; FILE *file; size_t nread; int fd[2]; char argument[PATH_MAX]; if(pipe(fd)<0){ return 1; } p = fork();

getppid() not returning parent's pid [duplicate]

扶醉桌前 提交于 2019-12-11 02:43:52
问题 This question already has answers here : Why do processes I fork get systemd as their parent? (3 answers) Closed 3 years ago . I have been trying to learn about fork and processes. I just encountered a small problem with this piece of code and was trying to understand why?. I was trying to duplicate a process by a system call Fork and with the value of pid being positive, it hit the parent and its getpid() was returned. And simultaneously it hit the child and its getpid() was returned. But

Interpreting STRACE output - pipes and forks

主宰稳场 提交于 2019-12-11 02:40:08
问题 I have the following code written in C, taken from https://beej.us/guide/bgipc/html/multi/pipes.html: #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(void) { int pfds[2]; pipe(pfds); if (!fork()) { close(1); /* close normal stdout */ dup(pfds[1]); /* make stdout same as pfds[1] */ close(pfds[0]); /* we don't need this */ execlp("/bin/ls", "ls", NULL); } else { close(0); /* close normal stdin */ dup(pfds[0]); /* make stdin same as pfds[0] */ close(pfds[1]); /* we don't need

fork并发服务器

核能气质少年 提交于 2019-12-11 02:31:36
并发服务器需要使用fork进行实现,其是unix中派生新进程的唯一方法。 fork函数定义 #include <unistd.h> pid_t fork(void); 调用fork一次,返回两次: 在调用进程(父进程)中返回一次,返回值为子进程ID;(父进程有多个子进程,通过返回来获取跟踪。) 在子进程中又返回一次,返回值为0;(子进程可以通过getppid获取父进程的ID) 通过返回值告知进程本身当前是子进程还是父进程。 并发服务器: 并发服务器使用fork子进程来服务每个客户。 典型使用: listenfd = socket(...); bind(listenfd , ...); listen(listenfd , MaxClient); for(;;) { connfd = accept(listenfd, ...);//侦听套接字转化为连接套接字,父进程继续等待。 if ( pid = fork() == 0)//子进程,父子共享的引用计数值变为2 { close(listenfd);//子进程关闭侦听套接字 do(connfd); //处理子进程连接套接字 close(connfd); //子进程关闭连接套接字 (仅仅把引用计数减1,并不是真的关闭套接字) exit(0); } close(connfd);//父进程关闭连接套接字(引用计数再次减1,若为0

unable to spawn git no such file or directory on a specific fork on github

南笙酒味 提交于 2019-12-11 02:09:01
问题 The problem is with this fork/repo : https://github.com/RubenWillems/CCNet I can get other forks on the same laptop, but not this one. trying with ssh gives the same problem. I'm suspecting a security setup issue with the fork, but I have no idea. Any idea on how to (start) tackling this, I am quite new to git. 回答1: This http://devnet.jetbrains.net/message/5321236#5321236 workaround from JetBrains works for me perfectly. Here are commands when JetBrains forum thread would not available

Can a child process wait for it's sibling process, and get its exit status?

半世苍凉 提交于 2019-12-11 01:37:45
问题 I'm trying to get from a child process the exit status of its 'sibling', I've tried to do that: int main(void){ int i,j,status,p1; pid_t pids[2],pid; for (i = 0; i < 2; i++){ pids[i]=fork(); /*child 1*/ if(pids[i]==0 && i==0)exit(5); /*child 2*/ else if(pids[i]==0 && i==1){ waitpid(pids[0],&p1,0); exit(WEXITSTATUS(p1)); } } for(j=0;j<2;j++ ){ pid = wait(&status); if(pid==pids[0]){ printf("child process 0 exit with status %d\n",WEXITSTATUS(status)); } else if(pid==pids[1]){ printf("child

Random number between forked processes is the same

故事扮演 提交于 2019-12-11 01:35:52
问题 I am forking multiple processes from a manager process. I then would like to make a random port number for these forked processes to listen on. However, when I seed random, and get a random number, I get the same number between the three processes. For example: manager: int main(){ for(int i = 0; i < rCount; i++){ pid_t pid = fork(); if (pid == 0) {// child execl(ROUTERLOCATION,"",NULL); //create router process } else { // parent } } } router: int main(){ randomPort(); } void randomPort(){

Control the length of Fork Super queue

旧巷老猫 提交于 2019-12-11 01:33:50
问题 Continuing from my last question Access element across multiple hash of hash of arrays I have this bit of code, use Forks::Super; foreach my $special_type (keys %test_variables) { my $last_job = undef; foreach my $test_module (keys %{$test_variables{$special_type}}) { foreach my $set_of_tests ($test_variables{$special_type}{$test_module}) { foreach my $test (@$set_of_tests){ print "Starting $test\n"; my $job = fork { name => "$special_type/$test_module/$test", cmd => "nosetests -m $special

SIGSTOP/SIGCONT POSIX behavior

女生的网名这么多〃 提交于 2019-12-11 00:28:01
问题 I'm playing around with signals: SIGSTOP and SIGCONT in particular. Here is a test program I wrote. The idea is to create a chain of N + 1 processes (including the main process). Each one has to wait for its child to stop, then stop itself. The main process has to wake up its child when the latter has stopped. To do so, the f function recursively create the process chain. Each of the process uses sigsuspend on the SIGCHLD signal apart from the last child who stops itself directly. When its

call a function many times in parallel

 ̄綄美尐妖づ 提交于 2019-12-11 00:14:03
问题 I want to call a function with different value of arg many times. To make the execution faster I want to call the function without considering its implementation. Inside a loop i pass the arg to the function and call it now my program should not wait for the function to execute and loop again, instead it should not consider that the function has not completed for the first loop it should go ahead and call the function again and again till the loop ends. how to use fork and threads to do this