Conserving processes: execve() vs fork()


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Conserving processes: execve() vs fork()
# 1  
Old 03-18-2011
Conserving processes: execve() vs fork()

Disclaimer: This is just a rainy day experiment. There is no expected "goal" other than to understand UNIX better.

After reading about fork and exec, my understanding is that forking, as the UNIX shell does by design, consequentially may sacrafice some speed versus an approach that runs in only a single process. (Is this correct?)

Further, my understanding is also that when we execute programs in a pipe, the programs are not executed sequentially, but they are executed simultaneously. (Is that correct?)

For better or worse, I began thinking about the idea of running only a single process at time, and conserving the number of processes that are running simultaneously.

I know that the shell builtin exec can be used to set up file descriptor redirects or to exit the last process in a script. I have used these techniques. But I do not think I truly understand the execve() system call.

Allow me to demonstrate my lack of understanding. (You may want to stop reading here if your intelligence is easily insulted.)

I made a shell function (see bottom of this post) which I named "nf0". "nf" stands for "no fork". "0" means "with zero arguments". It takes a base utility, plugs $1 into the text of execve C function, saves this as a file called "nf.c", then compiles that file to an ELF executable named "nf[utilityname]".

e.g.
Code:
nf0 cat ===> nfcat
nf0 sed ===> nfsed

Then I constructed a pipe:
Code:
trace ./nfcat < big-file | trace ./nfsed | trace ./nfcat

where "trace" is any vendor-specific UNIX trace utility.

I also monitored the output of ps.

As far as I can tell, as a naive UNIX neophyte, I have managed to stop the fork() calls. But have I actually achieved any efficiencies?

In terms of processing, how is this pipeline different from:

Code:
trace cat < big-file| trace sed | trace cat

I doubt I have increased job efficiency here. And I have not tested this properly. But I do want to understand what's happening "behind the scenes" of the shell when I run programs in a pipe.

Sorry for the long post.

Cheers!

Code:
nf0(){
path=$(command -v $1)
printf "
#include <stdio.h>
#include <stdlib.h>
main(int argc, char **argv, char **envp)
{
  char *a[2];
  int i;

  a[0] = \"$1\";
  a[1] = NULL;

  i = execve(\"${path-$1}\", a, envp);
  perror(\"nf$1: execve failed\");
  exit(1);
}
" > nf.c 
cc nf.c -o nf$1 

}


Last edited by uiop44; 03-18-2011 at 09:10 PM..
# 2  
Old 03-20-2011
As an example,think about the pipline
Code:
cat hugefile | head -1 | od -c

Lets ignore the fact that this script is badly written and just look at the result.
If you run the commands sequentially, cat cannot detect that it's stdout has closed and so must process the whole file, even though the head command will exit after it's read the 1st line.

Also when running sequentially, the pipe will need to contain the full contents of the cat output . With parallel processes the pipe is only buffering between the two processes as data is going in one side and out the other at the same time.

Also if system has more that 1 CPU later processes in the chain can make use of otherwise idle resources.

Last edited by Chubler_XL; 03-20-2011 at 10:31 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Making processes using fork

Can anyone help me with this? Create a parent process that gets from the command line n arguments arg1, arg2, ... , argn. The parent will create n/3 son processes, each of them will create a file with the name argi by concatenate the files argi+1 and argi+2. How can i concatenate those... (1 Reply)
Discussion started by: bunicu01
1 Replies

2. Programming

Issue when fork()ing processes

Hi guys! I'll simplify my problem. I have the following code: #include <fcntl.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <signal.h> #include <fcntl.h> #include <unistd.h> #include <sys/wait.h> #define max 25 #define buffdim 50 void p1(); void p2();... (2 Replies)
Discussion started by: pfpietro
2 Replies

3. Programming

Creating more processes with fork()

Hello people I need help How to make ONE process to create MORE (not one) processes with fork(). I tried several codes but do not work. Thanks (8 Replies)
Discussion started by: nekoj
8 Replies

4. Shell Programming and Scripting

fork processes

Hi, How to count how many processes opened by fork function in perl. Thanks (10 Replies)
Discussion started by: Anjan1
10 Replies

5. Programming

fork(), parent and child processes???

Hi friends, I have a small question regarding unix system call fork, I hope you will solve my problem. Here is the small program $ cat fork1.c #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { int pid; int x = 0; x = x + 1; pid = fork(); if(pid < 0) {... (2 Replies)
Discussion started by: gabam
2 Replies

6. SCO

-sh: fork failed - too many processes in sco unix 5.0.5

Dear experts, I have done a re-installation of sco unix openserver 5.0.5 and managed to create users. The problem am facing is that of one user logging in more than 5 times. How can i overcome this problem. the system give the error below. -sh: fork failed - too many processes in sco unix... (5 Replies)
Discussion started by: njoroge
5 Replies

7. Programming

pipe-fork-execve

Hi everyone , after a pipe() system call i've forked and entred into the child process to execve a shell script .the problem here is that when the execve sys call fail , i want to send the error code (eg errno) to the parent process using the pipe writer side p , there is nothing received in the... (4 Replies)
Discussion started by: xtremejames183
4 Replies

8. Shell Programming and Scripting

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n",... (1 Reply)
Discussion started by: green_dot
1 Replies

9. Programming

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n", getpid(),... (0 Replies)
Discussion started by: green_dot
0 Replies
Login or Register to Ask a Question