Pipe between Childs


 
Thread Tools Search this Thread
Top Forums Programming Pipe between Childs
# 1  
Old 11-07-2012
Pipe between Childs

Hey guys,

I have to make a C program that simulates this command :

cat (files here) | sort > file.txt

So, I start and create a pipe. Then create the first child. This first child will execute the Cat through the pipe. Then create a second child that will execute sort, with input from pipe. And redirect output to the file.

So, I have this fragment of code, but the program doesn't execute the Sort and it shows me the error message instead.

Code:
..... variables here ....

if (pipe(tub) < 0) panic("Creació pipe");  /* pipe creation */
 
   switch (fork()) /* First child will exec Cat */ 
   { 
    
    case -1: panic("Fork 1"); 
     
    case 0: 
     
    close(1); dup(tub[1]); 
    close(tub[0]); close(tub[1]); 
     
    arg_list[0] = "cat";       /* Input files for Cat */
    for (p=0; p<Nfitxers; p++) { 
    pid = Pids[p]; 
    sprintf(s, "%d_Winners.txt", pid); 
    arg_list[p+1]= strdup(s); 
    } 
     
    arg_list[Nfitxers]= NULL; 
     
    execv("/bin/cat", arg_list);  /* Execute Cat here */
    panic("Executant execv cat"); 
 
    
    default: 
    break; 
   } 
    
   switch (fork()) /* Second child execut Sort */ 
   { 
    
    case -1: panic("Fork 2"); 
     
    case 0: 
     
    close(0); dup(tub[0]); 
    close(tub[0]);  
 
/* Creating Output file here */

    if ((dest = open("Winners.txt", O_WRONLY|O_TRUNC|O_CREAT, 0600))<0) { 
       panic("Creació del fitxer destí"); } 
 
     
    dup2(dest,STDOUT_FILENO);    /* Redirect output to file */
    dup2(dest,STDERR_FILENO);  
    close(dest);  
     
    execl( "sort" , "sort", NULL );   /* Executing sort */
    panic("Executant execl sort"); 
 
     
    default: 
    break; 
    
   } 
    
   close(tub[0]);  
   close(tub[1]); 
    
    
   wait(&st1); 
   wait(&st2);

# 2  
Old 11-07-2012
Shows what error message?

sort doesn't need cat's help to read more than one file, which should make your problem significantly easier -- no pipe at all. sort file1 file2 ...
# 3  
Old 11-07-2012
This sounds a lot more like a homework problem than a real programming problem. If this isn't a homework item, just exec sort with a -o option to have it set its own output and add the list of files to be sorted as its operands.

If this is a homework item, this is the wrong forum.
# 4  
Old 11-20-2012
Linux

This is a homework
# 5  
Old 11-20-2012
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to ignore Pipe in Pipe delimited file?

Hi guys, I need to know how i can ignore Pipe '|' if Pipe is coming as a column in Pipe delimited file for eg: file 1: xx|yy|"xyz|zzz"|zzz|12... using below awk command awk 'BEGIN {FS=OFS="|" } print $3 i would get xyz But i want as : xyz|zzz to consider as whole column... (13 Replies)
Discussion started by: rohit_shinez
13 Replies

2. Programming

How Can I share a socket between childs?

Hello guys! I had seen some posts at this forum talking about my problem, but maybe my scenario is a little different, and I want other solutions. I saw users of this forums saying that the way to shared sockets is using UNIX Sockets, but this is the only way in my scenario? My Scenario:... (4 Replies)
Discussion started by: serpens11
4 Replies

3. Shell Programming and Scripting

Replace pipe with Broken Pipe

Hi All , Is there any way to replace the pipe ( | ) with the broken pipe (0xA6) in unix (1 Reply)
Discussion started by: saj
1 Replies

4. Shell Programming and Scripting

How can I use pipe

Hi, guys: I am working on my shell using c. How can I use pipe to implement the following? ls -l 1>> | grep hellp 1<< 2>> | less 2<< (the output of ls goes to grep, and the output of grep goes to less) Thanks Please use and tags when posting code, data or logs etc. to preserve... (1 Reply)
Discussion started by: tomlee
1 Replies

5. Programming

waiting for multiple childs - C - waitpid

Hi gurus, I would like to fork more children and then write their return values: so far I tried: #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main(void) { pid_t pid; int rv=0, i; ... (5 Replies)
Discussion started by: wakatana
5 Replies

6. Programming

Need help with fork, forking multiple childs and shared memory

Hi all, I m writing an application, where i need to fork multiple childs and those child should handle particular task given to them. More descriptive. For example, suppose i have 4 Network, each network has multiple nodes. Now on the basis of network child should be forked and these child... (8 Replies)
Discussion started by: helpmeforlinux
8 Replies

7. Programming

forking. sharing global data in childs

hi, i want to write a code for forking 3 4 child. n wants that every child process one of the account from global account list. i wrote a program for that, but problem is every child is processing every account in list. what can me done to avoid it. attaching code with it #include <stdio.h>... (2 Replies)
Discussion started by: anup13
2 Replies

8. Programming

share file descriptor between childs

#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/wait.h> #include <fcntl.h> #include <signal.h> #include <unistd.h> #include <string.h> #define BUFF_SIZE 256 #define CHILDS 4 #define DATAFILE "Client_Files.txt" void worker(int n);... (3 Replies)
Discussion started by: dlcpereira
3 Replies

9. Shell Programming and Scripting

How to kill a process and its childs given pid and userid

I need to write a shell script which would take 2 arguments pid , userid. Then it should kill all the child process under it. If a child process is not killed then it should wait for 1 minute and should kill. can anybody give me the idea to write it? (0 Replies)
Discussion started by: nani_g
0 Replies

10. Shell Programming and Scripting

Is there any cmd to kill a process including its childs ( or sub processes spawned by

Dear Unix Gurus, Here is my query. If i start a script,it inturn calls many other scripts ..and most of them continue to run in parallel. Suppose,if i want to stop my script for some reason,i need to kill -9 each of the processes running.It becomes clumsy if the sub processes r more. ... (15 Replies)
Discussion started by: gvsreddy_539
15 Replies
Login or Register to Ask a Question