Sponsored Content
Operating Systems Linux Red Hat Fork wait in background process - large delay Post 302328038 by vishnu.priya on Tuesday 23rd of June 2009 08:22:30 AM
Old 06-23-2009
Fork wait in background process - large delay

hi all,

We are trying to run a process in the background and in the process we call fork ;and wait for the child process to finish .We find that the died = wait(&status); happens after 10 seconds randomly and sometimes completes in time (within 1 sec)

This behavior is seen only when the program is run as a background process. With foreground the functionality is always within 1 sec.

Can anyone please suggest the reason for this random scheduling in Linux - Operating system is CentOS
Code:
    switch ( pid = fork())
    {
        case -1:
            DbgMsg( DBG_ERR, "Cannot fork for compile!\n");
            single--;
            return -1;
        case 0:
            execvp( file, argv);
            DbgMsg(DBG_ERR | DBG_COMPILER, "Should never happen\n");
        default:
            {
waitloop:
            died = wait(&status);
            if (died == pid)
                break;
            if (died == -1) {
#if 0 /* on systems that don't map errno to __error() */
                extern int errno;
#endif

                DbgMsg(DBG_WARNING | DBG_COMPILER, "Compilation error (%s)\n", strerror(errno));
                switch (errno) {
                    case ECHILD:
                    case EINTR:
                        break;
                    default:
                        DbgMsg( DBG_WARNING, "Unexpected compilation error (%d)\n", errno);
                        goto waitloop;
                }
            }
            DbgMsg( DBG_WARNING, "Wait returned for other child %x\n", died);
            goto waitloop;
            }
    }

Thanks in advance for any help
Thanks
Vishnu

Last edited by jim mcnamara; 06-23-2009 at 04:22 PM.. Reason: add code tags
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Wait for Background Process to complete

I am attempting within a for-loop, to have my shell script (Solaris v8 ksh) wait until a copy file command to complete before continueing. The specific code is: for files in $(<inputfile.lst) do mv directory/$files directory/$files ksh -m -i bg %% wait $! done I am shaky on the... (3 Replies)
Discussion started by: gozer13
3 Replies

2. Shell Programming and Scripting

wait command - cat it wait for not-chile process?

Did not use 'wait' yet. How I understand by now the wait works only for child processes, started background. Is there any other way to watch completion of any, not related process (at least, a process, owned by the same user?) I need to start a background process, witch will be waiting... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. UNIX for Advanced & Expert Users

how to make a parent wait on a child shells running in background?

Hi I have a shell script A which calls another 10 shell scripts which run in background. How do i make the parent script wait for the child scripts complete, or in other words, i must be able to do a grep of parent script to find out if the child scripts are still running. My Code: ... (1 Reply)
Discussion started by: albertashish
1 Replies

4. Shell Programming and Scripting

wait in background

can a wait command be run in background? or the script which has the wait command, be run background? test.sh ------- nohup a.sh & nohup b.sh & wait nohup test.sh & How can i run either wait or test.sh in background? i want test.sh to wait till a.sh and b.sh complete, and must be... (1 Reply)
Discussion started by: albertashish
1 Replies

5. UNIX for Dummies Questions & Answers

How can i use fork,sleep,wait and write in a process with father and son..??

Hi.. I was unable to do (gcc code) which refers to the fork,wait,sleep and write.. what i want to do: A process of father create (fork) a son and will sleep 90 seconds..After this, son process create a grandchild and will sleep 60 seconds..Grandchild process will sleep for 30 seconds..After... (3 Replies)
Discussion started by: gumlucin
3 Replies

6. Red Hat

Fork wait in background process - large delay

hi all, We are trying to run a process in the background and in the process we call fork ;and wait for the child process to finish .We find that the died = wait(&status); happens after 10 seconds randomly and sometimes completes in time (within 1 sec) This behavior is seen only when the... (0 Replies)
Discussion started by: vishnu.priya
0 Replies

7. Shell Programming and Scripting

How to make the parent process to wait for the child process

Hi All, I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program. I want 1st script to wait until the 'C' program completes. I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies

8. Programming

Newbie question on exec,fork, wait,pipe C

Hello everybody.I want to make clear that i am not going to ask from anybody to build my asignement but i have a big problem. I can't seem to find anywhere ONE good example on C about what i am trying to do:wall:.I think it is simple. All i ask is one example, even a link is fine. So, i want to... (1 Reply)
Discussion started by: Cuervo
1 Replies

9. Shell Programming and Scripting

calling a shell script in background and wait using "wait" in while loop

Hi, I am facing a strange issue, when i call a script from my while loop in background it doesnt go in background, despite the wait i put below the whil loop it goes forward even before the process put in background is completed. cat abc.txt | while read -u4 line do #if line contains #... (2 Replies)
Discussion started by: mihirvora16
2 Replies

10. Programming

Linux fork, execv, wait question

