Put stdout in a pipe - c

I want to know the number of time a word is repeat in different file,
for that i need to use fork() function and create for each file a child that will find this number and add the different results in the parent.
I successfully find the number occurrences, but can't communicate this information to the parent.
I understand that I need to use pipe and dup2. I used them both previously, but separately, and I couldn't really say that I am comfortable with them.
As you can see with the variable 'temp', the pipe is empty. At first I thought that it was a synchronization problem but it doesn't seem to be the case. It is my understanding that dup2(tube[1],1) put stdout in the pipe, but I am starting to doubt.
What do I miss ?
int main(int argc, char const *argv[])
{
int tube[2];pipe(tube);int temp;
int s=0;
for (int i = 2; i < argc; i++)
{
if (fork()==0)
{
dup2(tube[1],1);
close(tube[1]);
close(tube[0]);
execlp("grep","grep","-c",argv[1],argv[i],NULL);
}
}
wait(NULL);
for (int i = 2; i < argc; i++) {
{
close(tube[1]);
close(tube[0]);
read(tube[0],&temp,sizeof(int));
printf("temp=%d\n",temp);
s+=temp;
}
}
printf("s=%d",s);
return 0;
}

After pipie(), the first file descriptor (FD) returned is the reader side of the pipe, the second FD is the writer side. So, you need to close the first FD in the child path, and the second DF in the parent path of your pogram.
You must not close the first FD in the parent path, since you want the parent to read what the client(s) wrote. And you must not close the second FD in the child path; how shall it be able to write to the pipe otherwise.
Remember that stdout is thought to be output for the user, i.e. text. So, your code needs to receive a character string (the count of matching lines), then, test for a valid number, and convert this to an int (long int, long long int), to be able to sum up.
Also, more than one child process may have written it's result before the parent reads from the pipe, i.e. multiple newline terminated strings may be read in a single read.
Finally, you need to wait() upon each child process, otherwise they will become zombies.
The problem made me curious, so I tried to come up with some working code.
//------------------------------------------------------------------------------
//
// The purpose of this program is to find the total number of lines containing
// the specified grep pattern in all the files specified.
//
// Parameters:
// pattern file [ file [ ... ] ]
//
// pattern -> search pattern to be passed to grep
// file -> file(s) to be scanned for "pattern" occurences.
//
//------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main( int argc, char* argv[] ) {
int pipefd[2];
int totalCount = 0;
//----------------------------------------
// Open a pipe to receive the result from
// running "grep" in the child processes.
//----------------------------------------
pipe(pipefd);
//---------------------------------------------------------
// Start a child process for each file given as parameter.
// First file is passed as argument #2.
//---------------------------------------------------------
for ( int ii = 2; ii < argc; ii++ ) {
//------------------------
// This is the child code
//------------------------
if ( fork() == 0 ) {
//-----------------------------
// Redirect stdout to the pipe
//-----------------------------
dup2( pipefd[1], 1 );
//------------------------------------
// Close the reader side of the pipe.
//------------------------------------
close( pipefd[0] );
execlp( "grep", "grep", "-c", argv[1], argv[ii], NULL );
}
}
//-----------------------------------------------------------------------
// This is the parent code.
//
// There possibly is more than one child process writing to the pipe.
// Writes and reads are atomic, however, more than one child may be able
// to write to the pipe before the parent can read.
//-----------------------------------------------------------------------
for ( int ii = 2; ii < argc; ii++ ) {
char result[1024];
int bytesRead;
int ss, pp;
//---------------------------
// Close writer side of pipe
//---------------------------
close( pipefd[1] );
//----------------------------------------------------------
// Read the data that one or more child process has written
//----------------------------------------------------------
bytesRead = read( pipefd[0], &result, sizeof( result ) );
if ( bytesRead > 0 ) {
//---------------------------------------------------------
// One or more *newline terminated* string has been read
// from the pipe, representing the result from one or
// more grep command that has finised.
//
// Each newline terminated string is converted to a zero
// terminated C-sytle string, so that it can be passed to
// atoi().
//---------------------------------------------------------
ss = 0;
for ( pp = 0; pp < bytesRead; pp++ ) {
if ( result[pp] == '\n' ) {
result[pp] = 0x0;
totalCount += atoi( &result[ss] );
ss = pp + 1;
}
}
}
wait( NULL );
}
//-----------------------------------
// Print the final result and return
//-----------------------------------
printf("Total number of matches: %d\n\n", totalCount );
return 0;
}
Edit from April 19th: Removed some leftover from intermediate code version. Clarified the conversion from newline terminated to zero terminated string.

Related

Someone can help me with my code in c?

Iam trying to create a code with 1 parent and 2 childrens. The method recive 3 parameters:
original_file word1 word2
The parent read a file line by line:
If the line is pair, send the line to method proccess_pair and the word1.
If the line contains the word1, save the lines in the file_1.txt
If the line is odd, send the line to method proccess_odd and the word2.
If the line contains the word1, save the lines in the file_2.txt
Im beginner in c, and i trying with this:
int p_h1[2] // pipe from parent to child1
int p_h2[2];// pipe from parent to child2
int main(int argc, char **argv){
pid_t pdi1, pdi2;
FILE *fd; // for original file
FILE *p_h1f, *p_h2f; //file create for child1 and child2 respectively
char buffer[1024];//buffer
if (pid1<0){
fprintf(stderr,"Error fork \n %s \n",strerror(errno));
exit(EXIT_FAILURE);
}
else if (pid1==0){//Im the child1
//proccess for child 1
proccess_pair(arg[2]);
exit(EXIT_SUCCESS);
}
pid2 = fork();
if (pid2<0){
fprintf(stderr,"Error fork \n %s \n",strerror(errno));
exit(EXIT_FAILURE);
}
else if (pid2==0){//Im the child2
//proccess for child 2
proccess_odd(arg[2]);
exit(EXIT_SUCCESS);
}
//Parent dont read from pipe
close(p_h1[0]);
close(p_h2[0]);
fd = fopen(argv[1],"r"); //I openthe file for read it;
p_h1f = fdopen(p_h1[1],"w")
p_h2f = fdopen(p_h2[1],"w")
int i = 1;
while(fgets(buffer,1024,fd) != NULL){
if (i % 2 ==0){ //check if the lines is pairs
fputs(buffer,p_h1f);
fflush(p_h1f);
}else{
fputs(buffer,p_h2f);
fflush(p_h2f);
}
i++;
}
close(p_h1[1]);
close(p_h2[1]);
fclose(fd);
wait(NULL);
wait(NULL);
}
Both methods(for chil1 and chil2) will be the same(but closing the correct sides of pipes), for this reason i only implement one of them:
void proccess_pair(char *word1){
FILE *fd;
fd = fopen("file_1.txt","w");
//closing the not used
close(p_h1[1]);
close(p_h2[1]);
close(p_h2[0]);
int nsto = dup(1)//duplicate the stdout
dup2(fd,1);//changing stdout->file_1.txt
execlp("grep","grep",word1,NULL);//execution of grep
exit(EXIT_SUCCESS);
}
Im learning and i know that i have many many error, for this reason i need help.
Regards
How i can create many pipes in an array in c?
On a POSIX-conformant system, you can do that by calling pipe() many times on elements of a 2D array of int, as you presented.
¿I can use two different pipes(parent-child1,parent-child2)? I can use an array of pipes?
A pipe itself lives only in the kernel. There is no userspace data
structure representing a pipe, so you cannot have an array of pipes.
The file descriptors for the pipe ends, however, are just ints. The pipe() function takes as its argument a pointer to the first element of an array of at least two int, and (on success) it writes the appropriate file descriptors into the array.
From a C perspective, there is nothing special about the array in which the pipe ends are to be returned. In particular, it can be an element of a multi-dimensional array if you so desire. Or it can be a local variable. Or it can be a member of a struct or a union. Or it can be a large enough block of dynamically-allocated space. It's not special.
Something like this should work:
int new_process(char *word){ // return the writing part of a pipe to a newly created child
int p[2];
pipe( p ); // get a new pipe
if (fork()==0) { // create a new child
dup2(p[0],0); // child's in is pipe's entry
close(p[1]); // close writing part
execlp("grep","grep",word,NULL); // exec
}
close(p[0]); // parent don't need the reading part
return p[1]; // send back the writing part to caller
}
int main(int argc, char **argv) {
int f1 = new_process(argv[1]); // get the pipe to first child
int f2 = new_process(argv[1]); // ...second...
char buffer[1024];//buffer
FILE *fd = fopen(argv[1],"r"); // open the file for reading;
while(fgets(buffer,1024,fd) != NULL) { // read aline
if (i % 2 ==0) { //check if the line no is pair
fputs(buffer,f1); // send it to first child
fflush(f1);
} else{
fputs(buffer,f2); // send it to second child
fflush(f2);
}
i++;
}
close(f1);
close(f2);
fclose(fd);
wait(NULL);
wait(NULL);
}
Don't forget to add necessary controls on failures.

Interprocess Communication in C, one character at a time

First off, this IS homework, I am not asking for an answer, however I am confused about something.
I have a homework assignment for a programming class, and I am a little confused about how to write the code the specific way that the instructor is asking.
The program first creates a child process, and then proceeds to send command line arguments from the parent process, through a pipe, ONE CHARACTER at a time to the child process, and then read them into the child process ONE CHARACTER at a time, incrementing the character count in the child process each time a character is read in.
I think I accomplished sending the data through the pipe one character at a time, but I have no idea how to "go" to the child process every time a character is sent, read it, increment the number of characters, and then go back to the parent process and repeat.
Here is my code, It works and gives accurate answers, but any tips on how to accomplish what my instructor is asking would be appreciated, thank you!!
// Characters from command line arguments are sent to child process
// from parent process one at a time through pipe.
//
// Child process counts number of characters sent through pipe.
//
// Child process returns number of characters counted to parent process.
//
// Parent process prints number of characters counted by child process.
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char **argv)
{
// set up pipe
int pA[2];
char buff[50];
pipe(pA);
// call fork()
pid_t childId = fork();
if (childId == 0) {
// -- running in child process --
int nChars = 0;
// close the output side of pipe
close(pA[1]);
// Receive characters from parent process via pipe
// one at a time, and count them.
nChars = read(pA[0], buff, sizeof(buff)); //this line of code is what i need to change to be reading characters in one at a time
// Return number of characters counted to parent process.
return nChars;
}
else {
// -- running in parent process --
int nChars = 0;
int size = 0;
printf("CS201 - Assignment 3 - Timothy Jensen\n");
// close the input side of the pipe
close(pA[0]);
// Send characters from command line arguments starting with
// argv[1] one at a time through pipe to child process.
for (int i = 1; i < argc; i++)
{
size = strlen(argv[i]);
for (int z = 0; z < size; z++)
{
write(pA[1], &argv[i][z], 1);
}
}
// Wait for child process to return. Reap child process.
// Receive number of characters counted via the value
// returned when the child process is reaped.
wait(&nChars);
printf("child counted %d chars\n", nChars/256);
return 0;
}
}
You need to make the following changes:
Make the last argument 1 in the call to read.
read(pA[0], buff, 1);
Put the above call in a while loop and increment nChar for every successful attempt at read.
while ( read(pA[0], buff, 1) == 1 )
{
++nChars;
}
Close the file descriptor from the parent process once you are done writing to it.
Here's a working version of main.
int main(int argc, char **argv)
{
// set up pipe
int pA[2];
char buff[50];
pipe(pA);
// call fork()
pid_t childId = fork();
if (childId == 0) {
// -- running in child process --
int nChars = 0;
// close the output side of pipe
close(pA[1]);
// Receive characters from parent process via pipe
// one at a time, and count them.
while ( read(pA[0], buff, 1) == 1 )
{
++nChars;
}
return nChars;
}
else {
// -- running in parent process --
int nChars = 0;
int size = 0;
printf("CS201 - Assignment 3 - Timothy Jensen\n");
// close the input side of the pipe
close(pA[0]);
// Send characters from command line arguments starting with
// argv[1] one at a time through pipe to child process.
for (int i = 1; i < argc; i++)
{
size = strlen(argv[i]);
for (int z = 0; z < size; z++)
{
write(pA[1], &argv[i][z], 1);
}
}
close(pA[1]);
// Wait for child process to return. Reap child process.
// Receive number of characters counted via the value
// returned when the child process is reaped.
wait(&nChars);
printf("child counted %d chars\n", nChars/256);
return 0;
}
}
It seems a little silly, but you could change:
nChars = read(pA[0], buff, sizeof(buff));
to:
char ch;
nChars = read(pA[0], &ch, 1);
Of course, you would put the above into a loop to assemble a string 'one character at a time' back into buff.

c parallel processes involving pipe

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
int i;
int fd[2];
int values[argc - 1];
for( i = 1; i < argc; i++ ) {
pipe(fd);
switch( fork()) {
case 0: /* child */
/* Do stuff */
close(fd[0]);
int value = atoi(argv[i]);
write(fd[1], &value, sizeof(value));
exit( 0 );
case -1:
perror( "fork" );
exit(1);
default: /* parent */
close(fd[1]);
read(fd[0], &values[i - 1], sizeof(values[i - 1]));
/* do stuff, but don't wait() or terminate */
}
}
for (i = 0; i < (argc - 1); i++)
{
printf("%d\n", values[i]);
}
return 0;
}
im trying to create as many processes as the number of arguments given to the executable and have each process pipe the argument to the parent and store into an array and at the end print out the elements of the array. since the processes run in parallel, when i print out the array elements, the order should be random with respect to the order i entered these elements, but that doesnt seem to be the case as i have ran the executable 1 million times, can someone tell me what the problem is? So someone kindly pointed out that read serializes things, what should i do to make the processes truly parallel?
Your pipe read in the parent serializes things.
You (1) create the child; (2) child writes to the pipe; (3) parent will block on read until there is something to read. This repeats for all the children.

passing command line arguments to a child process and count them

I want the parent process to take the arguments to main() and send the characters in them one at a time to the child process through a pipe starting with argv[1] and continue through the rest of the arguments.(one call to write for each character).
I want the child process to count the characters sent to it by the parent process and print out the number of characters it received from the parent. The child process should not use the arguments to main() in any way whatsoever.
What am i doing wrong? do i need to use exec()?
output that isnt correct:
~ $ gc a03
gcc -Wall -g a03.c -o a03
~ $ ./a03 abcd ef ghi
child: counted 12 characters
~ $
here is the program..
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{
int length = 0;
int i, count;
int fdest[2]; // for pipe
pid_t pid; //process IDs
char buffer[BUFSIZ];
if (pipe(fdest) < 0) /* attempt to create pipe */
printf("pipe error");
if ((pid = fork()) < 0) /* attempt to create child / parent process */
{
printf("fork error");
}
/* parent process */
else if (pid > 0) {
close(fdest[0]);
for(i = 1; i < argc; i++) /* write to pipe */
{
write(fdest[1], argv[i], strlen(argv[1]));
}
wait(0);
} else {
/* child Process */
close(fdest[1]);
for(i = 0; i < argc; i++)
{
length +=( strlen(argv[i])); /* get length of arguments */
}
count = read(fdest[0], buffer, length);
printf("\nchild: counted %d characters\n", count);
}
exit(0);
}
You said that "the child process should not use the arguments to main() in any way whatsoever". However, I see that your child process is using argc. Doesn't this defeat your restriction?
You also say that you want "one call to write for each character". Your current implementation uses one call to write for each argument, not each character. Was this a typo? If not, you will want to use something more like this:
char nul='\0', endl='\n';
for (a=1; a < argc; ++a) {
for (c=0; c < strlen(argv[a]); ++c) {
write(fdest[1], &argv[a][c], 1);
}
write(fdest[1], &nul, 1);
}
write(fdest[1], &endl, 1);
This will write one character at a time, with each argument as a NULL-terminated string and a newline character at the end. The newline is only there to serve as a marker to indicate that there is no more data to send (and is safe to use since you won't be passing in a newline in a CLI argument).
The child process will just need to be a loop that reads incoming bytes one by one and increments a counter if the byte is not '\0' or '\n'. When it reads the newline character, it breaks out of the input processing loop and reports the value of the counter.
You have an error here:
write(fdest[1], argv[i], strlen(argv[1]));
You should take strlen(argv[i]) rather, or you're telling write() to read past the space of argv[i] and invoke undefined behavior.
Note that you're only calling read() once. By the time you're calling read(), perhaps only one of the argv[]s have been written by the parent. Or 2. Or any number of them.
The problem is here
write(fdest[1], argv[i], strlen(argv[1]));
Notice that this is strlen of argv[1], it should be argv[i]. You are actually referencing past the end of argv[2] and argv[3] in this loop
You are effectively writing strlen("abcd") * 3 characters which is 12 chars
In here:
for(i = 1; i < argc; i++) /* write to pipe */
{
write(fdest[1], argv[i], strlen(argv[1]));
}
strlen(argv[1]) should be in fact strlen(argv[i])

parent process, and a child process

I am trying to write a program that The parent process will take the arguments to main() and send the characters in them one at a time to the child process through a pipe (one call to write for each character). The child process will count the characters sent to it by the parent process and print out the number of characters it received from the parent. The child process should not use the arguments to main() in any way whatsoever. The child should return normally and not have the parent kill the child.
Am i counting the arguments right? am i sending the arguments in one at a time, and am i reaping the child?
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define size = 100;
int main(int argc, char *argv[])
{
int i, count =0;
int c;
int fdest[2]; // for pipe
pid_t pid; //process IDs
char buffer[BUFSIZ];
if (pipe(fdest) < 0) /* attempt to create pipe */
perror( "pipe" );
if ((pid = fork()) < 0) /* attempt to create child / parent process */
{
perror( "fork" );
}
/* parent process */
else if (pid > 0) {
close(fdest[0]);
for (i=1; i < argc; ++i)
{
for (c=0; c < strlen(argv[i]); ++c) {
write(fdest[1], &argv[i][c], 1);
}
}
close(fdest[1]);
wait(NULL);
exit(0);
} else {
/* child Process */
close(fdest[1]);
while (read(fdest[0], &buffer, 1) > 0)
{
count++;
}
printf("\nchild: counted %d characters\n", count);
}
wait(NULL);
exit(0);
}
The second wait() is superfluous; the child has no children of its own to wait for. The second 'exit(0);' could be replaced by 'return(0);'. You could omit the previous 'exit(0);' too.
The '#define size = 100;' is unused, which is just as well since the '=' makes it unusable for most purposes (and the semi-colon is a bad idea too - seldom does a macro end with a semi-colon). It should be '#define size 100' or 'enum { size = 100 };'. Often, people use upper case names for 'manifest constants', hence 'enum { SIZE = 100 };.
If you are reading one character at a time, you really don't need a buffer of size BUFSIZ (which is usually 512 or larger).
Also, it is a bad idea to do 'for (c = 0; c < strlen(argv[c]); c++)' because that calculates the length of the string on each iteration. Replace it with either of these:
for (const char *str = argv[i]; *str != '\0'; str++)
write(fdest, str, 1);
for (c = 0, len = strlen(argv[i]); c < len; c++)
write(fdest[1], &argv[i][c], 1);
You close the unused ends of the pipes - that is a crucial step to making things work correctly.
The code seems to be counting correctly. It works off the shelf when I test it. Why are you suspicious that it does not work?

Resources