New Posts  All Forums:Forum Nav:

c pipe program

post #1 of 7
Thread Starter 
okay so for an assignment i have to be able to do redirection, piping, and background processing....and some dos commands that are going to be used but switched at runtime with unix commands that are equivalent...

i am having issues which i have highlighted red...

the two main errors i have and have found is that when i know it is a pipe i will pipe the command then fork it...at the end i make sure the file descriptors are closed but it doesn't like me when i do it at the end of the if statement but it is okay being right after i do the execute command....

what i mean is this....if i do this code it errors at the lines in red..

Code:

          pipe(file_des);
                        if(fork()==0){
                                close(file_des[1]);
                                dup2(file_des[0], 0);
                                execvp(argv[0], argv);
                        }//end if statement
                        if((new_cid = fork()) == 0){
                                close(file_des[0]);
                                dup2(file_des[1], 1);
                                execvp(argv[3], argv+3);
                        }//end if statement
                        close(file_des[0]);
                        close(file_des[1]);
if i do this code it doesn't error at all....this is when i switch the close statements...
Code:
          pipe(file_des);
                        if(fork()==0){
                                close(file_des[1]);
                                dup2(file_des[0], 0);
                                execvp(argv[0], argv);
                                close(file_des[0]);
                        }//end if statement
                        if((new_cid = fork()) == 0){
                                close(file_des[0]);
                                dup2(file_des[1], 1);
                                execvp(argv[3], argv+3);
                                close(file_des[1]);
                        }//end if statement
here is the full source code for the entire piping program...i found i have some small glitches in running it....it also doesn't like the line where
dup2(file_des, 1); because it gives me a warning...this is in the redirection part...

Code:
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/file.h>
#include <sys/types.h>
#include <signal.h>

#define MAX_SIZE 80

int fg_id = 0, pid = 0, status = 0;

void sigchldHandler(){
        pid = wait(&status);
        if(pid == fg_id){
                fg_id = 0;
        }//end if statement
        signal(SIGCHLD, sigchldHandler);
}//end sigchldHandler

int main(int argc, char *argvv[]){
        char *dosCmd[] = {"exit","dir","md","type","sort", "del", "copy", ">", ">>", "&"},
             *unixCmd[] = {"exit","ls","mkdir","cat","sort","rm", "cp", ">", ">>", "&"};

        int file_des[2], i, z, forgrnd, redir, reder,file_ptr, new_cid, args;
        char command[MAX_SIZE], *delimenator = " \
", *argv[10];
        signal(SIGCHLD, sigchldHandler);

        while(1){
                i = redir = reder = file_ptr = new_cid = 0; forgrnd = 1;

                printf("terminal@piping> ");
                gets(command);

                if(command[0]==0) continue;
                argv[i] = strtok(command, delimenator);
                args++;

                while(argv[i] != NULL){
                        for(z = 0; (z < 10) && (strcmp(argv[i], dosCmd[z])!= 0); z++);
                        switch(z){
                                case 0: return 0; break;
                                case 1:
                                case 2:
                                case 3:
                                case 4:
                                case 5:
                                case 6: argv[i] = unixCmd[z]; break;
                                case 7: reder++; argv[i] = NULL; break;
                                case 8: redir++; argv[i] = NULL; break;
                                case 9: forgrnd = 0; break;
                        }//end switch statement
                        argv[++i] = strtok(NULL, delimenator);
                        args++;
                }//end while loop
                if(args > 2){
                        pipe(file_des);
                        if(fork()==0){
                                close(file_des[1]);
                                dup2(file_des[0], 0);
                                execvp(argv[0], argv);
                                close(file_des[0]);
                        }//end if statement
                        if((new_cid = fork()) == 0){
                                close(file_des[0]);
                                dup2(file_des[1], 1);
                                execvp(argv[3], argv+3);
                                close(file_des[1]);
                        }//end if statement
                        else{
                                if((new_cid = fork()) == 0){
                                        if(reder > 0){
                                              file_ptr= open(argv[z+1],O_WRONLY|O_CREAT|O_TRUNC, 0666);
                                        }//end if statement
                                        if(redir > 0){
                                                file_ptr = open(argv[z+1], O_WRONLY|O_APPEND, 0666);
                                        }//end if statement
                                        if(file_des){
                                                dup2(file_des, 1);
                                        }//end if statement
                                        if((redir > 0)|| (reder > 0)){
                                                close(file_ptr);
                                        }//end if statement
                                        execvp(argv[0], argv);
                                }//end if statement
                        }//end else statement
                }//end if statement
                if(forgrnd){
                        fg_id = new_cid;
                        while(fg_id);
                }//end if statement
        }//end while loop
        return 0;
}//end main
Intel build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 gigabyte p55-ud6 gigabyte gv-n560oc-1gi Corsair Vengeance CMZ8GX3M2A1600C9 
Hard DriveHard DriveHard DriveOptical Drive
Crucial M4 WD Caviar Black WD Caviar Black LiteOn Lightscribe 24x 
CoolingOSMonitorMonitor
Thermaltake Frio Extreme CLP0587 Arch Linux x86_64 samsung 2243swx ASUS vs-248H-p 
KeyboardPowerCaseMouse
moditek led flex Seasonic 860Watt Platinum Antec Lanboy air razor death adder 
  hide details  
Reply
Intel build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 gigabyte p55-ud6 gigabyte gv-n560oc-1gi Corsair Vengeance CMZ8GX3M2A1600C9 
Hard DriveHard DriveHard DriveOptical Drive
Crucial M4 WD Caviar Black WD Caviar Black LiteOn Lightscribe 24x 
CoolingOSMonitorMonitor
Thermaltake Frio Extreme CLP0587 Arch Linux x86_64 samsung 2243swx ASUS vs-248H-p 
KeyboardPowerCaseMouse
moditek led flex Seasonic 860Watt Platinum Antec Lanboy air razor death adder 
  hide details  
Reply
post #2 of 7
Not entirely sure what you're going after as I do not use Unix, but you are double-closing a file handle, which I'm pretty sure you can't do in any OS:

pipe(file_des);
if(fork()==0){
close(file_des[1]);
dup2(file_des[0], 0);
execvp(argv[0], argv);
}//end if statement
if((new_cid = fork()) == 0){
close(file_des[0]);
dup2(file_des[1], 1);
execvp(argv[3], argv+3);
}//end if statement
close(file_des[0]);
close(file_des[1]);


Typically, you want to open all your files, do all your work, then close all the open handles. If for some performance reason you need to close handles as soon as you're done with them, then make sure you're not closing an already closed handle by keeping track of it some way. For instance, after closing a handle, set it to 0. Then subsequent closes can check to make sure the handles aren't 0 already (closed).
Edited by Kuntz - 12/13/08 at 6:00pm
Watercooler
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 D0 @ 4400MHz [24/7] X58 Classified 3870 Toxic Dominator GT 1680 [6-6-5-16] 
Hard DriveOptical DriveOSMonitor
7200.12 - 500GB [RAID 1] Plextor PX-7555A Windows 7 Samsung 2343BWX 2048x1152 
KeyboardPowerCaseMouse
Logitech G19 Corsair HX850W Silverstone TJ07 Logitech G9 
Mouse Pad
Fragmat 
  hide details  
Reply
Watercooler
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 D0 @ 4400MHz [24/7] X58 Classified 3870 Toxic Dominator GT 1680 [6-6-5-16] 
Hard DriveOptical DriveOSMonitor
7200.12 - 500GB [RAID 1] Plextor PX-7555A Windows 7 Samsung 2343BWX 2048x1152 
KeyboardPowerCaseMouse
Logitech G19 Corsair HX850W Silverstone TJ07 Logitech G9 
Mouse Pad
Fragmat 
  hide details  
Reply
post #3 of 7
Thread Starter 
the reason why i did that was because i am either just writing or just reading i have no need to have both open at the same time...for the child process i want to read, for the parent process i want to write.....

but i don't see any harm in leaving both open then closing both at the end out side of the conditional statements but before the else...
Intel build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 gigabyte p55-ud6 gigabyte gv-n560oc-1gi Corsair Vengeance CMZ8GX3M2A1600C9 
Hard DriveHard DriveHard DriveOptical Drive
Crucial M4 WD Caviar Black WD Caviar Black LiteOn Lightscribe 24x 
CoolingOSMonitorMonitor
Thermaltake Frio Extreme CLP0587 Arch Linux x86_64 samsung 2243swx ASUS vs-248H-p 
KeyboardPowerCaseMouse
moditek led flex Seasonic 860Watt Platinum Antec Lanboy air razor death adder 
  hide details  
Reply
Intel build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 gigabyte p55-ud6 gigabyte gv-n560oc-1gi Corsair Vengeance CMZ8GX3M2A1600C9 
Hard DriveHard DriveHard DriveOptical Drive
Crucial M4 WD Caviar Black WD Caviar Black LiteOn Lightscribe 24x 
CoolingOSMonitorMonitor
Thermaltake Frio Extreme CLP0587 Arch Linux x86_64 samsung 2243swx ASUS vs-248H-p 
KeyboardPowerCaseMouse
moditek led flex Seasonic 860Watt Platinum Antec Lanboy air razor death adder 
  hide details  
Reply
post #4 of 7
I'll take a peek. Been awhile since I used signals and fork in c.

Just an observation, it's really useless to create comments on (// end while, // end for ) and not on the meat of the code. Comment first, code 2nd!
GAMR
(15 items)
 
  
CPUMotherboardGraphicsGraphics
6600K ASUS Z170-A ASUS 970 Strix ASUS 970 Strix 
RAMHard DriveHard DriveHard Drive
Crucial Ballistix Elite Samsung 850 EVO Intel 320 6400AAKS 
Hard DriveCoolingMonitorPower
6400AAKS Cooler Master Hyper 212 EVO ASUS VG24QE EVGA SuperNova 650 G2 
CaseMouseMouse Pad
Antec 1200 Razer Abyssus 3.5G QCK+ 
  hide details  
Reply
GAMR
(15 items)
 
  
CPUMotherboardGraphicsGraphics
6600K ASUS Z170-A ASUS 970 Strix ASUS 970 Strix 
RAMHard DriveHard DriveHard Drive
Crucial Ballistix Elite Samsung 850 EVO Intel 320 6400AAKS 
Hard DriveCoolingMonitorPower
6400AAKS Cooler Master Hyper 212 EVO ASUS VG24QE EVGA SuperNova 650 G2 
CaseMouseMouse Pad
Antec 1200 Razer Abyssus 3.5G QCK+ 
  hide details  
Reply
post #5 of 7
I'm having trouble figuring out what it is the program is supposed to be doing. For instance, you're forking at least twice. I haven't quite figured out why.
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
post #6 of 7
After thinking about your code and your goal. You should probably try and forget forking the way you are. The way you are forking here doesn't seem optimal and potentially you have jobs completing out of order. If your goal is to ultimately pipe things together or background them I'd suggest using waitpid()

Also, you are doing no error handling in your code. What happens when pipe() or fork() fails?
GAMR
(15 items)
 
  
CPUMotherboardGraphicsGraphics
6600K ASUS Z170-A ASUS 970 Strix ASUS 970 Strix 
RAMHard DriveHard DriveHard Drive
Crucial Ballistix Elite Samsung 850 EVO Intel 320 6400AAKS 
Hard DriveCoolingMonitorPower
6400AAKS Cooler Master Hyper 212 EVO ASUS VG24QE EVGA SuperNova 650 G2 
CaseMouseMouse Pad
Antec 1200 Razer Abyssus 3.5G QCK+ 
  hide details  
Reply
GAMR
(15 items)
 
  
CPUMotherboardGraphicsGraphics
6600K ASUS Z170-A ASUS 970 Strix ASUS 970 Strix 
RAMHard DriveHard DriveHard Drive
Crucial Ballistix Elite Samsung 850 EVO Intel 320 6400AAKS 
Hard DriveCoolingMonitorPower
6400AAKS Cooler Master Hyper 212 EVO ASUS VG24QE EVGA SuperNova 650 G2 
CaseMouseMouse Pad
Antec 1200 Razer Abyssus 3.5G QCK+ 
  hide details  
Reply
post #7 of 7
Thread Starter 
k i am taking ur suggestion to do error checking...but all the forks are the framework that the professor gave me to use...

everyone else used this to make theirs work....i might have some issues with evaluation of keeping track of stuff...like where i am at in the character array...because it exits, it does list the directories...twice....but some things it cannot do like make a directory and then list the directory and it cannot run things in the background...suggesting to me that it is something wrong with how i duplicate stuff or maybe closing the pipes at the right time...
Intel build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 gigabyte p55-ud6 gigabyte gv-n560oc-1gi Corsair Vengeance CMZ8GX3M2A1600C9 
Hard DriveHard DriveHard DriveOptical Drive
Crucial M4 WD Caviar Black WD Caviar Black LiteOn Lightscribe 24x 
CoolingOSMonitorMonitor
Thermaltake Frio Extreme CLP0587 Arch Linux x86_64 samsung 2243swx ASUS vs-248H-p 
KeyboardPowerCaseMouse
moditek led flex Seasonic 860Watt Platinum Antec Lanboy air razor death adder 
  hide details  
Reply
Intel build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 gigabyte p55-ud6 gigabyte gv-n560oc-1gi Corsair Vengeance CMZ8GX3M2A1600C9 
Hard DriveHard DriveHard DriveOptical Drive
Crucial M4 WD Caviar Black WD Caviar Black LiteOn Lightscribe 24x 
CoolingOSMonitorMonitor
Thermaltake Frio Extreme CLP0587 Arch Linux x86_64 samsung 2243swx ASUS vs-248H-p 
KeyboardPowerCaseMouse
moditek led flex Seasonic 860Watt Platinum Antec Lanboy air razor death adder 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming