Sponsored Content
Full Discussion: problem with pipes
Top Forums UNIX for Dummies Questions & Answers problem with pipes Post 302239166 by afser on Tuesday 23rd of September 2008 04:15:55 AM
Old 09-23-2008
Thank you guys..... I got it fixed.SmilieSmilieSmilie
IT was happening cuz of the statement fclose(fp2) which i had written at the end of the parent process. As execution would never reach that place, i was having this problem.

The solution is we have to use the above mentioned statement at the end of the child process.

here goes the correct code.....

#include <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main()
{
int pfd[2],i=0,j=0;;
pid_t cpid;
char buf[50],c;
FILE *fp1,*fp2;
int ch;

fp1=fopen("source.c","r+");
if(fp1==NULL)
{
perror("Error opening source file\n");
}
while((ch=getc(fp1))!=EOF)
{
buf[i]=ch;
i++;
}
buf[i] = '\0';
fclose(fp1);

if (pipe(pfd) == -1)
{
perror("pipe");
exit(EXIT_FAILURE);
}

cpid = fork();
if (cpid == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}

if (cpid == 0)
{ /* Child reads from pipe */
close(pfd[1]); /* Close unused write end */
fp2=fopen("dest.c","w+");
if(fp2==NULL)
{
perror("Error opening dest file\n");
_exit(1);
}
int i = 0;
while (buf[i]!='\0')
{
read(pfd[0], &buf[i], sizeof(buf[i]));
c=buf[i];
putc(c,fp2);
i++;
}
fclose(fp2);
close(pfd[0]);
_exit(EXIT_SUCCESS);
}
else
{ /* Parent writes to pipe */
j=0;
close(pfd[0]); /* Close unused read end */
while(buf[j]!='\0')
{
write(pfd[1], &buf[j], sizeof(buf[j]));
j++;
}
close(pfd[1]); /* Reader will see EOF */
wait(NULL); /* Wait for child */
exit(EXIT_SUCCESS);
}
}


cheers
afserSmilie
 

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. UNIX for Dummies Questions & Answers

learning about pipes!

im trying to figure out how to do the following: using pipes to combine grep and find commands to print all lines in files that start with the letter f in the current directory that contain the word "test" for example? again using pipes to combine grep and find command, how can I print all... (1 Reply)
Discussion started by: ez45
1 Replies

4. UNIX for Advanced & Expert Users

problem using pipes with "ls"

Hi all, I tried the following command $ find / -name xyx | ls -l so logically it should show the listing of directory xyz , assuming there's only one instance of xyz . But the above command shows the listing of current directory instead. I got the desired result using it in the... (4 Replies)
Discussion started by: bijeet_sunny
4 Replies

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

6. Shell Programming and Scripting

Problem with pipes on infinite streams

Here is an example code that shows the issue I have: #!/bin/bash counter() { seq 1000 | while read NUM; do echo $NUM echo "debug: $NUM" >&2 sleep 0.1 # slow it down so we know when this loop really ends done } counter | grep --line-buffered "" | head -n1 ... (10 Replies)
Discussion started by: tokland
10 Replies

7. Programming

Problem with pipes

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

8. Programming

please help a problem in client-server ipc message 2 pipes communication simple example

I want to have a message send & receive through 2 half-duplex pipes Flow of data top half pipe stdin--->parent(client) fd1--->pipe1-->child(server) fd1 bottom half pipe child(server) fd2---->pipe2--->parent(client) fd2--->stdout I need to have boundary structed message... (1 Reply)
Discussion started by: ouou
1 Replies

9. Programming

Problem with Pipes => Only works first pipe

Hi! I'm having problems with pipes... I need comunnications with childs processes and parents, but only one child can comunnicate with parent (first child), others childs can't. A brief of code: if(pipe(client1r)<0){ perror("pipe"); } ... (1 Reply)
Discussion started by: serpens11
1 Replies

10. Shell Programming and Scripting

Other way aside from putting more PIPES (|)

I already manage to get the output that i want.. but wat if removing all the pipes and convert it 1 liner with less pipes. My command below can get the ouput that i want. i just want to remove the pipes or less pipes. #cat file1 us-west-2a running i-3397a421... (2 Replies)
Discussion started by: kenshinhimura
2 Replies
PIPE(2) 						     Linux Programmer's Manual							   PIPE(2)

