Thread: Creating processes

  1. #1
    Registered User
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    DJGPP + gcc, and Win XP.

  4. #4
    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
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    I tried MSYS but the forking doesn't suceed also.

  6. #6
    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-28-2005, 12:59 AM
  5. creating n monitoring 5 processes (notepad)
    By nishzone in forum Windows Programming
    Replies: 3
    Last Post: 08-24-2003, 09:03 PM