Creating processes

This is a discussion on Creating processes within the Linux Programming forums, part of the Platform Specific Boards category; Hi, there. I have a small assignment: to make a little command line shell that executes programs. The shell is ...

  1. #1
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722

    Creating processes

    Hi, there. I have a small assignment: to make a little command line shell that executes programs. The shell is ok. My problem is to run processes. The code has to be 100% POSIX compatible.
    So I thought using execv(...)
    I've been reading the FAQs but don't understand well how to spawn the process. The shell is running then executes the progm. While the program is running the shell must remain idle. When the program ends, the shell must return. Could you please explain a bit better you execv works?? It seems that it spawns a process independent from the parent, but I'd like the parent to remain idle.

    //EDIT:
    I've been reading a bit more, and I found that calling system is the same as calling fork(), execv(), and wait(). But calling fork() always returns -1. Does this happens because I'm using windows??

    Code:
    #include<limits.h>
    #include<stdio.h>
    #include<string.h>
    #include<unistd.h>
    
    #define CMDSZ 512
    #define N_TOKENS 64
    
    int main(){
    	char cmd[CMDSZ];
    	char *tkns[N_TOKENS+1];
    	char curr_dir[PATH_MAX];
    	int curr_dir_len;
    	int i, k,len;
    	int pid;
    	
    	getcwd(curr_dir,PATH_MAX);
    	curr_dir_len = strlen(curr_dir);
    	if(curr_dir[curr_dir_len-1]!='/'){
    		curr_dir[curr_dir_len++]='/';
    		curr_dir[curr_dir_len]=0;
    	}
    		
    	while(1){
    		printf("minish > ");
    		fgets(cmd,CMDSZ,stdin);
    		
    		tkns[0]=strchr(cmd,'\n');
    		if(tkns[0]) *(tkns[0])=0;		
    		len = strlen(cmd);
    		
    		k=i=0;
    		while(i<len && k<N_TOKENS){
    			if(cmd[i]==' '){
    				cmd[i++]=0;
    				while(cmd[i]==' ') i++;					
    			}else if(cmd[i]=='\"'){
    				i++;
    				tkns[k++]=cmd+i;
    				while(cmd[i]!='\"') i++;
    				cmd[i++]=0;
    			}else{
    				tkns[k++]=cmd+i;
    				while(cmd[i]!=' ') i++;
    				cmd[i++]=0;
    			}
    		}
    		tkns[k]=0;
    		
    		if(stricmp(tkns[0] , "quit")==0){
    			printf("mini-shell over.\n");
    			break;
    		}
    		
    		pid = fork();
    		if(pid<0){
    			fprintf(stderr,"fork() error.\n");
    			getchar();
    		}else if(pid==0){
    			strcat(curr_dir+curr_dir_len,tkns[0]);
    			if(execv(curr_dir,tkns)<0)
    				printf("Error executing '%s'\n",tkns[0]);
    		}else
    			wait();
    			
    		curr_dir[curr_dir_len]=0;
    			
    	}
    }
    Last edited by xErath; 10-08-2004 at 10:45 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    > Does this happens because I'm using windows??
    *boggle*
    Windows - POSIX - posted on the Linux board....

    If it's returning -1, look at the errno value to see if that tells you why...

    While you're at it, say which compiler you're using and which version of windows you're using.
    IIRC, there is a POSIX layer for windows, but it is an extra download and it only works with NT/2K/XP
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    DJGPP + gcc, and Win XP.

  4. #4
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    remember djgpp was written for the non-multitasking msdos enviornment. if you want to use gcc under windows and get all the cool stuff, use either mingw or cygwin

    http://www.delorie.com/djgpp/doc/lib.../libc_300.html
    hello, internet!

  5. #5
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    I tried MSYS but the forking doesn't suceed also.

  6. #6
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    gonna have to be cygwin then i guess
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 34
    Last Post: 05-27-2009, 12:26 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Creating makefiles
    By Loctan in forum Linux Programming
    Replies: 4
    Last Post: 10-06-2005, 12:19 AM
  4. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-27-2005, 11:59 PM
  5. creating n monitoring 5 processes (notepad)
    By nishzone in forum Windows Programming
    Replies: 3
    Last Post: 08-24-2003, 09:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21