NAME
pipe, pipe2 - create pipe SYNOPSIS
#include <unistd.h> int pipe(int pipefd[2]); #define _GNU_SOURCE /* See feature_test_macros(7) */ #include <fcntl.h> /* Obtain O_* constant definitions */ #include <unistd.h> int pipe2(int pipefd[2], int flags); DESCRIPTION
pipe() creates a pipe, a unidirectional data channel that can be used for interprocess communication. The array pipefd is used to return two file descriptors referring to the ends of the pipe. pipefd[0] refers to the read end of the pipe. pipefd[1] refers to the write end of the pipe. Data written to the write end of the pipe is buffered by the kernel until it is read from the read end of the pipe. For fur- ther details, see pipe(7). If flags is 0, then pipe2() is the same as pipe(). The following values can be bitwise ORed in flags to obtain different behavior: O_CLOEXEC Set the close-on-exec (FD_CLOEXEC) flag on the two new file descriptors. See the description of the same flag in open(2) for rea- sons why this may be useful. O_DIRECT (since Linux 3.4) Create a pipe that performs I/O in "packet" mode. Each write(2) to the pipe is dealt with as a separate packet, and read(2)s from the pipe will read one packet at a time. Note the following points: * Writes of greater than PIPE_BUF bytes (see pipe(7)) will be split into multiple packets. The constant PIPE_BUF is defined in <limits.h>. * If a read(2) specifies a buffer size that is smaller than the next packet, then the requested number of bytes are read, and the excess bytes in the packet are discarded. Specifying a buffer size of PIPE_BUF will be sufficient to read the largest possible packets (see the previous point). * Zero-length packets are not supported. (A read(2) that specifies a buffer size of zero is a no-op, and returns 0.) Older kernels that do not support this flag will indicate this via an EINVAL error. Since Linux 4.5, it is possible to change the O_DIRECT setting of a pipe file descriptor using fcntl(2). O_NONBLOCK Set the O_NONBLOCK file status flag on the two new open file descriptions. Using this flag saves extra calls to fcntl(2) to achieve the same result. RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately. On Linux (and other systems), pipe() does not modify pipefd on failure. A requirement standardizing this behavior was added in POSIX.1-2016. The Linux-specific pipe2() system call likewise does not modify pipefd on failure. ERRORS
EFAULT pipefd is not valid. EINVAL (pipe2()) Invalid value in flags. EMFILE The per-process limit on the number of open file descriptors has been reached. ENFILE The system-wide limit on the total number of open files has been reached. ENFILE The user hard limit on memory that can be allocated for pipes has been reached and the caller is not privileged; see pipe(7). VERSIONS
pipe2() was added to Linux in version 2.6.27; glibc support is available starting with version 2.9. CONFORMING TO
pipe(): POSIX.1-2001, POSIX.1-2008. pipe2() is Linux-specific. EXAMPLE
The following program creates a pipe, and then fork(2)s to create a child process; the child inherits a duplicate set of file descriptors that refer to the same pipe. After the fork(2), each process closes the file descriptors that it doesn't need for the pipe (see pipe(7)). The parent then writes the string contained in the program's command-line argument to the pipe, and the child reads this string a byte at a time from the pipe and echoes it on standard output. Program source #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> int main(int argc, char *argv[]) { int pipefd[2]; pid_t cpid; char buf; if (argc != 2) { fprintf(stderr, "Usage: %s <string> ", argv[0]); exit(EXIT_FAILURE); } if (pipe(pipefd) == -1) { perror("pipe"); exit(EXIT_FAILURE); } cpid = fork(); if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (cpid == 0) { /* Child reads from pipe */ close(pipefd[1]); /* Close unused write end */ while (read(pipefd[0], &buf, 1) > 0) write(STDOUT_FILENO, &buf, 1); write(STDOUT_FILENO, " ", 1); close(pipefd[0]); _exit(EXIT_SUCCESS); } else { /* Parent writes argv[1] to pipe */ close(pipefd[0]); /* Close unused read end */ write(pipefd[1], argv[1], strlen(argv[1])); close(pipefd[1]); /* Reader will see EOF */ wait(NULL); /* Wait for child */ exit(EXIT_SUCCESS); } } SEE ALSO
fork(2), read(2), socketpair(2), splice(2), tee(2), vmsplice(2), write(2), popen(3), pipe(7) COLOPHON
This page is part of release 4.15 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. Linux 2017-11-26 PIPE(2)
All times are GMT -4. The time now is 10:54 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy