Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,105 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,699 people online right now. Registration is fast and FREE... Join Now!




Inter process communication

 
Reply to this topicStart new topic

Inter process communication

optimist1958
2 Oct, 2007 - 01:08 AM
Post #1

New D.I.C Head
*

Joined: 1 Oct, 2007
Posts: 1


My Contributions
CODE

#include <stdio.h>
#include <unistd.h>    

#define BUFSIZE 128

main()
{
    int fd[2];
    char n;
    int i,c,d;
    char line[BUFSIZE];
    
    
    pipe(fd);  
c=fork();
if(c>0){
     d=fork();
}    

        
    if (c == 0) {

        close(fd[0]);

        for (i=0; i < 10; i++) {

            sprintf(line,"%s","i am child 1");
            write(fd[1], line, BUFSIZE);
            printf("Child writes: %s\n","i am child 1");
            sleep(2);
        }
    }
    
    if (d == 0) {

        close(fd[0]);

        for (i=0; i < 10; i++) {

            sprintf(line,"%s","i am child 2");
            write(fd[1], line, BUFSIZE);
            printf("Child writes: %s\n","i am child 2");
            sleep(2);
        }
    }
    if(c>0 & d>0) {

        close(fd[1]);

        for (i=0; i < 10; i++) {

            printf("\t\t\t Parent trying to read pipe\n");
            read(fd[0], line, BUFSIZE);
            sscanf(line,"%s",&n);
            printf("\t\t\t Parent reads: %d\n",n);
        }
    }
}


the problem says:

create 3 processes, one parent and two child
the two child are to write different messages down a single pipe having size 128 bytes(I am child 1,I am child 2). make sure the children do not fill up the pipe i.e are hung waiting for the pipe to be read. if there is a block children will print an error message on screen and continue on. the parent is to continuously read the same pipe and print all the message that come to pipe. the parent is to perform a non blocking read on the pipe

i have tried to work out this problem. but its not exactly what the question asks. please try to solve it
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Inter Process Communication
2 Oct, 2007 - 02:46 AM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,441



Thanked: 64 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE

CODE

main()
{



Is there any reason why you did not set a return type?

http://members.aon.at/hstraub/linux/socket...ocket++_11.html <-- Good example of your project. Let Google be your friend.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:10PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month