Help Me

P

Piripiccio

Hello , please give me a little minute for this problem

the first thing ..... my english is very bad

I wrote a little program that using a socket pair with 2 process Padre
(Father) and Figlio (Son) but the
select see the future .

This is the code ... please help me before i become crazy .....

#include "my_posix.h"

int main(){

int ris,pid,sp[2],res=0;
ris=socketpair(AF_UNIX,SOCK_STREAM,0,sp);
if(ris){ printf("SP %d\n",ris); exit(2); }
pid=fork();
if(!pid) /*PADRE*/
{
res=8;
ris=write(sp[0],&res,sizeof res);
printf("Padre _scritto:%d in %dB\n",res,ris); fflush(stdout);
printf("Padre x Shut\n"); sleep(2); fflush(stdout);
shutdown(sp[0],SHUT_WR);
printf("Padre fatta Shut\n"); fflush(stdout); res=0;
ris=read(sp[0],&res, sizeof res);
printf("Padre letto %d in %dB\n",res,ris); fflush(stdout);
res=0;
ris=read(sp[0],&res, sizeof res);
printf("Padre letto %d in %dB\n",res,ris); fflush(stdout);
close(sp[0]);
printf("Padre fatta Close\n"); fflush(stdout);
}
else
{
fd_set fds;
FD_SET(sp[1],&fds);
res=select(100,&fds,0,0,0);
printf("Figlio fatta sel %d is set
%d\n",res,FD_ISSET(sp[1],&fds)); fflush(stdout);
ris=read(sp[1],&res, sizeof res);
printf("Figlio letto %d in %dB\n",res,ris); fflush(stdout);
res=select(100,&fds,0,0,0);
printf("Figlio fatta sel %d is set
%d\n",res,FD_ISSET(sp[1],&fds)); fflush(stdout);
res=0; ris=read(sp[1],&res, sizeof res);
printf("Figlio letto %d in %dB\n",res,ris); fflush(stdout);
res=5; sleep(1);
ris=write(sp[1],&res,sizeof res);
printf("Figlio x Shut _scritto:%dB\n",ris); fflush(stdout);
shutdown(sp[1],SHUT_WR);
printf("Figlio fatta Shut\n"); fflush(stdout);
close(sp[1]);
printf("Figlio fatta Close\n"); fflush(stdout);

}
printf("Uc: %u Sc: %d\n",(unsigned char)-5,(char)-130);
exit(-5);
}

The first select in the son (FIGLIO) returns -1 ...... WHY ? not 1
sleep in the PADRE code to prevent that process FIGLIO doesn't make
select .

THANKS A LOOOOOT
 
V

Vladimir S. Oka

Walter said:
sockets are not part of standard C. I recommend that you repost
to comp.unix.programmer .

Also try to use more readable coding style. Your lack of both horizontal
and vertical spacing makes it almost impossible to follow the code. No
wonder you fear of going crazy... ;-)
 
D

Dave Thompson

Hello , please give me a little minute for this problem

the first thing ..... my english is very bad

I wrote a little program that using a socket pair with 2 process Padre
(Father) and Figlio (Son) but the
select see the future .
The process (fork) and sockets part are as noted offtopic here, but
what I believe is your actual problem is ontopic. By the way, the
usual English terms are 'parent' and 'child'.
This is the code ... please help me before i become crazy .....

#include "my_posix.h"

int main(){

int ris,pid,sp[2],res=0;
ris=socketpair(AF_UNIX,SOCK_STREAM,0,sp);
if(ris){ printf("SP %d\n",ris); exit(2); }
pid=fork();
if(!pid) /*PADRE*/

<OT=POSIX> That's backward: fork() returns zero in the child and
nonzero (pid) in the parent. In this example your processing is
basically peer-to-peer so this doesn't hurt anything, but in other
cases this could be a serious error. said:
{
res=8;
ris=write(sp[0],&res,sizeof res);
printf("Padre _scritto:%d in %dB\n",res,ris); fflush(stdout);
printf("Padre x Shut\n"); sleep(2); fflush(stdout);

By the way, if stdout is to a terminal (more formally, to an
"interactive device") it should automatically be line buffered, so
since (all) your outputs end with \n fflush() isn't needed.

<OT> It is customary to close the fd's duplicated in each process that
it does not need. In this example your 'padre' (actually child) uses
only sp[0] so should close(sp[1]) and conversely 'figlio' sp[0].
However said:
shutdown(sp[0],SHUT_WR);
printf("Padre fatta Shut\n"); fflush(stdout); res=0;
ris=read(sp[0],&res, sizeof res);
printf("Padre letto %d in %dB\n",res,ris); fflush(stdout);
res=0;
ris=read(sp[0],&res, sizeof res);
printf("Padre letto %d in %dB\n",res,ris); fflush(stdout);
close(sp[0]);
printf("Padre fatta Close\n"); fflush(stdout);
}
else
{
fd_set fds;
FD_SET(sp[1],&fds);
res=select(100,&fds,0,0,0);

This is probably your problem. The fd_set fds is an automatic (stack)
variable and is not initialized, so its value is indeterminate;
formally it is Undefined Behavior to access it at all and in practice
you will usually get unpredictable garbage. In particular when you
pass it to select() it will probably contain bits set corresponding to
nonopen fds, which is an error. You need to FD_ZERO(&fds) first.

<OT> I don't recall if FD_SETSIZE is guaranteed to be at least 100; if
not this could be wrong even with the FD_ZERO. The customary way is to
add one to your highest fd value; since you have only one fd that is
just sp[1]+1. That way your code adapts to different systems. said:
printf("Figlio fatta sel %d is set
%d\n",res,FD_ISSET(sp[1],&fds)); fflush(stdout);
ris=read(sp[1],&res, sizeof res);
printf("Figlio letto %d in %dB\n",res,ris); fflush(stdout);
res=select(100,&fds,0,0,0);
printf("Figlio fatta sel %d is set
%d\n",res,FD_ISSET(sp[1],&fds)); fflush(stdout);
res=0; ris=read(sp[1],&res, sizeof res);
printf("Figlio letto %d in %dB\n",res,ris); fflush(stdout);
res=5; sleep(1);
ris=write(sp[1],&res,sizeof res);
printf("Figlio x Shut _scritto:%dB\n",ris); fflush(stdout);
shutdown(sp[1],SHUT_WR);
printf("Figlio fatta Shut\n"); fflush(stdout);
close(sp[1]);
printf("Figlio fatta Close\n"); fflush(stdout);

}
printf("Uc: %u Sc: %d\n",(unsigned char)-5,(char)-130);
exit(-5);
}

The first select in the son (FIGLIO) returns -1 ...... WHY ? not 1
sleep in the PADRE code to prevent that process FIGLIO doesn't make
select .
<OT> As I said, probably garbage in fds. </>

Some (standard) C library calls, and most (nearly all?) POSIX calls,
that return an error indication also set an error code in 'errno'
which you can (standardly) access after #include <errno.h>. Printing
strerror(errno), or just calling perror(str) which does so, will give
you some information that is usually helpful. Caveat: for some C
library routines where this would be helpful like fopen() the standard
doesn't _require_ that errno be set, so it possible that perror() etc.
will give unhelpful ("foo: no error") or even bogus ("foo: not a tty")
information, so don't rely absolutely on this.

<OT> The sleep() is irrelevant: select() with null timeout waits
'forever', which in this case will be until 'padre' sends something;
since its small write is probably buffered it will be the shutdown()
that actually sends the data followed by an EOF indication. </>

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,331
Latest member
ElaneLyttl

Latest Threads

Top