Home » Infrastructure » Unix » Fork() function in 'C'
Fork() function in 'C' [message #97659] Tue, 22 October 2002 03:58 Go to next message
mani
Messages: 105
Registered: September 1999
Senior Member
Hi,
I want to know about the fork() function in C.Can anyone help me reg this???

Thanks & Regards
Victoria
Re: Fork() function in 'C' [message #97661 is a reply to message #97659] Wed, 23 October 2002 02:12 Go to previous messageGo to next message
B
Messages: 327
Registered: August 1999
Senior Member
Hi forc() duplicate Ur current code ...
After a fork() U should control the PID to know if the child or the parent is currently running..
Re: Fork() function in 'C' [message #97736 is a reply to message #97659] Tue, 10 December 2002 03:37 Go to previous messageGo to next message
Fork You
Messages: 1
Registered: December 2002
Junior Member
what don't you understand? Fork creates a new process with a new id...inturn it creates a child process that is a copy of the parent process...when the child process terminates the parent process resumes and then terminates.
Re: Fork() function in 'C' [message #97782 is a reply to message #97659] Wed, 22 January 2003 05:35 Go to previous messageGo to next message
junjaewoo
Messages: 1
Registered: January 2003
Junior Member
http://www.junjaewoo.com

#define MAX_PROCESS 5;

static int current_process_num=0;
void install_sig_handler();
void sig_handler(int sig);

int main() {
int seconds;
int i;
install_sig_handler();
printf("pid: %d--enter seconds ", getpid());
fflush(stdout);
for(;;) {
scanf("%d", &seconds);
if (current_process_num < MAX_PROCESS){
current_process_num++;
switch (fork()){

case -1:
fprintf(fp,"ERROR! Can not spawn child processn");
exit(1);
case 0:
for (i=0;i<seconds;i++){
sleep (1);
}
_exit(0);
default:
break;
}
}
else {//has reach the limit
printf("has reached the limit, please try againn");
fflush(stdout);
}
}
return 0;
}

void install_sig_handler(){
struct sigaction sig_struct;
sig_struct.sa_handler=sig_handler;
sigemptyset(&sig_struct.sa_mask);
sig_struct.sa_flags=0;
if(sigaction(SIGCHLD, &sig_struct, NULL)!=0){
perror("error");
exit(1);
}
}

void sig_handler(int sig){
pid_t pid;
int status;
while ((pid=waitpid(-1, &status, WNOHANG))>0){
current_process_num--;
// printf("one child has exited, and the current process num is %dn",current_process_num);
}
}
Re: Fork() function in 'C' [message #98384 is a reply to message #97782] Sun, 05 September 2004 06:18 Go to previous messageGo to next message
ARIJIT GOSWAMI
Messages: 1
Registered: September 2004
Junior Member
I want to know about the defination of the fork function, am a grad engineer with specilisation in I.T, thanks for your help.
Have a good time.
Arijit Goswami
Re: Fork() function in 'C' [message #98389 is a reply to message #98384] Mon, 06 September 2004 21:08 Go to previous messageGo to next message
Jai Vrat Singh
Messages: 205
Registered: September 2002
Location: Singapore
Senior Member
I suppose you are using unix so you can get description for any C function on manual pages of unix.

give command
$ man fork


for example in my sytem it shows
System Calls                                              fork(2)

NAME
     fork, fork1 - create a new process

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

     pid_t fork(void);

     pid_t fork1(void);

DESCRIPTION
     The fork() and fork1() functions create a new  process.  The
     new  process (child process) is an exact copy of the calling
     process (parent process). The  child  process  inherits  the
     following attributes from the parent process:

        o  real user ID, real group ID, effective user ID, effec-
           tive group ID

        o  environment

        o  open file descriptors

        o  close-on-exec flags (see exec(2))

        o  signal handling settings (that is,  SIG_DFL,  SIG_IGN,
           SIG_HOLD, function address)

        o  supplementary group IDs

        o  set-user-ID mode bit

        o  set-group-ID mode bit

        o  profiling on/off status

        o  nice value (see  nice(2))

        o  scheduler class (see priocntl(2))

        o  all attached shared memory segments (see shmop(2))

Re: Fork() function in 'C' - Addition [message #98390 is a reply to message #98384] Mon, 06 September 2004 21:14 Go to previous messageGo to next message
Jai Vrat Singh
Messages: 205
Registered: September 2002
Location: Singapore
Senior Member
It is a system call ( a standard C library) so defnition is not available. For more details if you see this output from
$ man fork 


you can see
#include <unistd.h>

you can see the header for more details

vi /usr/include/unistd.h 
Re: Fork() function in 'C' [message #98589 is a reply to message #97659] Fri, 21 January 2005 20:32 Go to previous message
Seetha
Messages: 7
Registered: November 2004
Junior Member
Dear sir,
I want know about the fork() func and their operations
so,kindly explain the func and give one example for that
func.

Thank you

yours faithfuly
seetha.B

[Updated on: Fri, 18 February 2005 23:32]

Report message to a moderator

Previous Topic: Question on space usage >>
Next Topic: output of uniq -c command as pipe delimited
Goto Forum:
  


Current Time: Sat Jan 16 09:09:22 CST 2021