Quantcast

Maximum PC

It is currently Wed Nov 26, 2014 6:10 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: A little help with programming in Linux... in C
PostPosted: Mon Oct 26, 2009 8:05 pm 
Smithfield
Smithfield

Joined: Sun Jun 18, 2006 7:37 pm
Posts: 5466
Just so I can get this out of the way, yes it's for class. But no, I'm not asking you to do all of it. I just need help with this problem.

I'm basically creating a shell that coincides with my OS class, and it should have the basic features of a regular shell. The only problem I'm having with is running the the following commands:

< /home/ma/cs/Data1/input1 > your.outputb tr a-z A-Z &
>your.outputf</home/ma/cs/Data1/input1 tr a-z A-Z

This is injected into my program as a typical input redirect (i.e., p2 < input). My program does the first line correctly. The second part? Not so much. It leaves a blank file half the time. Sometimes it works, sometimes it doesn't. But I need this to work all the time. My execution part of the code is as follows (the redirects work so I know it can't be that)

Code:
//Fork shell and execute command
            if( (kidpid = fork ()) == 0) //CHILD PROCESS
            {
               for( i = 0; i < c ; i ++)
               {
                  if(compare(argv[i], "\0"))
                     break;
                  newargv[i] = argv[i];
               }
               newargv[c] = NULL; //Last argv should be nulled
               if((execvp(newargv[0], newargv))<0)
               {
                  perror("Could not execute");
               }
               exit(1);
            }
            else if (kidpid == -1) //FORK ERROR
            {
               perror("Cannot fork\n");
            }
            else //PARENT PROCESS
            {

               if(*background == 0)
               {   
                  wait(&child_pid);
                  dup2(std_in, STDIN_FILENO);
                  dup2(std_out, STDOUT_FILENO);   

               }
               else
               {         
                  dup2(std_in, STDIN_FILENO);
                  dup2(std_out, STDOUT_FILENO);         
                  x = 255;
                  int directory = 0;
                  for(i = 0; i < STORAGE ; i++)
                  {
                     if ( argv[0][i] == '\0')
                        break;
                     else if(argv[0][i] == '/')
                     {
                        directory = 1;
                        x = i+1;
                     }
                  }
                  for(i = 0; i < STORAGE ; i++)
                  {
                     if(x+i == STORAGE)
                        break;
                     else if ( directory == 1)
                        argv[0][i] = argv[0][x+i];
                  }
                  printf("[ %s ]\t[ %d ]\n",argv[0],getppid());
               }
            }
            
         }   


Apparently this program is very finnicky depending on where its run. I developed this in Ubuntu, only to realize that once it's uploaded to the server that it needs to run on, it works 90% correctly. So if you notice anything strange, that's why.

Also the part of the code that starts with x = 255; was a design spec... Unless someone else can tell me a quick and dirty way to get the process name.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Nov 11, 2009 12:12 am 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
"It leaves a blank file half the time. Sometimes it works, sometimes it doesn't. "

I don't have time to review your code at the moment, but it sounds like you may have forgot to flush the file buffer.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.