Sponsored Content
Top Forums Programming C function to start process but to return right away Post 302428047 by Corona688 on Tuesday 8th of June 2010 03:53:33 PM
Old 06-08-2010
You're not calling wait() anywhere, so you may be generating lots of zombie processes -- processes that have completed but haven't been waited for.

You may wish to keep a thread around that does nothing but call wait() in a loop to handle them. You could also catch the SIGCHLD signal but this is subject to problems.[COLOR="#738fbf"]

Code:
int main(void)
{
  initialize();
  while (FCGI_Accept() >= 0)  
  {
		  pid_t pid = fork();
		  if(pid < 0)
		  {
		    exit(1);
		  }
		  else if(pid == 0)
		  {
		    //do things
				  continue;
		  }
		  bstring s = bfromcstr("Content-type: text/html\n\n");

    bcatcstr(s, "hellow");
    printf("%s",s->data);
  }
  return EXIT_SUCCESS;
}

You are not making your child processes quit! That continue should be an exit, unless you want growing swarms of copies of your process all waiting on while (FCGI_Accept() >= 0).
This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to start a process and make it sleep for 5 mins and then kill that process

how to start a process and make it sleep for 5 mins and then kill that process (6 Replies)
Discussion started by: shrao
6 Replies

2. Shell Programming and Scripting

return value of a function

Hi I have a doubt in the way the variables inside a function are treated . if a function is called from the main script directly, the variables inside them act as global variables. however if the return value of the function is stored to some other variable in the main script as shown,... (3 Replies)
Discussion started by: prez
3 Replies

3. Shell Programming and Scripting

Script - How to automatically start another process when the previous process ends?

Hi all, I'm doing automation task for my team and I just started to learn unix scripting so please shed some light on how to do this: 1) I have 2 sets of datafiles - datafile A and B. These datafiles must be loaded subsequently and cannot be loaded concurrently. 2) So I loaded datafile A... (10 Replies)
Discussion started by: luna_soleil
10 Replies

4. Shell Programming and Scripting

return value of a function

I have write a shell function to get the maximum of a vector. However, the returned value from the function is not always the correct one. Here is the script: maxval() { local max j i size arrval size=$1 ; shift max=-999999999 i=0 while do arrval="$1" if then ... (5 Replies)
Discussion started by: fl0r10
5 Replies

5. Shell Programming and Scripting

return in function

I am using ksh. I want to know how can we make any function to return string or double value. I dont want to use the global variables. (5 Replies)
Discussion started by: PRKS
5 Replies

6. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

7. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

8. Shell Programming and Scripting

Pass return value of a function in background process

Hi, I have created a function f1 defined in script A.sh .I have called this function in background . But I want to use its return value for another function f2 in script A.sh. I tried declaring it as a global variable, yet it always returns the status as 0. Is there any way with which I can get... (7 Replies)
Discussion started by: ashima jain
7 Replies

9. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

10. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies
WAIT(2) 						     Linux Programmer's Manual							   WAIT(2)

NAME
wait, waitpid - wait for process termination SYNOPSIS
#include <sys/types.h> #include <sys/wait.h> pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options); DESCRIPTION
The wait function suspends execution of the current process until a child has exited, or until a signal is delivered whose action is to terminate the current process or to call a signal handling function. If a child has already exited by the time of the call (a so-called "zombie" process), the function returns immediately. Any system resources used by the child are freed. The waitpid function suspends execution of the current process until a child as specified by the pid argument has exited, or until a signal is delivered whose action is to terminate the current process or to call a signal handling function. If a child as requested by pid has already exited by the time of the call (a so-called "zombie" process), the function returns immediately. Any system resources used by the child are freed. The value of pid can be one of: < -1 which means to wait for any child process whose process group ID is equal to the absolute value of pid. -1 which means to wait for any child process; this is the same behaviour which wait exhibits. 0 which means to wait for any child process whose process group ID is equal to that of the calling process. > 0 which means to wait for the child whose process ID is equal to the value of pid. The value of options is an OR of zero or more of the following constants: WNOHANG which means to return immediately if no child has exited. WUNTRACED which means to also return for children which are stopped, and whose status has not been reported. (For Linux-only options, see below.) If status is not NULL, wait or waitpid store status information in the location pointed to by status. This status can be evaluated with the following macros (these macros take the stat buffer (an int) as an argument -- not a pointer to the buffer!): WIFEXITED(status) is non-zero if the child exited normally. WEXITSTATUS(status) evaluates to the least significant eight bits of the return code of the child which terminated, which may have been set as the argu- ment to a call to exit() or as the argument for a return statement in the main program. This macro can only be evaluated if WIFEX- ITED returned non-zero. WIFSIGNALED(status) returns true if the child process exited because of a signal which was not caught. WTERMSIG(status) returns the number of the signal that caused the child process to terminate. This macro can only be evaluated if WIFSIGNALED returned non-zero. WIFSTOPPED(status) returns true if the child process which caused the return is currently stopped; this is only possible if the call was done using WUNTRACED. WSTOPSIG(status) returns the number of the signal which caused the child to stop. This macro can only be evaluated if WIFSTOPPED returned non-zero. Some versions of Unix (e.g. Linux, Solaris, but not AIX, SunOS) also define a macro WCOREDUMP(status) to test whether the child process dumped core. Only use this enclosed in #ifdef WCOREDUMP ... #endif. RETURN VALUE
The process ID of the child which exited, or zero if WNOHANG was used and no child was available, or -1 on error (in which case errno is set to an appropriate value). ERRORS
ECHILD if the process specified in pid does not exist or is not a child of the calling process. (This can happen for one's own child if the action for SIGCHLD is set to SIG_IGN. See also the LINUX NOTES section about threads.) EINVAL if the options argument was invalid. EINTR if WNOHANG was not set and an unblocked signal or a SIGCHLD was caught. NOTES
The Single Unix Specification describes a flag SA_NOCLDWAIT (not supported under Linux) such that if either this flag is set, or the action for SIGCHLD is set to SIG_IGN then children that exit do not become zombies and a call to wait() or waitpid() will block until all children have exited, and then fail with errno set to ECHILD. The original POSIX standard left the behaviour of setting SIGCHLD to SIG_IGN unspecified. Later standards, including SUSv2 and POSIX 1003.1-2001 specify the behaviour just described as an XSI-compliance option. Linux does not conform to the second of the two points just described: if a wait() or waitpid() call is made while SIGCHLD is being ignored, the call behaves just as though SIGCHLD were not being igored, that is, the call blocks until the next child terminates and then returns the PID and status of that child. LINUX NOTES
In the Linux kernel, a kernel-scheduled thread is not a distinct construct from a process. Instead, a thread is simply a process that is created using the Linux-unique clone(2) system call; other routines such as the portable pthread_create(3) call are implemented using clone(2). Before Linux 2.4, a thread was just a special case of a process, and as a consequence one thread could not wait on the children of another thread, even when the latter belongs to the same thread group. However, POSIX prescribes such functionality, and since Linux 2.4 a thread can, and by default will, wait on children of other threads in the same thread group. The following Linux-specific options are for use with children created using clone(2). __WCLONE Wait for "clone" children only. If omitted then wait for "non-clone" children only. (A "clone" child is one which delivers no sig- nal, or a signal other than SIGCHLD to its parent upon termination.) This option is ignored if __WALL is also specified. __WALL (Since Linux 2.4) Wait for all children, regardless of type ("clone" or "non-clone"). __WNOTHREAD (Since Linux 2.4) Do not wait for children of other threads in the same thread group. This was the default before Linux 2.4. CONFORMING TO
SVr4, POSIX.1 SEE ALSO
clone(2), signal(2), wait4(2), pthread_create(3), signal(7) Linux 2000-07-24 WAIT(2)
All times are GMT -4. The time now is 10:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy