Monday, June 23, 2008

Using fork in Linux/Unix

Using fork in Linux/Unix :-

A useful way of creating processes is by using fork, but you must take care to keep track of child processes, here is an example of fork written in c.


include int files "sys/types.h", "unistd.h", and "stdio.h".

int main()
{
pid_t id;
char *message;
int n;
printf("fork program startingn");
id = fork();
switch(id) {
case -1:
exit(1);
case 0:
message = "this is the child process";
n = 3;
break;
default:
message = "this is the parent process";
n = 6;
break;
}
for(; n > 0; n-)
{
puts(message);
sleep(1);
}
exit(0);
}


save it as fork1.c and now lets compile this small fork program using the GNU C compiler, now just use the following command: cc -o fork1 fork1.c
Now you can run it : fork &


1 comment:

Anonymous said...

can u give some example of using vfork?