Trouble with pipes in chat client on linux


 
Thread Tools Search this Thread
Top Forums Programming Trouble with pipes in chat client on linux
# 8  
Old 10-19-2012
Quote:
Originally Posted by Corona688
In what way are the pipes "not working"? Always be specific.

Pipes buffer. You may not get a line out of the pipe the instant you write it in.
Pipe should flush if you close it after writing to it.

Quote:
Originally Posted by vindy
I thought if childpid was zero you're in the child process.
That is correct, it was a typo from me.

Quote:
Originally Posted by vindy
The problem I'm dealing with right now is that the child process is terminating but the parent is not. I write a q character to the pipe to inform the parent to terminate, but the parent apparently never gets the q on the read end of the pipe. I keeping polling the pipe but it never gets the q character.
Ok looking into this deeper problem seems to be here:

Code:
read(ParentRead, &buf, 1);

Do you see whats wrong with this line?
# 9  
Old 10-19-2012
Quote:
Originally Posted by expl
If parent exits while child is still running kernel will send SIGINT to your child that will cause premature termination of it. You either need to call wait() function on the child or implement a better design.
Which system behaves in this way? To my knowledge, no UNIX-like system sends children a signal when their parent dies. The only thing that happens is that the child is adopted by another process (usually PID 1, usually init).

The only behavior similar to that which you've described is when a child uses Linux's prctl(2) with the PR_SET_PDEATHSIG option. This instructs the kernel to send the chosen signal to that child when its parent dies. However, this is not default behavior; it is non-standard; the signal chosen need not be SIGINT; and the signal value is cleared at every fork(2), so even if a child is set to be notified, its children won't be unless they also call prctl(2).

In this thread's particular case, if the child receives a signal because of its parent's death, it'll likely be a SIGPIPE from the child writing to a broken pipe.

Regards,
Alister
# 10  
Old 10-19-2012
This points to an interesting planning issue: The parent should supervise the children, not the other way around (duh), so the parent can get SIG child if the child goes wrong. I suppose in shell you might even be able to trap that signal.

It is one of the limittions of UNIX piping that something upstream like a sort or find will run on mindless of the loss of processes downstream, until it tries to write something. I even tried writing 0 length messages to the pipe to see if the downstream was still there, but to no avail, or worse, false EOF detection from read() returning zero. So, if it is critical, you need a watchdog or cleanup process. Children can have children, ad infinitum, complicating the process. One way to identify the whole tree is to redirect stderr to a log file, so fuser can tell you who is attached.

Last edited by DGPickett; 10-19-2012 at 05:30 PM..
# 11  
Old 10-19-2012
Quote:
Originally Posted by DGPickett
This points to an interesting planning issue: The parent should supervise the children, not the other way around (duh), so the parent can get SIG child if the child goes wrong. I suppose in shell you might even be able to trap that signal.
You can, but then you get sigchild for every child that quits, including things like mv and cat, not just subshells.
# 12  
Old 10-20-2012
Quote:

Code:
read(ParentRead, &buf, 1);

Do you see whats wrong with this line?
Forgive my stupidity but I don't. I've never used a pipe before so I'm rather ignorant of exactly how interprocess communication works.

---------- Post updated at 11:41 PM ---------- Previous update was at 11:33 PM ----------

I think I see now what you were getting at expl. I was giving the read function the address of an address. Sorry about that. I went into the code, got rid of the ampersand, and rebuilt the program but to no avail. Something is still wrong.

---------- Post updated 10-20-12 at 06:47 PM ---------- Previous update was 10-19-12 at 11:41 PM ----------

Ok, I feel dumb. As it turns out the calls to write to and read from the pipes are working. I forgot that scanf was a blocking function call. Sorry for wasting your time guys, and thank you for all your help.

Last edited by vindy; 10-20-2012 at 01:44 AM.. Reason: typo
# 13  
Old 10-22-2012
Yes, but you can keep track of which PIDs you are tracking and just using wait() (not writing signal catchers), can filter child responses for what pids and what return codes you care about. In bash, you can devote a watcher wait subshell for each background process pid $! saved as you launch them, each retrieving the exit code from that child.

Last edited by DGPickett; 10-22-2012 at 03:33 PM..
# 14  
Old 10-23-2012
Quote:
Originally Posted by vindy
Forgive my stupidity but I don't. I've never used a pipe before so I'm rather ignorant of exactly how interprocess communication works.
It's nothing to do with pipes, it's to do with how arrays work. &buf is overdoing it, it's a pointer already.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Linux chat with other users

Besides talk and write are there any other good programs for talking to a particular user on the same Linux system. (1 Reply)
Discussion started by: cokedude
1 Replies

2. Programming

logic understanding for inter client chat server

hello everyone, i am making chat server in linux using c. i have made programs in which group chat can take place between multiple clients but i am not able to understand how to make 2 particular clients chat with each other. please help!!! (1 Reply)
Discussion started by: sweetbella
1 Replies

3. Programming

please help a problem in client-server ipc message 2 pipes communication simple example

I want to have a message send & receive through 2 half-duplex pipes Flow of data top half pipe stdin--->parent(client) fd1--->pipe1-->child(server) fd1 bottom half pipe child(server) fd2---->pipe2--->parent(client) fd2--->stdout I need to have boundary structed message... (1 Reply)
Discussion started by: ouou
1 Replies

4. Homework & Coursework Questions

Help with server client chat system

request create a chat system in linux where a user type smth and all the other users connected to server get the message.then a user have to create join leave or delete a channel of chat if he created it.i did the server and the client but i dont know how to implemt the chat rooms.i was ... (1 Reply)
Discussion started by: demonmind
1 Replies

5. UNIX for Dummies Questions & Answers

Server-client chat with a bit more

The task is to create a server client chat that contains a few basic safeguards against floods etc and which is capable of issuing at least one or two commands on the client computer. Working samples of such arrangements abound but freeware/shareware samples are not readily available. A catch in... (3 Replies)
Discussion started by: Bobby
3 Replies

6. Programming

How can i make two client to chat with each other?

How can i make two client to chat with each other.. without any work of server, i mean peer to peer i am making chat messenger on which all host will connect to/via server and if any host want private chat then thay work independently... this is code(attached) which i have made for server... (1 Reply)
Discussion started by: bt87
1 Replies

7. UNIX for Dummies Questions & Answers

Server/client chat

I want to make the following programm. Using the server/client model I want 2 client to connect to the server then the server sends back to the clients the ip address and a number of a poort in order to open a udp connection between clients without using the server? What I have done since now is... (2 Replies)
Discussion started by: kasma
2 Replies

8. Programming

client /server pipes

here is the concept: the client reads a pathname from the standard input and writes it to pipe1.The server reads this pathname from the pipe1 and tries to open the file for reading.If the server can open the file ,the server responds by reading the file and writting it to pipe2;otherwise the... (2 Replies)
Discussion started by: tolkki
2 Replies

9. Programming

Chat client-server program

Good day everyone, I'm doing a chat client-server program:server is to receive messages from clients through a TCP port and multicast them back to all clients through a UDP port. This is my client program. I'd not know why it just sends and receives msg from server once, then it stops. Is... (1 Reply)
Discussion started by: powermind
1 Replies

10. Programming

multiuser chat server closes when one client closes. code included

I have been trying to write a very basic chat program but at the moment I am having problems getting it to be multiuser as it closes all connections when one client shutsdown. I have also been having problems trying to get the program to display a list of usernames to the clients. I have tried... (0 Replies)
Discussion started by: dooker
0 Replies
Login or Register to Ask a Question