LinuxQuestions.org
Help answer threads with 0 replies.
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 01-08-2005, 06:34 AM   #1
elmafiacs
LQ Newbie
 
Registered: Apr 2004
Posts: 27

Rep: Reputation: 15
fork() question


Where I can find a good tutorial on the fork() system call, and in process communication? (got them:
http://www-cs.canisius.edu/PL_TUTORI...ED/concurrency
http://www-cs.canisius.edu/PL_TUTORIALS/C/ADVANCED/ipc

Besides that, I want to know how to do this:

- You create a child process to copy a file while doing something different, but you only want to start that process when the user requests to copy a file.

All stuff I've read says something like that I have to start the two process separated and at the beginning of the program, something like:

Code:
int process_id;

main()
{
process_id=fork();

if(process_id==0) {
    break;  /* go on */
else do_other_thing();
}
}
And that's not what I am looking for.


Thanks in advance.

Last edited by elmafiacs; 01-08-2005 at 09:55 AM.
 
Old 01-08-2005, 10:10 AM   #2
Inunu
LQ Newbie
 
Registered: Jan 2005
Location: NZ
Distribution: Customized MDK 9
Posts: 13

Rep: Reputation: 0
The idea of fork() is to clone the original process code/data to a new one. At the end of each "fork()" call, you get two different processes running up to that exact point in their own execution flow. Unless you want to try something like POSIX threads (see man 3 pthread_create()), this is how fork() works.

Suppose your original process is A. After calling, fork() returns the PID of newly cloned process B to your original one (A). It returns 0 to B, and this new process immediately begins to perform something different. That is why it is so-called fork as the execution path of two processes diverses.

Code:
#include <stdio.h>
main(){
  ....
  if(fork()==0){
    /* The newly created process will run into here */
    FILE *src, *dest;
    char buffer[1024];
    int count;

    if((src=fopen("source_file.txt", "r"))==NULL){
      /* Failed to open source file */
     exit(-1);
    }
    if((dest=fopen("destination_file.txt", "r"))==NULL){
      /* Failed to open destination file */
     exit(-2);
    }
    while((count=fread(buffer, 1, 1024, src))>0)
      fwrite(buffer, 1, count, dest);
    fclose(src);
    fclose(dest);
    exit(0);
  }
  else {
    /* Original process will run into here */
    printf("Begining file copy...\n");

    /* Do your thing */
    ....
  }
}
Alternatively you can use POSIX thread. A new thread will start from a function that you specify and terminate when it exits the function. You can use global or static variables for IPC as they share the same process data space.
 
Old 01-08-2005, 10:25 AM   #3
elmafiacs
LQ Newbie
 
Registered: Apr 2004
Posts: 27

Original Poster
Rep: Reputation: 15
Thanks.
 
Old 01-08-2005, 12:42 PM   #4
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
If you want the second process to wait for something to happen, you can use the sigsuspend system call to block the process until it receives the signal. Before you do that, set up a signal handler (using sigaction) for one of the user-reserved signals (SIGUSR1 or SIGUSR2), and then use sigsuspend on that signal. Then have the parent process send the signal to the child (remember, fork returns the child's PID) when the parent is ready for the child to go. When the child receives the signal, it will wake up and then you can have it do what it needs to do.
 
  


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
fork() in c++ deveraux83 Programming 5 11-13-2004 03:12 PM
more on fork() feetyouwell Programming 6 09-17-2004 11:18 AM
fork() vibhory2j Linux - Software 1 05-24-2004 04:11 AM
over-fork luzi82 Linux - Newbie 2 01-02-2004 06:55 AM
fork() lowlifeish Programming 3 11-04-2002 10:50 AM

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

All times are GMT -5. The time now is 07:18 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