Sponsored Content
Full Discussion: unnamed pipes and threads
Top Forums Programming unnamed pipes and threads Post 302573797 by nimesh on Tuesday 15th of November 2011 03:17:10 PM
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.
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

9. Programming

Problem with pipes

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

10. 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
create child(1m)														  create child(1m)

NAME
create child - Creates a child pointer at the master replica of the parent directory SYNOPSIS
cdscp create child child-name clearinghouse clearinghouse-name ARGUMENTS
The full name of the child pointer. The full name of a clearinghouse that contains a replica of the child directory. DESCRIPTION
The create child command creates a child pointer at the master replica of the parent directory. When CDS looks up a name in the namespace, it uses child pointers to locate directory replicas. Use the set cdscp preferred clearinghouse command before issuing this command to ensure that the request is directed to the master replica. Privilege Required You must have insert permission to the parent directory. NOTES
Use the create child command only to re-create a child pointer that is accidentally deleted. This command is designed only for trou- bleshooting. This command will fail if the associated directory does not exist. If the associated directory exists, this command will return success- fully. This command is replaced at Revision 1.1 by the dcecp command and may not be provided in future releases of DCE. EXAMPLE
The following command creates the child pointer in the parent directory /.:/subsys. It uses the replica located at the /.:/subsys/NY_CH clearinghouse to fill in its replica set. cdscp> create child /.:/subsys clearinghouse /.:/subsys/NY_CH RELATED INFORMATION
Commands: delete child(1m), list child(1m), show child(1m) create child(1m)
All times are GMT -4. The time now is 11:59 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy