473,382 Members | 1,814 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,382 software developers and data experts.

Help Me

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

Mar 1 '06 #1
3 1943
In article <11**********************@e56g2000cwe.googlegroups .com>,
Piripiccio <ab********@libero.it> wrote:
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


sockets are not part of standard C. I recommend that you repost
to comp.unix.programmer .
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Mar 1 '06 #2
Walter Roberson wrote:
In article <11**********************@e56g2000cwe.googlegroups .com>,
Piripiccio <ab********@libero.it> wrote:
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


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... ;-)

--
BR, Vladimir

"Pascal is Pascal is Pascal is dog meat."
-- M. Devine and P. Larson, Computer Science 340

Mar 1 '06 #3
On 1 Mar 2006 07:56:54 -0800, "Piripiccio" <ab********@libero.it>
wrote:
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. </>
{
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, failing to do this is only confusing and risky. </>
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. </>
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
Mar 6 '06 #4

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

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.