Message passing from child to parent using pipes


 
Thread Tools Search this Thread
Special Forums IP Networking Message passing from child to parent using pipes
# 1  
Old 09-28-2013
Message passing from child to parent using pipes

Hi,

I am trying my hand in networking programming in C, and got stuck in piping.

I was following some tutorial and did the forking like :

Code:
while (1) 
    {
        newsockfd = accept(sockfd, 
                (struct sockaddr *) &cli_addr, &clilen);
        if (newsockfd < 0)
        {
            perror("ERROR on accept");
            exit(1);
        }
        /* Create child process */
        pid = fork();
        if (pid < 0)
        {
            perror("ERROR on fork");
	    exit(1);
        }
        if (pid == 0)  
        {
            /* This is the client process */
            close(sockfd);
            doprocessing(newsockfd);
            exit(0);
        }
        else
        {
            close(newsockfd);
        }
    } /* end of while */

I want to have:

The client forks off a child. After the child is forked off, the parent process enters a second loop in which it continually reads and prints out status messages received from the child via a half-duplex pipe.
How to do the thing in bold?

Apologies , if it is a lame question..but how do we do that or some reference of the same would help me..


Regards

Abhinav
# 2  
Old 09-28-2013
Presumably your process' environment includes definitions for all of the variables and non-standard functions you're using in this while loop. Without seeing some of that, we don't have enough context to know what this code would do. But, there is absolutely nothing in this loop that would create a half duplex pipe between two processes.

If I'm reading your code correctly, however, one thing this loop will do is create a large number of processes that will bring your system to a crawl, hit a limit on the number of processes allowed per userID, or both.

Without knowing what the doprocessing() function does, there is no way we can evaluate what this loop will do. I would guess that it is either writing data to the parent that the parent does not read, or it is reading data from its parent that the parent never writes.

Check your local man pages for pipe and for accept.

Consider why you think you want to use while(1) to fork a bunch of children. From the description of your problem it sounds like you only need one parent and one child.
# 3  
Old 09-28-2013
Thanks for the reply..sorry if the code is misleading but I was following an online tutorial to learn network programming.

All i want to do is to keep client in a loop until the user types "quit" so I will change while to do-while.
The client enters an infinite loop in which it queries the user which service is being requested. There are two options : echo and time.

The client forks off a child. After the child is forked off, the parent process enters a second loop in which it continually reads and prints out status messages received from the child via a half-duplex pipe.

I hope it clears some part of what I want to implement.

So i thought of :
do
{
if user enters "echo"
then
it will fork off a child, doEcho() but what to do after that..I am not getting as
per the requirement like parents goes in infinte loop and all..
else if user enters "daytime"
then
it will fork off a child , doDayTime() and again the same.what to do/.

}while (user enters exit)

I guess I am following the right track???


Please guide me.

Thanks and Regards

Abhinav

Last edited by abhi1988sri; 09-28-2013 at 05:56 AM..
# 4  
Old 09-28-2013
Your statement of requirements and your code snippets do not seem to be related to each other at all. You talk about reading data from a user, but there is absolutely nothing in your while loop that reads any data. You talk about the functions doEcho() and doDayTime(), but there are no definitions for these functions and no calls to these functions. You talk about network programming and you talk about half-duplex pipes, but there are no pipes and you aren't showing us how your socket is set up. You have a doprocessing() function that each child process calls, but, again, there is no declaration for this function.

With all of the missing declarations, missing functions, missing context, I'm not going to try to recreate and debug your program. By visual inspection, it still looks to me like the parent will fork() as many children as it can until the system is saturated or the system starts failing fork()s due to user process count limits.

With all of this confusion and missing details, I have no idea how to help you. The only thing I can suggest is to go back to your online tutorial and seek help from the people who maintain that tutorial; maybe they will have the needed context to understand what you're trying to do.
# 5  
Old 09-29-2013
Sorry again for not communicating properly.I did try to code for the same and read about half duplex pipe and forking.

The problem I am facing is whenever I am closing my client window, server window is also getting closed.

Also, for message passing from client child to client and then to server , I have used pipe but not able to achieve that. Whenever I am writing anything in the client window, I am getting some encoded characters on the client screen and nothing on the server screen.May be because I am not sending anything to the server using send(). Below is my code for client and server.

server.c

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 3490
#define BACKLOG 10

int main()
{
    struct sockaddr_in server;
    struct sockaddr_in dest;
    int status,socket_fd, client_fd,num;
    socklen_t size;

    char buffer[10241];
    char *buff;
//  memset(buffer,0,sizeof(buffer));
    int yes =1;



    if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0))== -1) {
        fprintf(stderr, "Socket failure!!\n");
        exit(1);
    }

    if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
        perror("setsockopt");
        exit(1);
    }
    memset(&server, 0, sizeof(server));
    memset(&dest,0,sizeof(dest));
    server.sin_family = AF_INET;
    server.sin_port = htons(PORT);
    server.sin_addr.s_addr = INADDR_ANY; 
    
	printf("local address: %s\n", inet_ntoa( server.sin_addr));
	if ((bind(socket_fd, (struct sockaddr *)&server, sizeof(struct sockaddr )))== -1)    { //sizeof(struct sockaddr) 
        fprintf(stderr, "Binding Failure\n");
        exit(1);
    }

    if ((listen(socket_fd, BACKLOG))== -1){
        fprintf(stderr, "Listening Failure\n");
        exit(1);
    }

     while(1) {

        size = sizeof(struct sockaddr_in);

        if ((client_fd = accept(socket_fd, (struct sockaddr *)&dest, &size))==-1 ) {
            perror("accept");
            exit(1);
        }
        printf("Server got connection from client %s\n", inet_ntoa(dest.sin_addr));

        while(1) {

                if ((num = recv(client_fd, buffer, 1024,0))== -1) {
                        perror("recv");
                        exit(1);
                }
                else if (num == 0) {
                        printf("Connection closed\n");
                        //So I can now wait for another client
                        break;
                }
                buffer[num] = '\0';
                printf("Server:Msg Received %s\n", buffer);
                if ((send(client_fd,buffer, strlen(buffer),0))== -1) 
                {
                     fprintf(stderr, "Failure Sending Message\n");
                     close(client_fd);
                     break;
                }

                printf("Server:Msg being sent: %s\nNumber of bytes sent: %d\n",buffer, strlen(buffer));

        } //End of Inner While...
        //Close Connection Socket
        close(client_fd);
    } //Outer While

    close(socket_fd);
    return 0;
} //End of main


client.c

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <regex.h>
#include <sys/wait.h>

#define PORT 3490
#define MAXSIZE 1024

int main(int argc, char *argv[])
{
    struct sockaddr_in server_info;
    struct in_addr ipv4addr;
    struct hostent *he;
    int socket_fd,num;
    char buffer[1024];
    char *ip;
    char buff[1024];
	int pipefd[2];
    pid_t cpid;
    char buf[1024];
    

	he = gethostbyname("Abhinav");
    if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0))== -1) {
        fprintf(stderr, "Socket Failure!!\n");
        exit(1);
    }

    memset(&server_info, 0, sizeof(server_info));
    server_info.sin_family = AF_INET;
    server_info.sin_port = htons(PORT);
    server_info.sin_addr = *((struct in_addr *)he->h_addr);
    printf("local address: %s\n", inet_ntoa( server_info.sin_addr));
	if (connect(socket_fd, (struct sockaddr *)&server_info, sizeof(struct sockaddr))<0) {
        //fprintf(stderr, "Connection Failure\n");
        perror("connect");
        exit(1);
    }

    //buffer = "Hello World!! Lets have fun\n";
    //memset(buffer, 0 , sizeof(buffer));
    while(1) {
        //echo client only
		
		printf("Client: Enter Data for Server:\n");
        fgets(buffer,MAXSIZE-1,stdin);
        
		if (pipe(pipefd) == -1) {
        perror("pipe");
        exit(1);
    	}
    	cpid = fork();
   		if (cpid == -1) {
        	perror("fork");
        	printf("inside error");
			exit(1);
    	}
    	if (cpid == 0) {    /* Child writes to a pipe */
		close(pipefd[0]); //closes read end
		write(pipefd[1],buffer , sizeof(buffer)); //writes to a pipe
		close(pipefd[1]); //closes write end
		//_exit(0);
	}
	else
	{
		close(pipefd[1]);
		while(read(pipefd[0],buf,1)>0)
			write(STDOUT_FILENO,buf,1);
		//send(socket_fd,buf, strlen(buf),0)	
		write(STDOUT_FILENO,"\n",1);
		close(pipefd[0]);
		wait(NULL);
		exit(0);
	}
		
	close(socket_fd);

}
//End of main
 
}

currently I am doing client for one service "echo" only. I have placed the send function in client. Is it the right place to put it?

Please guide me and sorry for coding like this..I am new to network programming and C.

Regards

Abhinav
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing key column from parent to child records

Hi Forum. I have this challenging issue that I'm hoping someone can help me. I have a file that contains 3 different types of segments (AM00, AM01, AM32) in a hierarchy structure and I want to be able to pass the column key from the parent record to the children records. AM00 - parent key:... (13 Replies)
Discussion started by: pchang
13 Replies

2. Programming

Child threads communicating with main thread via pipes

I have a simple client/server program I am using for learning purposes. I have it setup so that after server is setup and listening it than goes into a loop where it accepts incoming client connections. After each connection, the client socket is than passed to a thread routine where it can be... (3 Replies)
Discussion started by: Majortom71
3 Replies

3. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

4. Homework & Coursework Questions

Need help with deleting childīs parent and child subprocess

1. The problem statement, all variables and given/known data: I need to make an program that in a loop creates one parent and five children with fork(). The problem i'm trying to solve is how to delete the parent and child of the childīs process. 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: WhiteFace
0 Replies

5. Programming

IPC - pipes between parent and child process

Hi guys, I'm having some problem here, I'm studying pipes, and i want to create a shell in C and at this point a don't want to use semaphores, instead I want to use tricks. Straight to the doubt: I've a parent and a child process, and both of them has some code to execute, and the child process... (5 Replies)
Discussion started by: pharaoh
5 Replies

6. Shell Programming and Scripting

Passing a variable from a child script back to the parent

Hi I have written a script using ftp to get files from one server and copy them to 2 dirrerent servers. I wish to call this script from a parent script that will check the number of files copied and run a check sum for each file. As the filenames for the files in the get portion of the script... (3 Replies)
Discussion started by: Andy82
3 Replies

7. Shell Programming and Scripting

Parent/Child Processes

Hello. I have a global function name func1() that I am sourcing in from script A. I call the function from script B. Is there a way to find out which script called func1() dynamically so that the func1() can report it in the event there are errors? Thanks (2 Replies)
Discussion started by: yoi2hot4ya
2 Replies

8. Programming

Implementing 2 pipes between a parent and child process

Hi all, I'm trying to write a program that has some data it wants to send through a filter program(in this case tr), and then recieve the output from that filter program. The way I'm trying to do it is by setting up two pipes between the programs and piping the data in through one pipe and back... (2 Replies)
Discussion started by: bwgoudey
2 Replies

9. Filesystems, Disks and Memory

How hard can it be? ps child/parent

:( Since I'm fairly new to the scene and don't have much experience in shell programming, I decided to check out the net for a useful script or two. What I'm looking for is a script that would let me enter a PID and then show the process tree associated with it. So it would display the (grand-)... (2 Replies)
Discussion started by: velde046
2 Replies

10. Programming

how to passing message along pipes??

Dear All, Would you tell me how I can passing a message along pipes? I have built a token ring connected with pipes, but I don't know how to pass the message along it through the command line... Thx. Yours, i- (1 Reply)
Discussion started by: iminus
1 Replies
Login or Register to Ask a Question