Hi All, I have a program for class that needs to do the following: 1. Print the directory entries from the current directory using ncurses 2. Provide a prompt next to each directory entry and allow the user to enter commands that may or may not be about the file 3. Execute those commands in... (1 Reply)
Discussion started by: afulldevnull
1 Replies
wait3(3C)						   Standard C Library Functions 						 wait3(3C)

NAME
wait3, wait4 - wait for process to terminate or stop SYNOPSIS
#include <sys/wait.h> #include <sys/time.h> #include <sys/resource.h> pid_t wait3(int *statusp, int options, struct rusage *rusage); pid_t wait4(pid_t pid, int *statusp, int options, struct rusage *rusage); DESCRIPTION
The wait3() function delays its caller until a signal is received or one of its child processes terminates or stops due to tracing. If any child process has died or stopped due to tracing and this has not already been reported, return is immediate, returning the process ID and status of one of those children. If that child process has died, it is discarded. If there are no children, -1 is returned immediately. If there are only running or stopped but reported children, the calling process is blocked. If statusp is not a null pointer, then on return from a successful wait3() call, the status of the child process is stored in the integer pointed to by statusp. *statusp indicates the cause of termination and other information about the terminated process in the following man- ner: o If the low-order 8 bits of *statusp are equal to 0177, the child process has stopped; the 8 bits higher up from the low-order 8 bits of *statusp contain the number of the signal that caused the process to stop. See signal.h(3HEAD). o If the low-order 8 bits of *statusp are non-zero and are not equal to 0177, the child process terminated due to a signal; the low- order 7 bits of *statusp contain the number of the signal that terminated the process. In addition, if the low-order seventh bit of *statusp (that is, bit 0200) is set, a ``core image'' of the process was produced; see signal.h(3HEAD). o Otherwise, the child process terminated due to an exit() call; the 8 bits higher up from the low-order 8 bits of *statusp contain the low-order 8 bits of the argument that the child process passed to exit(); see exit(2). The options argument is constructed from the bitwise inclusive OR of zero or more of the following flags, defined in <sys/wait.h>: WNOHANG Execution of the calling process is not suspended if status is not immediately available for any child process. WUNTRACED The status of any child processes that are stopped, and whose status has not yet been reported since they stopped, are also reported to the requesting process. If rusage is not a null pointer, a summary of the resources used by the terminated process and all its children is returned. Only the user time used and the system time used are currently available. They are returned in the ru_utime and ru_stime, members of the rusage struc- ture, respectively. When the WNOHANG option is specified and no processes have status to report, wait3() returns 0. The WNOHANG and WUNTRACED options may be combined by the bitwise OR operation of the two values. The wait4() function is an extended interface. With a pid argument of 0, it is equivalent to wait3(). If pid has a nonzero value, then wait4() returns status only for the indicated process ID, but not for any other child processes. The status can be evaluated using the macros defined by wait.h(3HEAD). RETURN VALUES
If wait3() or wait4() returns due to a stopped or terminated child process, the process ID of the child is returned to the calling process. Otherwise, -1 is returned and errno is set to indicate the error. If wait3() or wait4() return due to the delivery of a signal to the calling process, -1 is returned and errno is set to EINTR. If WNOHANG was set in options, it has at least one child process specified by pid for which status is not available, and status is not available for any process specified by pid, 0 is returned. Otherwise, -1 is returned and errno is set to indicate the error. The wait3() and wait4() functions return 0 if WNOHANG is specified and there are no stopped or exited children, and return the process ID of the child process if they return due to a stopped or terminated child process. Otherwise, they return -1 and set errno to indicate the error. ERRORS
The wait3() and wait4() functions will fail and return immediately if: ECHILD The calling process has no existing unwaited-for child processes. EFAULT The statusp or rusage arguments point to an illegal address. EINTR The function was interrupted by a signal. The value of the location pointed to by statusp is undefined. EINVAL The value of options is not valid. The wait4() function may fail if: ECHILD The process specified by pid does not exist or is not a child of the calling process. The wait3()and wait4() functions will terminate prematurely, return -1, and set errno to EINTR upon the arrival of a signal whose SA_RESTART bit in its flags field is not set (see sigaction(2)). ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |MT-Level |Async-Signal-Safe | +-----------------------------+-----------------------------+ SEE ALSO
kill(1), exit(2), waitid(2), waitpid(3C), getrusage(3C), signal(3C), signal.h(3HEAD), wait(3C), wait.h(3HEAD), proc(4) NOTES
If a parent process terminates without waiting on its children, the initialization process (process ID = 1) inherits the children. The wait3() and wait4() functions are automatically restarted when a process receives a signal while awaiting termination of a child process, unless the SA_RESTART bit is not set in the flags for that signal. SunOS 5.10 3 Mar 1995 wait3(3C)
All times are GMT -4. The time now is 10:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy