LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-23-2008, 02:17 PM   #1
cleopard
Member
 
Registered: May 2006
Posts: 39

Rep: Reputation: 15
'socketpair' call


I have a pretty basic question about the use of the 'socketpair' call. I'll refer to this code example:


int main(void)
{
int sv[2]; /* the pair of socket descriptors */
char buf; /* for data exchange between processes */

if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) {
perror("socketpair");
exit(1);
}

if (!fork()) { /* child */
read(sv[1], &buf, 1);
printf("child: read '%c'\n", buf);
buf = toupper(buf); /* make it uppercase */
write(sv[1], &buf, 1);
printf("child: sent '%c'\n", buf);

} else { /* parent */
write(sv[0], "b", 1);
printf("parent: sent 'b'\n");
read(sv[0], &buf, 1);
printf("parent: read '%c'\n", buf);
wait(NULL); /* wait for child to die */
}

return 0;
}

The child process is reading and writing to sv[1] and the parent to sv[0]. In general, is that the way one should write the code, or code the parent, for instance, read and/or write to sv[1] as well?
 
Old 04-23-2008, 09:35 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by cleopard View Post
The child process is reading and writing to sv[1] and the parent to sv[0]. In general, is that the way one should write the code
That’s a perfectly reasonable way to write the code.
Quote:
Originally Posted by cleopard View Post
code the parent, for instance, read and/or write to sv[1] as well?
It is possible, but wouldn’t make much sense in most situations (and perhaps could become deadlocked if you don’t be careful) for the parent to read and write from both sockets. But the choice of which socket to use is arbitrary (as the the standard says, “The two sockets shall be identical.”).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do i call a kernel call that i have created in Minix jianelisj Programming 1 04-03-2008 03:11 PM
How can i make centos 4.5's system call using vDSO(call *%gs:0x10) instead of int80 tclwp Red Hat 3 08-06-2007 12:07 AM
How can I script an autologin, automatically call kde(or simillar) and call an app aboaventura Slackware 8 02-03-2007 11:00 PM
What do you call your remote control? I call mine... RHLinuxGUY General 19 10-18-2004 06:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration