Example #1
0
/*
 * Common code for cmd_prog and cmd_shell.
 *
 * Note that this does not wait for the subprogram to finish, but
 * returns immediately to the menu. This is usually not what you want,
 * so you should have it call your system-calls-assignment waitpid
 * code after forking.
 *
 * Also note that because the subprogram's thread uses the "args"
 * array and strings, until you do this a race condition exists
 * between that code and the menu input code.
 */
static
int
common_prog(int nargs, char **args)
{
	struct proc *proc;
	int result;

#if OPT_SYNCHPROBS
	kprintf("Warning: this probably won't work with a "
		"synchronization-problems kernel.\n");
#endif

	/* Create a process for the new program to run in. */
	proc = proc_create_runprogram(args[0] /* name */);
	if (proc == NULL) {
		return ENOMEM;
	}

	result = thread_fork(args[0] /* thread name */,
			proc /* new process */,
			cmd_progthread /* thread function */,
			args /* thread arg */, nargs /* thread arg */);
	if (result) {
		kprintf("thread_fork failed: %s\n", strerror(result));
		proc_destroy(proc);
		return result;
	}

	/*
	 * The new process will be destroyed when the program exits...
	 * once you write the code for handling that.
	 */ 

	return 0;
}
Example #2
0
int sys_fork(struct trapframe* tf, pid_t* retval) {
	int errcheck;    //this is to check if there's error in some funcitons
	//first creating a new proc
	struct proc* child = proc_create_runprogram("child123");
   //proctable_insert(child);
	if(child == NULL) {
		return ENOMEM;
	}
	curproc->child_pid = child->pid;
	curproc->child = child;      ///////////////
	child->parent = curproc;     //////////////
	
	//now, copying the address space
	struct addrspace* newas;
	errcheck = as_copy(curproc_getas(), &newas);
	if(errcheck != 0) {    //there's an error
		return errcheck;
	}
	child->p_addrspace = newas;
	
	//now copying the trapframe to the child
	struct trapframe* newtf = kmalloc(sizeof(struct trapframe));
	if(newtf == NULL) {
		return ENOMEM;
	}
	*newtf = *tf;
	//now, do the thread_fork
	errcheck = thread_fork("child2", child, enter_forked_process, newtf, 0);
	if(errcheck != 0) {
		return errcheck;
	}
	*retval = child->pid;
	return(0);
}
Example #3
0
/*
 * Common code for cmd_prog and cmd_shell.
 *
 * Note that this does not wait for the subprogram to finish, but
 * returns immediately to the menu. This is usually not what you want,
 * so you should have it call your system-calls-assignment waitpid
 * code after forking.
 *
 * Also note that because the subprogram's thread uses the "args"
 * array and strings, until you do this a race condition exists
 * between that code and the menu input code.
 */
static
int
common_prog(int nargs, char **args)
{
	struct proc *proc;
	int result;

	/* Create a process for the new program to run in. */
	proc = proc_create_runprogram(args[0] /* name */);
	if (proc == NULL) {
		return ENOMEM;
	}

	result = thread_fork(args[0] /* thread name */,
			proc /* new process */,
			cmd_progthread /* thread function */,
			args /* thread arg */, nargs /* thread arg */);
	if (result) {
		kprintf("thread_fork failed: %s\n", strerror(result));
		proc_destroy(proc);
		return result;
	}

	/*
	 * The new process will be destroyed when the program exits...
	 * once you write the code for handling that.
	 */

	return 0;
}
Example #4
0
int sys_fork(struct trapframe *tf, pid_t *retval){

  const char* name = "Anonymous student's process";


// create process struct for child
  struct proc *childp = proc_create_runprogram(name);
  if(childp == NULL){
    return ENOMEM; //OUT OF MEMORY
  }

// create and copy as to child
  struct addrspace* childas = kmalloc(sizeof(struct addrspace));
  if(childas == NULL){
    proc_destroy(childp);
    return ENOMEM;
  }

  if(as_copy(curproc->p_addrspace, &childas)){
    kfree(childas);
    as_destroy(childas);
    proc_destroy(childp);
    return ENOMEM;
  }
// attach new as to child proc
  childp->p_addrspace = childas;
  
// create parent child relationship
  struct procinfo *pi = array_get(procinfotable, childp->pid - 1);
  if(pi == NULL){
    kfree(childas);
    as_destroy(childas);
    proc_destroy(childp);
    return ECHILD; // NO SUCH CHILD IN THE INFO TABLE
  }
  pi->parent_pid = curproc->pid;

// create thread for child and pass it tf
  struct trapframe *childtf = kmalloc(sizeof(struct trapframe));
  memcpy(childtf,tf,sizeof(struct trapframe));

  void ** data = kmalloc(2*sizeof(void*));
  data[0] = (void*)childtf;
  data[1] = (void*)childas; 

  int err = thread_fork(name, childp,(void *)enter_forked_process, data, 0);
  if(err){
    kfree(childas);
    kfree(childtf);
    as_destroy(childas);
    proc_destroy(childp);
    kfree(childtf);
    return ENOMEM;
  }


  *retval = childp->pid;
  return 0;
}
Example #5
0
/*
 * Common code for cmd_prog and cmd_shell.
 *
 * Note that this does not wait for the subprogram to finish, but
 * returns immediately to the menu. This is usually not what you want,
 * so you should have it call your system-calls-assignment waitpid
 * code after forking.
 *
 * Also note that because the subprogram's thread uses the "args"
 * array and strings, until you do this a race condition exists
 * between that code and the menu input code.
 */
static
int
common_prog(int nargs, char **args)
{
	struct proc *proc;
	int result;
	int status;
	int retval;
	unsigned tc;

	/* Create a process for the new program to run in. */
	proc = proc_create_runprogram(args[0] /* name */);
	if (proc == NULL) {
		return ENOMEM;
	}

	tc = thread_count;

	result = thread_fork(args[0] /* thread name */,
			proc /* new process */,
			cmd_progthread /* thread function */,
			args /* thread arg */, nargs /* thread arg */);



	if (result) {
		kprintf("thread_fork failed: %s\n", strerror(result));
		proc_destroy(proc);
		return result;
	}
	result = sys_waitpid(proc->pid, &status, 0 , &retval);
	if (result ) {
		kprintf_n("sys_waitpid done with result %d for pid %d \n",result,proc->pid );
		//return 0;
	}

	/*
	 * The new process will be destroyed when the program exits...
	 * once you write the code for handling that.
	 */

	// Wait for all threads to finish cleanup, otherwise khu be a bit behind,
	// especially once swapping is enabled.
	thread_wait_for_count(tc);

	return 0;
}
Example #6
0
/*
 * Common code for cmd_prog and cmd_shell.
 *
 * Note that this does not wait for the subprogram to finish, but
 * returns immediately to the menu. This is usually not what you want,
 * so you should have it call your system-calls-assignment waitpid
 * code after forking.
 *
 * Also note that because the subprogram's thread uses the "args"
 * array and strings, until you do this a race condition exists
 * between that code and the menu input code.
 */
static
int
common_prog(int nargs, char **args)
{
	struct proc *proc;
	int result;

#if OPT_SYNCHPROBS
	kprintf("Warning: this probably won't work with a "
		"synchronization-problems kernel.\n");
#endif

	/* Create a process for the new program to run in. */
	proc = proc_create_runprogram(args[0] /* name */);
	if (proc == NULL) {
		return ENOMEM;
	}

        pid_t cpid = 0;
        int error = 0;
        error = proc_getAndCreateNewPid(&cpid);
        if (error)
        {
            proc_destroy(proc);
            return ENPROC;
        }
        proc->pid = cpid;
        proc_exitCodeNotNeeded(cpid);

	result = thread_fork(args[0] /* thread name */,
			proc /* new process */,
			cmd_progthread /* thread function */,
			args /* thread arg */, nargs /* thread arg */);
	if (result) {
		kprintf("thread_fork failed: %s\n", strerror(result));
		proc_destroy(proc);
		return result;
	}

#ifdef UW
	/* wait until the process we have just launched - and any others that it 
	   may fork - is finished before proceeding */
	P(no_proc_sem);
#endif // UW

	return 0;
}
Example #7
0
pid_t sys_fork(struct trapframe * tf,int *retval)
{
  int res = 0;
  int index = 0;
  char *forkedName = kmalloc(sizeof(char) *NAME_MAX);
  strcpy(forkedName, curproc->p_name);
  strcat(forkedName, "_fork");
  struct proc * childProc = proc_create_runprogram(forkedName);
  struct addrspace * childsSpace = kmalloc(sizeof(struct addrspace));
  res = as_copy(curproc->p_addrspace, &childsSpace);
  //kprintf("curproc->pid  = %d\n", (int)curproc->pid);
  if(res) {
    kprintf(" WE ARE FORKING ");
  }
  childProc-> p_addrspace = childsSpace;
 struct trapframe *tf_child = kmalloc(sizeof(struct trapframe));
  if(tf_child == NULL){
    kprintf(" trapframe PROBLEM  ");
  }
 memcpy(tf_child, tf, sizeof(struct trapframe));
  //*tf_child = *tf;
 //kprintf("cant copy mem ");
  for(int i = 0; i < 65536; i++)
  {
    if(!process_table[i])
    {
      childProc->pid = i;
      index = i;
      break;
    }
  }
  //kprintf("cant set pid ");
  childProc->ppid = curproc->pid;
  struct proc_entry * pentry = proc_entry_create(childProc);
  process_table[index] = pentry;

  res  = thread_fork(forkedName, childProc, entrypoint, (struct trapframe *) tf_child, (unsigned long)childsSpace);
  *retval = childProc->pid;
  return(0);
}