LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2006, 01:20 AM   #1
DanTaylor
Member
 
Registered: Jan 2006
Distribution: Debian Sarge
Posts: 265

Rep: Reputation: 30
Where to learn to use fork???


Help! I need to learn to use fork, and can't find any good documentation on it. Where can I go? I've googled, and havn't come up with any good documentation.
 
Old 04-01-2006, 03:03 AM   #2
cupubboy
Member
 
Registered: May 2003
Location: Bucharest,Romania
Distribution: Fedora Core 7
Posts: 109

Rep: Reputation: 15
http://www.google.com/search?q=c+fork
 
Old 04-01-2006, 03:55 AM   #3
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
What is fork? Is it a programming language?
 
Old 04-01-2006, 04:20 PM   #4
KimVette
Senior Member
 
Registered: Dec 2004
Location: Lee, NH
Distribution: OpenSUSE, CentOS, RHEL
Posts: 1,794

Rep: Reputation: 46
Quote:
Originally Posted by Gins
What is fork? Is it a programming language?
You're kidding, right? (hard to tell om April 1)

Fork is an alternative to threading. One program can fork off into two programs and use some sort of IPC to handle data exchange so that two tasks can be done concurrently (or on SMP, simultaneously)
 
Old 04-01-2006, 04:44 PM   #5
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Quote:
Originally Posted by KimVette
Fork is an alternative to threading.
fork, together with exec, is the Unix way of executing another program.
 
Old 04-02-2006, 08:04 AM   #6
Kenji Miyamoto
Member
 
Registered: Dec 2004
Distribution: Mandrake 10.1; Fedora Core 3; FreeBSD 5.3; Slackware 10.1 (2.6.10);
Posts: 234

Rep: Reputation: 30
Quote:
Originally Posted by addy86
fork, together with exec, is the Unix way of executing another program.
In addition, output from the program can be taken in via pipe() and dup() (or dup2()).
 
Old 04-02-2006, 08:14 AM   #7
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Don't forget the old trust "man fork".

Fork Man Page.....
Code:
FORK(2)                    Linux Programmer’s Manual                   FORK(2)

NAME
       fork - create a child process

SYNOPSIS
       #include <sys/types.h>
       #include <unistd.h>

       pid_t fork(void);

DESCRIPTION
       fork  creates a child process that differs from the parent process only
       in its PID and PPID, and in the fact that resource utilizations are set
       to 0.  File locks and pending signals are not inherited.

       Under Linux, fork is implemented using copy-on-write pages, so the only
       penalty incurred by fork is the time and memory required  to  duplicate
       the parent’s page tables, and to create a unique task structure for the
       child.

RETURN VALUE
       On success, the PID of the child process is returned  in  the  parent’s
       thread  of execution, and a 0 is returned in the child’s thread of exe-
       cution.  On failure, a -1 will be returned in the parent’s context,  no
       child process will be created, and errno will be set appropriately.

ERRORS
       EAGAIN fork cannot allocate sufficient memory to copy the parent’s page
              tables and allocate a task structure for the child.

       ENOMEM fork failed to allocate the necessary kernel structures  because
              memory is tight.

CONFORMING TO
       The fork call conforms to SVr4, SVID, POSIX, X/OPEN, BSD 4.3.

SEE ALSO
       clone(2), execve(2), vfork(2), wait(2)

Linux 1.2.9                       1995-06-10                           FORK(2)
Typical 30 second example:

Code:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

int main ()
{
    int pid;
    printf ("Forking!\n");
    if ((pid = fork()) == 0) {
        printf ("I am the child process!\n");
    } else {
        printf ("I am the parent process!\n");
        printf ("My child is %d\n",pid);
    }

    printf ("I am process %d\n",getpid());

    return 0;
}
Example output:
Code:
j_shaw@jtshaw:~$ ./fork
Forking!
I am the child process!
I am process 9319
I am the parent process!
My child is 9319
I am process 9318
As you can see, one process says its the child, the other says is the parent, they both print the last printf...

Last edited by jtshaw; 04-02-2006 at 08:19 AM.
 
Old 04-02-2006, 11:18 AM   #8
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
Fork is an alternative to threading.

What is threading? I don't know much about those things. I know about the scripting and some people on this forum taught me a bit about 'sed' and 'awk'.


What is threading?
-------------------------------------------------------

Fork, together with exec, is the Unix way of executing another program.

What is ' exec' ?
I know UNIX fairly well.

----------------------------------

It seems fork is for limited Linux versions. I am running Mandriva 2006 64 bit version.

[nissanka@c83-250-107-194 ~]$ man fork
No manual entry for fork
[nissanka@c83-250-107-194 ~]$

Last edited by Gins; 04-02-2006 at 11:24 AM.
 
Old 04-02-2006, 11:40 AM   #9
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Fork should be in all versions of unix/linux. It is a basic POSIX function. I'm guessing you just don't have the developers man pages installed on your system.

Exec is a system call for executing a file. If you are writing a program that needs to launch another porgram you will typically see something like this:

Code:
if (fork() == 0) {
    execv("/bin/myprogram",NULL);
}
This would exec the program /bin/myprogram with no command line arguements.

A thread can pretty much be thought of as a "light-weight" process that shares the same memory space as the process that creates it (and all other threads that process created). This allows you to easily share information across your threads while having each do its own thing with it.
 
Old 04-03-2006, 12:51 AM   #10
DanTaylor
Member
 
Registered: Jan 2006
Distribution: Debian Sarge
Posts: 265

Original Poster
Rep: Reputation: 30
I'm sorry, I was referring to the fork in perl. I forgot I was referring to a pretty universal function of linux and other programming languages.
 
Old 04-03-2006, 01:00 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Well, there's these, courtesy of google:
http://perldoc.perl.org/functions/fork.html
http://www.xav.com/perl/lib/Pod/perlfork.html
http://vergil.chemistry.gatech.edu/r...l/process.html


Threads enable you to have 'lightweight process' within a parent process. On 2.6 kernel you only see 1 process, on 2.4 you see a process for each thr.
Here's the Perl tutorial:
http://72.14.203.104/search?q=cache:...en&lr=&strip=1
 
Old 04-03-2006, 02:49 AM   #12
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
About Fork and Exec

fork() and exec() are linux system calls... The basic purpose of these calls are to create a new process... Like if we have a program which is executing and we want to create run a child that will run in parallel with this, what we will do ... We will make a fork() call and this system call will make a copy of parent process... Now we have 2 process with same binary, now we call the exec() that will overwrite the parents binary with your requested stuff....
 
Old 04-06-2006, 04:38 PM   #13
elyk1212
Member
 
Registered: Jan 2005
Location: Chandler, AZ USA
Distribution: Mandrake/Mandriva 10.2
Posts: 186

Rep: Reputation: 30
Quote:
It seems fork is for limited Linux versions. I am running Mandriva 2006 64 bit version.

[nissanka@c83-250-107-194 ~]$ man fork
No manual entry for fork
[nissanka@c83-250-107-194 ~]$
To GINs:

First try:
$ info libc

Then chose: 'Processes' from the menu
Then chose: 'Create a Process' from the menu.

This is a great way to see info the C concepts and API of fork. You need the libc manual installed, and info. Fork has been around for a long time, it is just part of the C libraries the concept is repeated in many languages. I am fairly certain that all Linux versions that have c libraries WILL make references to fork in MANY utilities, but you may never notice them if you do not look into the source or program.

Quote:
A thread can pretty much be thought of as a "light-weight" process that shares the same memory space as the process that creates it (and all other threads that process created). This allows you to easily share information across your threads while having each do its own thing with it.
Yep, exactly true. Also, from my understanding, fork is similar except it copies the name space/activation records/memory, as if the child is a separate running program.

Last edited by elyk1212; 04-06-2006 at 04:46 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
about fork() kpachopoulos Programming 2 02-14-2005 12:24 AM
fork beginner_84 Programming 2 08-20-2004 04:53 AM
fork() vibhory2j Linux - Software 1 05-24-2004 04:11 AM
about fork eshwar_ind Programming 5 02-11-2004 03:38 AM
Fork Ztyx Linux - General 1 08-31-2002 11:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration