unnamed pipes and threads


 
Thread Tools Search this Thread
Top Forums Programming unnamed pipes and threads
# 1  
Old 11-15-2011
unnamed pipes and threads

Hi!
I am writing a C program that will create a child, child will create a thread and the thread will send a message to a unnamed pipe and will print the message before exiting.

here is my work:
Code:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
 
void *thread_function (void *arg);
char message [] = "Hello World";
int fd[2]; // Creating file descriptor
char buff [100];
 
int main()
{
  pipe (fd); //creating unnamed pipe
  pid_t pid;
  pthread_t a_thread;
  void *thread_result;
  pid = fork();
  switch(pid)
  {
    case -1:
      perror("fork failed");
      exit(1);
    case 0: // creating child
      printf("Child Running\n");
      pthread_create (&a_thread, NULL, thread_function, (void *)message);
      sleep (3);
    default:
      break;
  }
  close (fd[1]); //closing write file descriptor
  close (fd[0]); //closing read file descriptor
  exit(0);
}
 
 
void *thread_function (void *arg)
{
  printf ("Thread running\n");
  sleep (3);
  write (fd[1], message, strlen (message)); // writing to pipe
  read (fd[0], buff, strlen (message)); // reading from file
  printf ("Message is: %s\n", buff);
  pthread_exit("thread exiting");
}

Seems like my thread doesn't exit and also the message is not send to pipe in thread

thank you

Last edited by Scott; 11-15-2011 at 04:29 PM.. Reason: Please use code tags... and indent your code. Thanks.
# 2  
Old 11-15-2011
Putting data into a pipe doesn't make it immediately available on the other end. A pipe tries to get at least a line of data for efficiency reasons, or even more.

Try "Hello World\n". I seem to remember that newlines can flush a pipe.

Failing that, closing the write end after you finish writing into it will always make the data appear on the read end.
# 3  
Old 11-15-2011
I think you are exiting the process before the thread has a chance to write/read and print the buffer contents. You sleep(3) after you start the thread and then sleep(3) in the thread. Likely that the sleep in the switch will finish and return causing the process to exit before the sleep in the thread finishes.


Instead of using a sleep() after creating the thread you should use pthread_join() to wait for the thread to be done. If you do that you should see the message from the thread.
# 4  
Old 11-15-2011
Good catch. But I still think the pipe will block until fed an \n or the write end is closed, too.
# 5  
Old 11-15-2011
Quote:
Originally Posted by Corona688
Good catch. But I still think the pipe will block until fed an \n or the write end is closed, too.

Thanks.



I thought so too, but I gave the code a try on both a SUSE and FreeBSD and it doesn't block either way.
# 6  
Old 11-16-2011
Thanks guys, exactly what I wanted!
# 7  
Old 11-16-2011
Quote:
Originally Posted by agama
Thanks.



I thought so too, but I gave the code a try on both a SUSE and FreeBSD and it doesn't block either way.
Good to know.

I know it definitely used to block until it had full lines, though. I was trying to use pipes for inter-thread communication in Linux many years ago, and found it much easier to write newlines than to do non-blocking I/O. So I'm not sure you can depend on the pipe not blocking like that.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Programming

Problem with pipes

problem solved. (1 Reply)
Discussion started by: superfons
1 Replies

3. Programming

Pipes in C

Hello all, I am trying to learn more about programming Unix pipes in C. I have created a pipe that does od -bc < myfile | head Now, I am trying to create od -bc < myfile | head | wc Here is my code, and I know I might be off, thats why I am here so I can get some clarification. #include... (1 Reply)
Discussion started by: petrca
1 Replies

4. Shell Programming and Scripting

Pipes not working

Hi, thanks for b4. can anyone tell me why following not working: noUsers=$(who | cut -d" " -f1 | wc -l) What i'm trying to do is get a list of logged on users and pass it to 'wc -l' and store the output to a variable. Any ideas? (1 Reply)
Discussion started by: Furqan_79
1 Replies

5. UNIX for Advanced & Expert Users

Threads and Threads Count ?

Hi all, How can I get the list of all Threads and the Total count of threads under a particular process ? Do suggest !! Awaiting for the replies !! Thanks Varun:b: (2 Replies)
Discussion started by: varungupta
2 Replies

6. UNIX for Advanced & Expert Users

FIFO Pipes

Hi...Can anyone please guide me on FIFO Pipes in UNIX.I have lerant things like creating fifo pipes,using them for reads and writes etc.I want to know what is the maximum amount of memory that such a pipe may have? Also can anyone guide me on where to get info on this topic from? (4 Replies)
Discussion started by: tej.buch
4 Replies

7. Programming

pipes inside

Extremely need to understand some aspects of pipes realization. The main question is in which memory are pipes placed? (13 Replies)
Discussion started by: pranki
13 Replies

8. Programming

pipes + structs

heya got a small problem here. im trying to pipe eg: 'ls | more'. i have created a command line parser which separates ls and more into two commands. i have also created a struct, each struct has a command name, number of arguments, redirect_in, redirect_out, pipe_in, etc etc.... eg: struct... (0 Replies)
Discussion started by: mile1982
0 Replies

9. Shell Programming and Scripting

cd using pipes

Hi, Can the cd command be invoked using pipes??? My actual question is slightly different. I am trying to run an executable from different folders and the path of these folders are obtained dynamically from the front end. Is there a way in which i can actually run the executable... (2 Replies)
Discussion started by: Sinbad
2 Replies

10. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies
Login or Register to Ask a Question