Fork() function in 'C' [message #97659] |
Tue, 22 October 2002 03:58 |
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 |
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 |
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 |
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 #98389 is a reply to message #98384] |
Mon, 06 September 2004 21:08 |
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
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))
|
|
|
|
|