LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-23-2010, 12:26 AM   #1
mgnidhi_3july
LQ Newbie
 
Registered: May 2010
Posts: 23

Rep: Reputation: 15
Question Named Pipe creation problem


Hi all,
i'm newbie..i'm just trying to understand named pipes in linux.For that i'm just trying to create one named pipe using mkfifo.but its not working properly..following is my code snippet. so if anyone comes across the mistake i made pls let me know..
thanks in advance...

Code:
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
    int fd, ret_val, count, numread ;
    char buf[255];
    
    /* Create the named - pipe */
    ret_val = mkfifo("/home/dbg/Sri/IPC/pipeTemp", 0666);
     printf("value s:%d",ret_val); //here i'm trying to print the 
                                   //return value of mkfifo its not
                                   //printing.What's the reason?
  
    
      if((ret_val == -1)){
        perror("Error creating the named pipe");
        exit (1);
    }
     
   getch();
}

regards,
Sri
 
Old 06-23-2010, 04:12 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,532

Rep: Reputation: 132Reputation: 132
if I comment out 2 lines
exit(1);
getch;

it is working
 
Old 06-23-2010, 05:18 AM   #3
mgnidhi_3july
LQ Newbie
 
Registered: May 2010
Posts: 23

Original Poster
Rep: Reputation: 15
Unhappy hi jay..

could you tel me exactly how did u do that???please...


Quote:
Originally Posted by j-ray View Post
if I comment out 2 lines
exit(1);
getch;

it is working
 
Old 06-23-2010, 05:23 AM   #4
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,672
Blog Entries: 29

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
Nidhi,

Post the errors you get while compiling that code.
 
Old 06-23-2010, 05:58 AM   #5
mgnidhi_3july
LQ Newbie
 
Registered: May 2010
Posts: 23

Original Poster
Rep: Reputation: 15
Question Hi anisha

first of all i thank u for replying for my quest..
the code which i posted here worked properly..but when i enhanced that code for opening a named pipe its not at all opening..it shows no error and compiles very well.but while executing fd=open(path ,O_RDONLY); at this time its not working properly...
Code:
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
    int fd, ret_val, count, numread ;
    char buf[255];
   
   /* Create the named - pipe */
     ret_val = mkfifo("/home/dbg/Sri/IPC/pipeTemp", 0666);
     printf("value s:%d\n",ret_val);  //prints 0 ie creates a pipe..


    if ((ret_val == -1) && (errno != EEXIST)) {
        perror("Error creating the named pipe");
        exit (1);
    }

    else
    {
      printf("named pipe created\n"); //printing this mes too...
    }


//from the following like its not working properly...
   fd = open("/home/dbg/Sri/IPC/pipeTemp", O_RDONLY);
//after the above line is executed fd value not equal to -1 so control 
//goes to else part right? but its not going... 
   if(fd== -1)
   {
      printf("couldn't open the file");
   }

   else
   {
    printf("File opened value s:%d\n",fd);//not printing this value.
   }

  read ( fd, buf, 100 );
  printf ( "buff data...:%s\n", buf );
    //getchar();
    return 0;
}
Quote:
Originally Posted by anishakaul View Post
Nidhi,

Post the errors you get while compiling that code.
 
Old 06-23-2010, 06:50 AM   #6
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,672
Blog Entries: 29

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
Quote:
Originally Posted by mgnidhi_3july View Post
Code:
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
    int fd, ret_val, count, numread ;
    char buf[255];
   
   /* Create the named - pipe */
     ret_val = mkfifo("/home/dbg/Sri/IPC/pipeTemp", 0666);
     printf("value s:%d\n",ret_val);  //prints 0 ie creates a pipe..


    if ((ret_val == -1) && (errno != EEXIST)) {
        perror("Error creating the named pipe");
        exit (1);
    }
    else
    {
      printf("named pipe created\n"); //printing this mes too...
    }


//from the following like its not working properly...
   fd = open("/home/dbg/Sri/IPC/pipeTemp", O_RDONLY);
//after the above line is executed fd value not equal to -1 so control 
//goes to else part right? but its not going... 
   if(fd== -1)
   {
      printf("couldn't open the file");
   }

   else
   {
    printf("File opened value s:%d\n",fd);//not printing this value.
   }

  read ( fd, buf, 100 );
  printf ( "buff data...:%s\n", buf );
    //getchar();
    return 0;
}
Change the RED colored code to the following, now compile and tell the results:
Code:
if ((ret_val < 0)
    {
        perror("Error creating the named pipe");
        exit (1);
    }
    else
    {
      printf("named pipe created\n"); //printing this mes too...
    }
 
Old 06-23-2010, 08:34 AM   #7
mgnidhi_3july
LQ Newbie
 
Registered: May 2010
Posts: 23

Original Poster
Rep: Reputation: 15
Hi anisha...

thanks for your patience..taking so much concern about my query...
i resolved that...one of my senior who sits beside me helped me in resolving it..the problem was not with server code which i had posted here..the problem was with client code..their while giving same path for pipe as fd = open("/home/dbg/Sri/IPC/pipeTemp", O_WRONLY); i had given like fd = open("home/dbg/Sri/IPC/pipeTemp", O_WRONLY); i missed '/' before home...because of that only it was not able to write..
now my code is working properly...two processes are communicating.. finally i'm very happy..since morning i was searching for this mistake.. anyway thanks a lot 'dhidhi' for trying out...:-)

regards,
Sri
 
1 members found this post helpful.
Old 06-23-2010, 08:36 AM   #8
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,672
Blog Entries: 29

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
Well it is good you solved your problem!

You can mark the thread SOLVED if you think you have got an answer to your problem through the Thread tools menu at the top of this page !
 
Old 06-23-2010, 08:52 AM   #9
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,672
Blog Entries: 29

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
P.S.
No need to mail me your replies, I can read them here as well
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
execvp with a named pipe bahwilliams Programming 2 02-01-2007 04:16 PM
What is a named pipe ronzoid Linux - General 1 01-18-2006 05:30 PM
unlinking named pipe sahel Programming 1 01-12-2006 05:45 AM
unlinking named pipe sahel Programming 1 01-09-2006 11:36 PM
named pipe sahel Programming 1 01-09-2006 09:29 AM


All times are GMT -5. The time now is 06:35 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration