473,324 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Fork + Waitpid

Hi all,
i have made a simple script that use fork, waitpid and sigaction for
SIGCHLD.

#include<sys/types.h>
#include<sys/wait.h>
#include<stdio.h>
#include<errno.h>
#include<unistd.h>
#include<signal.h>

int pid;

void chld_signal(int sig)
{
int status,child_val;

printf("\n SIGHCHILD trapped \n");

if(waitpid(pid,&status,0)<0)
{
fprintf(stderr, "waitpid failed\n");
return;
}

if(WIFEXITED(status))
{
child_val=WEXITSTATUS(status);
printf("child's pid %d exited normally with status
%d\n",pid,child_val);
}
}

int main(int argc, char *argv[])
{
char *prog_argv[4];

int iCount;

struct sigaction chld_action;

/* Initialize the signal structure mask. */

chld_action.sa_handler=chld_signal;
sigemptyset(&chld_action.sa_mask);
chld_action.sa_flags=0;

if(sigaction(SIGCHLD,&chld_action,NULL)<0)
{
fprintf(stderr,"\nSigaction failed\n");
return 1;
}
else
printf("\nSigaction Success\n");
/*
** Build argument list
*/

prog_argv[0] = "/usr/bin/ls";
prog_argv[1] = "-lrt";
prog_argv[2] = "/usr1/sdc/corsis/stuff";
prog_argv[3] = NULL;

/*
** Create a process for the cmd */

if((pid=fork()) < 0)
{
fprintf(stderr,"Fork failed");
exit(errno);
}

if(!pid)
{
/* Child */

printf("\nExecuting command [%s]\n",prog_argv[0]);

printf("\nParent Pid %d\n\n",getppid());

execvp(prog_argv[0], prog_argv);
}
if(pid)
{
/*
* Parent
*/

printf("\nWe're in the parent; let's wait for the child pid [%d] to
finish \n",pid);

sleep(1);
}

return 0;
}

Now the problem, if i delete the 'sleep(1)' i can't see anything about all
the printf into chld_signal function.
If i put the 'sleep(1)' i can see all the printf, the code,status of
waitpid.

WHY ???

Thanks and have a nice week.
Nov 14 '05 #1
4 8704
lasek wrote:
Hi all,
i have made a simple script that use fork, waitpid and sigaction for
SIGCHLD.

[code snipped]

Sorry, as your question has nothing to do with standard C, we can't help
you here. Please try news:comp.unix.programmer, where your question
would be *on* topic!

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #2
In article <e8******************************@localhost.talkab outprogramming.com>,
lasek <cl**************@acrm.it> wrote:
i have made a simple script that use fork, waitpid and sigaction for
SIGCHLD.
That's not a "script", that's a program.

fork() and waitpid() are not specified by the C standards. Nor are
several of the include files you list.
Now the problem, if i delete the 'sleep(1)' i can't see anything about all
the printf into chld_signal function.
If i put the 'sleep(1)' i can see all the printf, the code,status of
waitpid. WHY ???


Try flushing your buffers. But moreso, try reading the
documentation about which functions may legally be called from
a signal handling routine. If you are looking at POSIX.1-1990,
it's in section 3.3.1.3 "Signal Actions".
--
'The short version of what Walter said is "You have asked a question
which has no useful answer, please reconsider the nature of the
problem you wish to solve".' -- Tony Mantler
Nov 14 '05 #3
lasek wrote:
Hi all,
i have made a simple script that use fork, waitpid and sigaction for
SIGCHLD.
You need to post this to a newsgroup for your implementation. None of
'fork', 'waitpid', 'sigaction', or 'SIGCHLD' have any meaning in C
itself. These are features of your implementation. Similarly, the headers #include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h> are not standard C headers, but are implementation-specific.

Your other 3 headers *are* standard ones: #include<stdio.h>
#include<errno.h>
#include<signal.h>
[...]
Now the problem, if i delete the 'sleep(1)' i can't see anything about all
the printf into chld_signal function.

sleep() is not a standard C function. An implementation-specific
newsgroup or mailing list is what you want.
Nov 14 '05 #4
# void chld_signal(int sig)
# {
# int status,child_val;
#
# printf("\n SIGHCHILD trapped \n");
#
# if(waitpid(pid,&status,0)<0)
# {
# fprintf(stderr, "waitpid failed\n");
# return;
# }
#
# if(WIFEXITED(status))
# {
# child_val=WEXITSTATUS(status);
# printf("child's pid %d exited normally with status
# %d\n",pid,child_val);
# }
# }

You should avoid calling non-reentrant functions from a signal handler; stdio
is generally nonreentrant.
# Now the problem, if i delete the 'sleep(1)' i can't see anything about all
# the printf into chld_signal function.

When the program exits, you should expect all of its signal/interrupt handlers
to be terminated at the same time unless you've arranged something with the
operating system to allow persistent handlers. If you don't sleep, the next
statement exits the program, probably before the child exits.

(On unix, you synchronise a parent with a child exit with the wait* family
functions (see also `man waitpid`). The SIGCHLD handler doesn't do the
synchronise; it merely lets you know a child is awaiting synchronisation.)

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Raining down sulphur is like an endurance trial, man. Genocide is the
most exhausting activity one can engage in. Next to soccer.
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Josh Denny | last post by:
I am trying to write a simple file transfer server in perl that will reside of both windows and linux platforms. Basically, it accepts a connection, forks a process, and then should close the...
1
by: Yin | last post by:
I am writing a script that monitors a child process. If the child process dies on its own, then the parent continues on. If the child process is still alive after a timeout period, the parent...
1
by: Alexander N. Spitzer | last post by:
I am trying to write a program that will fork a process, and execute the given task... the catch is that if it runs too long, I want to clean it up. this seemed pretty straight forward with a...
27
by: steve | last post by:
I was given the following code, and asked what the possible outputs could be. We're learning about processes and forking. int value; int main(){ int pid, number = 1; value = 2; pid = fork();...
2
by: Rafael Giannetti Viotti | last post by:
Hi, I am working with the subprocess.py module in Python 2.4.4 and I am confused about it's functionality. It uses the standard pipe-fork-exec method to start a subprocess: # create pipes ...
2
by: ganeshp | last post by:
Hi , Below given is my code: This generates the below given error. Can any one help me out on this error please ? Actually i have a huge project written using C++ language on linux , I want...
1
by: Falcolas | last post by:
Hi all, This may be more of a Linux question, but it relates to how Python forks... Today, I implemented a pretty simple listener script using os.fork. The script runs fine, and performs as...
9
by: Gilles Ganault | last post by:
Hello I need to launch a Python script, and fork it so that the calling script can resume with the next step will the Python script keeps running. I tried those two, but they don't work, as...
3
by: RedWiz | last post by:
Hi i have to develop a multihreaded php application on linux, then through pcntl_fork and wait. I tried it, but there something going wrong, i think. The difference whit other languages like c...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.