LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-04-2015, 06:52 AM   #16
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869

Please click on the Edit link under your post and add the [code] and [/code] tags (which I've already asked you to use).

Last edited by NevemTeve; 11-04-2015 at 06:54 AM.
 
Old 11-04-2015, 07:05 AM   #17
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
To me that code is all wrong, here is how I create a blocking pipe:
Code:
    int pipe_desc[2];

    if(pipe(pipe_desc) == -1) {
        perror("Error creating pipe: %d:%s\n", errno, strerror(errno));
    }
I just happen to make my pipes non-blocking, so following code I do use to make it non-blocking would be:
Code:
    int fl;

    /* RECEIVE side of the pipe */
    fcntl(pipe_desc[0], F_GETFL, &fl);
    fcntl(pipe_desc[0], F_SETFL, (fl | O_NONBLOCK));
    /* TRANSMIT side of the pipe */
    fcntl(pipe_desc[1], F_GETFL, &fl);
    fcntl(pipe_desc[1], F_SETFL, (fl | O_NONBLOCK));
I long ago wrote a blog entry on this subject, feel free to review, it is located here.
 
Old 11-04-2015, 07:09 AM   #18
YehudaSinger
Member
 
Registered: Jun 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
Code

Dear Nevem Teve
1. I tried to use the pipe2 with this code:


ret_code = pipe2) fd, flags) ;
AT first flag was initialized to !O_NONBLOCK. After that I tried with flags =0.
I moved to this updated code:

int *pipe_fd

static int pipe_index = 0 ;
int ret_code ;
char pipe_index_char[10];

const std::string pipe_file_name_prefix = "PipeFile" ;

int pipe_flags ;
mode_t pipe_mode ;
std::string pipe_file_name ;

sprintf(pipe_index_char, "%u", pipe_index ) ;

pipe_file_name =
pipe_file_name_prefix+pipe_index_char ;
pipe_flags = O_RDWR | O_CREAT | O_TRUNC ; // O_WRONLY | O_CREAT | O_TRUNC ;
pipe_flags = pipe_flags & ~O_NONBLOCK ;
pipe_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ;

pipe_fd[0] = open( pipe_file_name.c_str() , pipe_flags, pipe_mode ) ;
if ( pipe_fd[0] == -1 )
{
std::cout << "Error in Creating Pipe for Read \n" ;
std::cout << "Errno is: " << strerror(errno) << "\n" ;
}

pipe_fd[1] = open( pipe_file_name.c_str() ,pipe_flags ,pipe_mode ) ;
if ( pipe_fd[1] == -1 )
{
std::cout << "Error in Creating Pipe for Write\n" ;
std::cout << "Errno is: " << strerror(errno) << "\n" ;
}
ret_code = fcntl (pipe_fd[0], F_SETFL, pipe_flags) ;
if (ret_code==-1 )
{
std::cout << "Error in setting the Block property. \n" << "Error Code = " << ret_code << "\n" ;
}

Best regards,
Yehuda
 
Old 11-04-2015, 07:32 AM   #19
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
please read post#16
 
Old 11-04-2015, 07:58 AM   #20
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Where have you used the pipe2() function? It's not in that code. Why not read the recommendations to use [code] tags for one, and then secondly why not look at the 5 or 6 lines of sample code I posted and realize that this does exactly what you intend.

You are not using pipe() or pipe2() at all. Please review the man page. Why not give the blog entry a read too. There are code examples and there are additional blog entries to illustrate more complex things like how to use select() when you have multiple descriptors to check for inbound data.

I realize that persons like to do things their selves and thus feel they've learned better that way.

I myself learned these things by emulation, by searching for code examples, then doing experimentations much like you are, and then improving upon them.

But what I'm telling you is that while you are on the cusp of working with the concept of pipes, you are not actually correctly working with pipes in the classical Unix/Linux sense.

I believe my original experimentations were guided by the results of a web search where I used terms like "pipe programming example linux c" and I probably allowed the auto-complete to give me alternate suggestions.

The two main concepts to start with are (1) how to create a pipe at all, and then (2) how to use it. The next level of step is how to configure it. You say you want non-blocking I/O and I've told you that a natively created pipe is non-blocking I/O.

Once again, I'm not angry here, but what you're doing is making a case for the continual divisiveness between questioners and the offerers of answers. You'll get frustrated, because you've put in some effort, it's not correct, so you've been given some other recommendations, valid ones, and also not too complicated ones, and it appears that you're ignoring those things completely, as well as the points related to using [code] tags. We get frustrated seeing yet another questioner who ignores our suggestions, so we back off and figure, "Why bother? They're not reading what we write."
 
  


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
[SOLVED] why read() is blocking in the case of reading one end of pipe tarunchawla Programming 3 10-13-2014 08:57 AM
Red Hat SeLinux is blocking ssh and http unix1adm Linux - Security 11 03-07-2011 02:04 PM

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

All times are GMT -5. The time now is 06:10 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