|
Home > Archive > Unix Programming > October 2005 > wait(pid) problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| Hi I have a question. I have written a C-code that will create a
process to open a socket and listen to incoming connections. I am
running the program on Windows XP under "CYGWIN".
my problem is that calling wait() doesn't block the main process it
always return and I can see the child process running and the port is
open for LISTEN mode. I need to block the main process how can I do
that? does this problem have to do with windows OS? if I run the same
program on UNIX will it work as desired?
Thank you.
I have something similar to the following code
void ManageConn()
{
1. this function will create a socket
2. bind it
3. listen
4. then I have the following
while(1)
{
len = sizeof(cliaddr);
if( (connfd = accept(listenfd, (SA *) &cliaddr, &len)) < 0)
err_sys("socket accept error");
cinfo = new struct clientinfo;
memcpy((void *)&(cinfo->cliaddr), (void *) &cliaddr, (size_t)len);
cinfo->connfd = connfd;
pthread_create(&tid, NULL, &Msensor, (void *) cinfo);
}
return;
}
int main()
{
int pid;
if( (pid2 = fork()) == 0)
{
ManageConn();
exit(0);
}
wait(&pid1);
return 0;
}
| |
| Maxim Yegorushkin 2005-10-24, 3:48 pm |
|
Bader wrote:
> Hi I have a question. I have written a C-code that will create a
> process to open a socket and listen to incoming connections. I am
> running the program on Windows XP under "CYGWIN".
> my problem is that calling wait() doesn't block the main process it
> always return and I can see the child process running and the port is
> open for LISTEN mode. I need to block the main process how can I do
> that? does this problem have to do with windows OS? if I run the same
> program on UNIX will it work as desired?
[]
> int main()
> {
> int pid;
>
> if( (pid2 = fork()) == 0)
> {
> ManageConn();
> exit(0);
> }
>
> wait(&pid1);
> return 0;
> }
You don't check fork() and wait() return values in the parent. They may
fail. http://www.opengroup.org/onlinepubs...tions/wait.html
| |
| Barry Margolin 2005-10-24, 3:48 pm |
| In article <1129990618.023981.282450@g49g2000cwa.googlegroups.com>,
"Maxim Yegorushkin" <maxim.yegorushkin@gmail.com> wrote:
> Bader wrote:
>
> You don't check fork() and wait() return values in the parent. They may
> fail. http://www.opengroup.org/onlinepubs...tions/wait.html
But if he sees the child process running, as he said, doesn't that imply
that fork() succeeded?
Maybe Cygwin doesn't conform to POSIX in this respect, so perhaps the OP
should post to a Windows newsgroup, since this shouldn't happen in a
real Unix system.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
| |
| Henry Townsend 2005-10-24, 3:48 pm |
| Barry Margolin wrote:
> Maybe Cygwin doesn't conform to POSIX in this respect, so perhaps the OP
> should post to a Windows newsgroup, since this shouldn't happen in a
> real Unix system.
Or maybe he should try to get it working on an actual Unix system first
and then port it to Cygwin. It may be possible to do this with a
Linux-on-a-CD distribution like Knoppix. I believe there's a
Linux-on-a-flash-drive floating around someplace too, though you might
need a fairly big flash drive to hold the base OS plus gcc.
HT
| |
| Maxim Yegorushkin 2005-10-24, 3:48 pm |
|
Barry Margolin wrote:
[]
> But if he sees the child process running, as he said, doesn't that imply
> that fork() succeeded?
>
> Maybe Cygwin doesn't conform to POSIX in this respect, so perhaps the OP
> should post to a Windows newsgroup, since this shouldn't happen in a
> real Unix system.
I tried sample code with wait() and it worked as expected on FC4 and
Cygwin.
$ uname -a
CYGWIN_NT-5.1 home 1.5.17(0.129/4/2) 2005-05-25 19:38 i686 unknown
unknown Cygwin
$ g++ --version
g++ (GCC) 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
$ cat exp.cpp
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
pid_t pid(fork());
switch(pid)
{
case -1:
perror("fork");
exit(EXIT_FAILURE);
case 0:
printf("child %d started\n", int(getpid()));
sleep(3);
printf("child %d finished\n", int(getpid()));
exit(EXIT_SUCCESS);
}
int status;
while(-1 == wait(&status))
perror("wait");
printf("child %d finished with status %d\n", int(pid), status);
}
$ make
g++ -I../ -fmessage-length=0 -Wall -O3 -fomit-frame-pointer -c exp.cpp
-o exp.o
g++ -o exp exp.o
$ ./exp
child 3244 started
child 3244 finished
child 3244 finished with status 0
|
|
|
|
|