09-23-2008
problem with pipes
I have written the following program. The function of this prog is to read data from a file(source.c) and write into another file(dest.c) using pipes. I have just written a line in the source file.Im able to compile and run the program without errors. But the data is not written onto the other file(dest.c);
please help!!!!!!!!!!!!!!!!!!!!!!
[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[20],ch,c;
FILE *fp1,*fp2;
fp1=fopen("source.c","r+");
if(fp1==NULL)
{
perror("Error opening source file\n");
}
while((ch=getc(fp1))!=EOF)
{
buf[i]=ch;
printf("i=%d ch=%c\n",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 */
char arr;
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 (i<=19)
{
read(pfd[0], &buf[i], sizeof(buf[i]));
c=buf[i];
putc(c,fp2);
i++;
}
close(pfd[0]);
_exit(EXIT_SUCCESS);
}
else
{ /* Parent writes argv[1] to pipe */
j=0;
close(pfd[0]); /* Close unused read end */
write(pfd[1], buf, strlen(buf));
j++;
close(pfd[1]); /* Reader will see EOF */
wait(NULL); /* Wait for child */
exit(EXIT_SUCCESS);
}
fclose(fp2);
}
10 More Discussions You Might Find Interesting
1. Filesystems, Disks and Memory
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
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
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
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
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
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 solved. (1 Reply)
Discussion started by: superfons
1 Replies
8. Programming
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
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
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
LEARN ABOUT FREEBSD
pipe2
PIPE(2) BSD System Calls Manual PIPE(2)
NAME
pipe, pipe2 -- create descriptor pair for interprocess communication
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <unistd.h>
int
pipe(int fildes[2]);
int
pipe2(int fildes[2], int flags);
DESCRIPTION
The pipe() system call creates a pipe, which is an object allowing bidirectional data flow, and allocates a pair of file descriptors.
The pipe2() system call allows control over the attributes of the file descriptors via the flags argument. Values for flags are constructed
by a bitwise-inclusive OR of flags from the following list, defined in <fcntl.h>:
O_CLOEXEC Set the close-on-exec flag for the new file descriptors.
O_NONBLOCK Set the non-blocking flag for the ends of the pipe.
If the flags argument is 0, the behavior is identical to a call to pipe().
By convention, the first descriptor is normally used as the read end of the pipe, and the second is normally the write end, so that data
written to fildes[1] appears on (i.e., can be read from) fildes[0]. This allows the output of one program to be sent to another program: the
source's standard output is set up to be the write end of the pipe, and the sink's standard input is set up to be the read end of the pipe.
The pipe itself persists until all its associated descriptors are closed.
A pipe that has had an end closed is considered widowed. Writing on such a pipe causes the writing process to receive a SIGPIPE signal.
Widowing a pipe is the only way to deliver end-of-file to a reader: after the reader consumes any buffered data, reading a widowed pipe
returns a zero count.
The bidirectional nature of this implementation of pipes is not portable to older systems, so it is recommended to use the convention for
using the endpoints in the traditional manner when using a pipe in one direction.
RETURN VALUES
The pipe() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate
the error.
ERRORS
The pipe() and pipe2() system calls will fail if:
[EMFILE] Too many descriptors are active.
[ENFILE] The system file table is full.
[ENOMEM] Not enough kernel memory to establish a pipe.
The pipe2() system call will also fail if:
[EINVAL] The flags argument is invalid.
SEE ALSO
sh(1), fork(2), read(2), socketpair(2), write(2)
HISTORY
The pipe() function appeared in Version 3 AT&T UNIX.
Bidirectional pipes were first used on AT&T System V Release 4 UNIX.
The pipe2() function appeared in FreeBSD 10.0.
BSD
May 1, 2013 BSD