Exemplo n.º 1
0
int
clone(void)
{
  char* stack;
  int i, pid, size;
  struct proc *np;
  cprintf("a\n");
  // Allocate process.
  if((np = allocproc()) == 0)
    return -1;

  // Point page dir at parent's page dir (shared memory)
  np->pgdir = proc->pgdir;
  // This might be an issue later.
  np->sz = proc->sz;
  np->parent = proc;
  *np->tf = *proc->tf;
  
  if(argint(1, &size) < 0 || size <= 0 || argptr(0, &stack, size) < 0) {
    kfree(np->kstack);
    np->kstack = 0;
    np->state = UNUSED;
    return -1;
  }
<<<<<<< HEAD
Exemplo n.º 2
0
//PAGEBREAK: 32
// Set up first user process.
void
userinit(void)
{
  struct proc *p;
  extern char _binary_initcode_start[], _binary_initcode_size[];

  p = allocproc();
  
  initproc = p;
  if((p->pgdir = setupkvm()) == 0)
    panic("userinit: out of memory?");
  inituvm(p->pgdir, _binary_initcode_start, (int)_binary_initcode_size);
  p->sz = PGSIZE;
  memset(p->tf, 0, sizeof(*p->tf));
  p->tf->cs = (SEG_UCODE << 3) | DPL_USER;
  p->tf->ds = (SEG_UDATA << 3) | DPL_USER;
  p->tf->es = p->tf->ds;
  p->tf->ss = p->tf->ds;
  p->tf->eflags = FL_IF;
  p->tf->esp = PGSIZE;
  p->tf->eip = 0;  // beginning of initcode.S

  safestrcpy(p->name, "initcode", sizeof(p->name));
  p->cwd = namei("/");

  // this assignment to p->state lets other cores
  // run this process. the acquire forces the above
  // writes to be visible, and the lock is also needed
  // because the assignment might not be atomic.
  acquire(&ptable.lock);

  p->state = RUNNABLE;

  release(&ptable.lock);
}
// Create a new process copying p as the parent.
// Sets up stack to return as if from system call.
// Caller must set state of returned proc to RUNNABLE.
int
fork(void)
{
  int i, pid;
  struct proc *np;

  // Allocate process.
  if((np = allocproc()) == 0)
    return -1;

  // Copy process state from p.
  if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
    kfree(np->kstack);
    np->kstack = 0;
    np->state = UNUSED;
    return -1;
  }
  np->sz = proc->sz;
  np->sz2=proc->sz2;
  np->parent = proc;
  *np->tf = *proc->tf;

  // Clear %eax so that fork returns 0 in the child.
  np->tf->eax = 0;

  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = filedup(proc->ofile[i]);
  np->cwd = idup(proc->cwd);
 
  pid = np->pid;
  np->state = RUNNABLE;
  safestrcpy(np->name, proc->name, sizeof(proc->name));
  return pid;
}
Exemplo n.º 4
0
//PAGEBREAK: 32
// Set up first user process.
void
userinit(void)
{
  struct proc *p;
  extern char _binary_initcode_start[], _binary_initcode_size[];
  
  p = allocproc();
  initproc = p;
  if((p->pgdir = setupkvm()) == 0)
    panic("userinit: out of memory?");
  inituvm(p->pgdir, _binary_initcode_start, (int)_binary_initcode_size);
  p->sz = PGSIZE;
  memset(p->tf, 0, sizeof(*p->tf));
  p->tf->cs = (SEG_UCODE << 3) | DPL_USER;
  p->tf->ds = (SEG_UDATA << 3) | DPL_USER;
  p->tf->es = p->tf->ds;
  p->tf->ss = p->tf->ds;
  p->tf->eflags = FL_IF;
  p->tf->esp = PGSIZE;
  p->tf->eip = 0;  // beginning of initcode.S

  safestrcpy(p->name, "initcode", sizeof(p->name));
  p->cwd = namei("/");

  p->state = RUNNABLE;
}
int
clone(void(*fcn)(void*), void *arg, void *stack)
{
  int i, pid;
  struct proc *np;
  uint * sp;

// ALLOCATES A NEW THREAD //

  // Allocate process.
  if((np = allocproc()) == 0)
    return -1;

  //if a thread calls clone it makes its parent the parent of the new thread
  if(proc->isThread == 1){
    np->parent = proc->parent;
    *np->tf = *proc->parent->tf;
    np->sz = proc->parent->sz;
    np->pgdir = proc->parent->pgdir;
  }
  else{  
    np->parent = proc;
    *np->tf = *proc->tf;
    np->sz = proc->sz;
    np->pgdir = proc->pgdir;
  }
  
  np->isThread = 1;
  np->state = RUNNABLE;
  // Clear %eax so that fork returns 0 in the child.
  np->tf->eax = 0;

  pid = np->pid;

  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = filedup(proc->ofile[i]);
  np->cwd = idup(proc->cwd);


  safestrcpy(np->name, proc->name, sizeof(proc->name));
  //safestrcpy(np->name, "ThreadChildren", sizeof("ThreadChildren"));

  // Push argument strings, prepare rest of stack in ustack.
  sp = (uint *) ((char *)stack + PGSIZE);
  sp -= 2;
  sp[0] = 0xffffffff;  // fake return PC
  sp[1] = (uint) arg;

  // Commit to the user image.s
  np->tf->eip = (uint) fcn;  // main
  np->tf->esp = (uint) sp;

  //cprintf("[proc clone]-pid-%d\n",pid);
  switchuvm(np);
  //return pid on success not 0 yo
  return pid;
}
Exemplo n.º 6
0
int clone(void(*fcn)(void*), void *arg, void *stack) { // FIX THIS !
  int pid;
  struct proc *newtask;
  // Copy process state from p.
  /*if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
    kfree(np->kstack);
    np->kstack = 0;
    np->state = UNUSED;
    return -1;
  }*/
  
  // Allocate process.
  if((newtask = allocproc()) == 0)
    return -1;
  newtask->isThread = 1; // labelling as thread
  newtask->sz = proc->sz;
  newtask->parent = proc;
  *newtask->tf = *proc->tf;

  // Clear %eax so that clone returns 0 in the child.
  newtask->tf->eax = 0;

  /*for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = filedup(proc->ofile[i]);
  np->cwd = idup(proc->cwd);*/
  safestrcpy(newtask->name, proc->name, sizeof(proc->name));
 
  pid = newtask->pid;

  // lock to force the compiler to emit the np->state write last.
  /*
  acquire(&ptable.lock);
  np->state = RUNNABLE;
  release(&ptable.lock);*/
  
  
   // temporary array to copy into the bottom of new stack 
  // for the thread (i.e., to the high address in stack
  // page, since the stack grows downward)
  uint ustack[2];
  uint sp = (uint)stack+PGSIZE;
  ustack[0] = 0xffffffff; // fake return PC
  ustack[1] = (uint)arg;

  sp -= 8; // stack grows down by 2 ints/8 bytes
  if (copyout(newtask->pgdir, sp, ustack, 8) < 0) {
    // failed to copy bottom of stack into new task
    return -1;
  }
  newtask->tf->eip = (uint)fcn;
  newtask->tf->esp = sp;
  switchuvm(newtask);
  newtask->state = RUNNABLE;
  
  return pid;
}
Exemplo n.º 7
0
int clone(void(*fcn) (void*), void *arg, void *stack) {
    int i, pid;
    struct proc *np;

    if ((uint) stack % PGSIZE != 0) {
        return -1;
    }

    if ((uint) stack + PGSIZE > proc->sz) {
        return -1;
    }


    if ((np = allocproc()) == 0) {
        cprintf("can't alloc\n");
        return -1;
    }


    np->isThread = 1;
    np->stack = stack;
    np->pgdir = proc->pgdir;
    np->sz = proc->sz;
    np->parent = proc;
    *np->tf = *proc->tf;
    np->tf->eax = 0;
    np->tf->eip = (uint) fcn;

    //make array of args
    uint stack_val;
    uint stack_arr[2];
    stack_arr[0] = 0xffffffff;
    stack_arr[1] = (uint) arg;
    stack_val = (uint) stack + PGSIZE;
    stack_val -= 2 * sizeof(uint);


    //copied from exec.c
    if (copyout(np->pgdir, stack_val, stack_arr, 2 * sizeof(uint)) < 0) {
        return -1;
    }

    np->tf->esp = (uint) stack_val;

    //copy open files
    for(i = 0; i < NOFILE; i++)
        if(proc->ofile[i])
            np->ofile[i] = filedup(proc->ofile[i]);
    np->cwd = idup(proc->cwd);

    np->pid = proc->pid;
    pid = np->pid;
    np->state = RUNNABLE;
    safestrcpy(np->name, proc->name, sizeof(proc->name));
    return pid;
}
Exemplo n.º 8
0
int 
clone(void(*func)(void*), void* arg, void* stack)
{
  int i, pid;
  struct proc *np;
   
  //input arguments
  
  if((uint)stack + PGSIZE > proc->sz)
    return -1;
  if((uint)stack%PGSIZE != 0)
    return -1;
  
  // Allocate process.
  if((np = allocproc()) == 0)
    return -1;

  // Copy process state from p.
  np->pgdir = proc->pgdir;
   
  //set up the stack
  void* stackTop = stack+PGSIZE;
  *(uint*)(stackTop-8) = 0xffffffff;
  //get the input arg into stacktop -4
  *(uint*)(stackTop-4) = (uint)arg;

  np->sz = proc->sz;
  np->parent = proc;
  *np->tf = *proc->tf;

  //This is a thread;
  np->thread = 1;
  np->ustack = stack;
  
  // Clear %eax so that fork returns 0 in the child.
  np->tf->eax = 0;
  //set the esp and eip
  np->tf->eip = (uint)func;
  np->tf->esp = (uint)(stackTop - 8);

  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = filedup(proc->ofile[i]);

  safestrcpy(np->name, proc->name, sizeof(proc->name));
 
  pid = np->pid;
  
  np->cwd = idup(proc->cwd);
  // lock to force the compiler to emit the np->state write last.
  acquire(&ptable.lock);
  np->state = RUNNABLE;
  release(&ptable.lock);
  return pid;
}
Exemplo n.º 9
0
Arquivo: proc.c Projeto: sding3/CS537
int
clone(void(*fcn)(void*), void *arg, void *stack){
  int i, pid;
  struct proc *np;
  int size = 4096;

  if((np = allocproc()) == 0)
     return -1;

  proc->children++;

  np->pgdir = proc->pgdir;
  np->sz = proc->sz;
  np->parent = proc;
  np->parentind = proc->ind;
  np->tid = proc->tid;
  *np->tf = *proc->tf;

  
  np->tf->eax = 0;
  
   
  void *tmp = arg;
  memmove(stack+size-4,&tmp,4);
  //cprintf("clone: arg is put at mem addr %d\n",stack+size-8);
  memset(stack+size-8,(int)0xFF,4);
  //memset(stack+size-12,(int)proc->tf->ebp,4);
  np->tf->esp = (uint)stack+size-8; //why esp!=ebp, i thought they needed to be =
  np->tf->ebp = (uint)stack+size-12;
  np->tf->eip = (int) fcn;
  np->joinstack = stack;
  
  /*
  uint stackSize = *(uint *)proc->tf->ebp - proc->tf->esp;
  np->tf->esp = (uint)stack+size - stackSize;
  uint topSize = *(uint *)proc->tf->ebp - proc->tf->ebp;
  np->tf->ebp = (uint)stack+size - topSize;
  memmove((void *)(np->tf->esp),(const void *)(proc->tf->esp), stackSize);
  np->tf->eip = (int) fcn;
  */

  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = filedup(proc->ofile[i]);
  np->cwd = idup(proc->cwd);

  pid = np->pid;
  np->state = RUNNABLE;
  safestrcpy(np->name, proc->name, sizeof(proc->name));


  return pid;
}
Exemplo n.º 10
0
// Create a new process copying p as the parent.
// Sets up stack to return as if from system call.
// Caller must set state of returned proc to RUNNABLE.
int
fork(void)
{
  int i, pid;
  struct proc *np;

  // Allocate process.
  if((np = allocproc()) == 0)
    return -1;

  // Copy process state from p.
  if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
    kfree(np->kstack);
    np->kstack = 0;
    np->state = UNUSED;
#ifdef CS333_SCHEDULER
    acquire(&ptable.lock);
    putOnFreeList(np);
    release(&ptable.lock);
#endif
    return -1;
  }
  np->sz = proc->sz;
  np->parent = proc;
  *np->tf = *proc->tf;

    //Set guid and uid to np from proc
    np->gid = proc->gid;
    np->uid = proc->uid;
    np->priority = DEFAULTPRIO;

  // Clear %eax so that fork returns 0 in the child.
  np->tf->eax = 0;

  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = filedup(proc->ofile[i]);
  np->cwd = idup(proc->cwd);

  safestrcpy(np->name, proc->name, sizeof(proc->name));
 
  pid = np->pid;

  // lock to force the compiler to emit the np->state write last.
  acquire(&ptable.lock);
  np->state = RUNNABLE;
#ifdef CS333_SCHEDULER
  putOnReadyList(np, np->priority);
#endif
  release(&ptable.lock);
  
  return pid;
}
Exemplo n.º 11
0
int clone(void(*fcn)(void*), void *arg, void *stack) {
  int i, pid;
	//cprintf("proc.c void *arg print: %d\n", &arg);
  struct proc *newtask;
	// Allocate process.
  if ((newtask = allocproc()) == 0) return -1;
  
  //newtask->kstack = stack;
  newtask->isThread = 1; // labelling as thread
  newtask->sz = proc->sz;
	if (proc->isThread) {
		newtask->parent = proc->parent;
	}
	else {
  	newtask->parent = proc;
	}
  *newtask->tf = *proc->tf;
  newtask->pgdir = proc->pgdir;

  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      newtask->ofile[i] = filedup(proc->ofile[i]);
  newtask->cwd = idup(proc->cwd);

  safestrcpy(newtask->name, proc->name, sizeof(proc->name));

  // Clear %eax so that clone returns 0 in the child.
  newtask->tf->eax = 0;
  pid = newtask->pid;
  
  
  // temporary array to copy into the bottom of new stack 
  // for the thread (i.e., to the high address in stack
  // page, since the stack grows downward)
  uint ustack[2];
  uint sp = (uint)stack+PGSIZE;
  ustack[0] = 0xffffffff; // fake return PC
  ustack[1] = (uint)arg;

  sp -= 8; // stack grows down by 2 ints/8 bytes
  if (copyout(newtask->pgdir, sp, ustack, 8) < 0) {
    // failed to copy bottom of stack into new task
    cprintf("I'm crashing...");
    return -1;
  }
  newtask->tf->eip = (uint)fcn;
  newtask->tf->esp = sp;
  switchuvm(newtask);
  newtask->state = RUNNABLE;
  
  return pid;
}
Exemplo n.º 12
0
// Create a new process copying p as the parent.
// Sets up stack to return as if from system call.
// Caller must set state of returned proc to RUNNABLE.
int
fork(void)
{
  int i, pid;
  struct proc *np;

  // Allocate process.
  if((np = allocproc()) == 0)
    return -1;

  // Copy process state from p.
  if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
    kfree(np->kstack);
    np->kstack = 0;
    np->state = UNUSED;
#ifdef USE_CS333_SCHEDULER
    acquire(&ptable.lock);
    addToFreeList(np);
    release(&ptable.lock);
#endif
    return -1;
  }
  np->sz = proc->sz;
  np->parent = proc;
  *np->tf = *proc->tf;

  // Clear %eax so that fork returns 0 in the child.
  np->tf->eax = 0;

  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = filedup(proc->ofile[i]);
  np->cwd = idup(proc->cwd);

  safestrcpy(np->name, proc->name, sizeof(proc->name));
 
  pid = np->pid;
  np->uid = np->parent->uid;
  np->gid = np->parent->gid;

  // lock to force the compiler to emit the np->state write last.
  acquire(&ptable.lock);
  np->state = RUNNABLE;
#ifdef USE_CS333_SCHEDULER
  if (!setPri(np, DEF_PRI))
    cprintf("ERROR: DEF_PRI invalid. Must be between 0 and %d. Current value: %d.\n", N_PRI, DEF_PRI);
  addToPriQ(np, np->pri);
#endif
  release(&ptable.lock);
  
  return pid;
}
Exemplo n.º 13
0
int clone(void (*fcn)(void*), void *arg, void *stack)
{
    int i, pid;
    struct proc *np;

    if ((int)stack % PGSIZE != 0) {
        return -1;
    }
    if ((np = allocproc()) == 0) {
        return -1;
    }

    np->pgdir = proc->pgdir;
    np->sz = proc->sz;

    if (proc->is_thread == 1) {
        np->parent = proc->parent;
    }
    else {
        np->parent = proc;
    }
    *np->tf = *proc->tf;
    np->is_thread = 1;
    np->tf->eax = 0;

    for(i = 0; i < NOFILE; i++) {
        if(proc->ofile[i]) {
            np->ofile[i] = filedup(proc->ofile[i]);
        }
    }
    np->cwd = idup(proc->cwd);

    safestrcpy(np->name, proc->name, sizeof(proc->name));
    pid = np->pid;

    uint ustack[2];
    uint sp = (uint)stack+PGSIZE;
    ustack[0] = 0xffffffff;
    ustack[1] = (uint)arg;

    sp -= 8;
    if (copyout(np->pgdir, sp, ustack, 8) < 0) {
        return -1;
    }

    np->tf->eip = (uint)fcn;
    np->tf->esp = sp;
    switchuvm(np);
    np->state = RUNNABLE;
    return pid;
}
Exemplo n.º 14
0
int
thread_create(void (*tmain)(void *), void *stack, void *arg)
{
  int i, pid;
  struct proc *np;
  uint sp, ustack[3];

  // Allocate process.
  if((np = allocproc()) == 0)
    return -1;

  // Mimic exec.c
  sp = (uint)stack;
  sp = (sp - (strlen(arg) + 1)) & ~3;
  copyout(proc->pgdir, sp, arg, strlen(arg) + 1);
  ustack[1] = sp;
  ustack[2] = 0;

  ustack[0] = 0xffffffff;  // fake return PC

  sp -= 3*4;
  copyout(proc->pgdir, sp, ustack, 3*4);

  //print_stack(np->pid, (char*)stack, (char*)sp);

  // Copy process state from p.
  np->pgdir = proc->pgdir;
  np->sz = proc->sz;
  np->parent = proc;
  *np->tf = *proc->tf;

  np->tf->eip = (uint)(tmain);
  np->tf->esp = sp;

  // Clear %eax so that fork returns 0 in the child.
  np->tf->eax = 0;

  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = filedup(proc->ofile[i]);
  np->cwd = idup(proc->cwd);
 
  pid = np->pid;
  np->state = RUNNABLE;
  safestrcpy(np->name, proc->name, sizeof(proc->name));

  np->child_stack = stack;

  return pid;
}
Exemplo n.º 15
0
// Create a new process copying p as the parent.
// Sets up stack to return as if from system call.
// Caller must set state of returned proc to RUNNABLE.
int
fork(void)
{

  int i, pid;
  struct proc *np;

  // Allocate process.
  if((np = allocproc()) == 0)
    return -1;

  // Copy process state from p.
  if(!(np->pgdir = copyuvm(proc->pgdir, proc->sz))){
    kfree(np->kstack);
    np->kstack = 0;
    np->state = UNUSED;
    return -1;
  }
  np->sz = proc->sz;
  np->parent = proc;
  *np->tf = *proc->tf;
   

  // Clear %eax so that fork returns 0 in the child.
  np->tf->eax = 0;

  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = filedup(proc->ofile[i]);
  np->cwd = idup(proc->cwd);
 
  pid = np->pid;
  np->state = RUNNABLE;
  
  //START HW4
  // priority and affinity
  np->affinity = proc->affinity;
  np->priority = proc->priority;
  
  acquire(&runqueue.lock); 
  cprintf("enqueuing forked process %d to queue %d, in cpu %d\n", np->pid, np->priority, cpu->id);
  enqueue( np->pid , np->priority );  //
 // print_queue( np->priority );
  release(&runqueue.lock); 
  // END HW4 
  
  safestrcpy(np->name, proc->name, sizeof(proc->name));
  return pid;
}
Exemplo n.º 16
0
// Create a new process copying p as the parent.
// Sets up stack to return as if from system call.
// Caller must set state of returned proc to RUNNABLE.
int
fork(void)
{
  int i, pid;
  struct proc *np;

  // Allocate process.
  if((np = allocproc()) == 0)
    return -1;

  // Copy process state from p.
  if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
    kfree(np->kstack);
    np->kstack = 0;
    np->state = UNUSED;

    //**** remove--this might be not what I want
    //cprintf("remove in fork\n");
    acquire(&ptable.lock);
    removefromq(np);
    release(&ptable.lock);
    //****

    return -1;
  }
  np->sz = proc->sz;
  np->parent = proc;
  *np->tf = *proc->tf;
  np->priority = proc->priority; // Initialize priority
  np->next = NULL;

  // Clear %eax so that fork returns 0 in the child.
  np->tf->eax = 0;

  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = filedup(proc->ofile[i]);
  np->cwd = idup(proc->cwd);
 
  pid = np->pid;
  np->state = RUNNABLE;
  
  //*** add
  addtoq(np);
  //***

  safestrcpy(np->name, proc->name, sizeof(proc->name));
  return pid;
}
Exemplo n.º 17
0
int kthread_create( void*(*start_func)(), void* stack, unsigned int stack_size )
{
int i, pid;
  struct proc *np;

  // Allocate process.
  if((np = allocproc()) == 0)
    return -1;
for(i=0;i<64;i++)
  {
      np->sleepingThreads[i]=0;
  }
 if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
   
    kfree(np->kstack);
    np->kstack = 0;
    np->state = UNUSED;
    
    
    return -1;
  }

nextThread++;
np->threadId=nextThread;
np->pid=proc->pid;
 np->pgdir=proc->pgdir;
  np->sz = proc->sz;
  np->parent = proc->parent;
  
  *np->tf = *proc->tf;
  np->tf->eip=(uint)start_func;
  np->tf->esp=(uint)stack+stack_size;

  // Clear %eax so that fork returns 0 in the child.
  np->tf->eax = 0;

  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = proc->ofile[i];
  np->cwd = proc->cwd;
 
  pid = np->pid;
  np->state = RUNNABLE;
  safestrcpy(np->name, "thread", sizeof(proc->name));
  return pid;



}
Exemplo n.º 18
0
// Create a new process copying p as the parent.
// Sets up stack to return as if from system call.
// Caller must set state of returned proc to RUNNABLE.
int
fork(void)
{
  int i, pid;
  struct proc *np;

  // Allocate process.
  if((np = allocproc()) == 0)
    return -1;

  // Copy process state from p.
  if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
    kfree(np->kstack);
    np->kstack = 0;
    np->state = UNUSED;
    return -1;
  }
  np->sz = proc->sz;
  np->parent = proc;
  np->procShmemCount=proc->procShmemCount;
  *np->tf = *proc->tf;
  
  for(i=0;i<4;i++)
  {
  	//cprintf("before at %d bitmap =%d\n",i,proc->bitmap[i]);
  	np->bitmap[i]=proc->bitmap[i];
//  	cprintf("after at %d bitmap =%d \n",i,proc->bitmap[i]);
  	//proc->bitmap[i];
  	if(proc->bitmap[i]!=-1)
  	{
  		inc_shmem_proc_count(i);
  	}
  	
  }
  

  // Clear %eax so that fork returns 0 in the child.
  np->tf->eax = 0;

  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = filedup(proc->ofile[i]);
  np->cwd = idup(proc->cwd);
 
  pid = np->pid;
  np->state = RUNNABLE;
  safestrcpy(np->name, proc->name, sizeof(proc->name));
  return pid;
}
Exemplo n.º 19
0
Arquivo: proc.c Projeto: vener91/cs537
//Thread library
int clone(void(*fcn)(void*), void *arg, void*stack){
	if((uint)stack == 0){
		return -1;
	}

  	int i, pid;
	struct proc *np;
	//struct proc *currProc = proc;

	if(proc == NULL)
		return -1;
  	// Allocate process. (copied from fork())
  	if((np = allocproc()) == 0)
		return -1;

	// Copy process state from currProc
	np->pgdir 	= proc->pgdir;
	np->sz 		= proc->sz;
	np->parent 	= proc;
	np->sz = proc->sz;
	np->parent = proc;
	*np->tf = *proc->tf;
	np->stack = stack;

	//creating virtual main
	np->tf->esp = (uint)(stack - sizeof(void*));
	void** targ;
	targ = (void*) np->tf->esp;
	*targ = arg;
	np->tf->esp = np->tf->esp - sizeof(void*);
	targ = (void*) np->tf->esp;
	*targ = (void*)0xffffffff;
	np->tf->eip = (uint)fcn; //Set return address to worker

	// Clear %eax so that fork returns 0 in the child.
	np->tf->eax = 0;

	for(i = 0; i < NOFILE; i++)
		if(proc->ofile[i])
			np->ofile[i] = filedup(proc->ofile[i]);
	np->cwd = idup(proc->cwd);

	pid = np->pid;
	np->state = RUNNABLE;
	safestrcpy(np->name, proc->name, sizeof(proc->name));
	return pid;
}
Exemplo n.º 20
0
Arquivo: proc.c Projeto: zvikadori/xv6
// Create a new process copying p as the parent.
// Sets up stack to return as if from system call.
// Caller must set state of returned proc to RUNNABLE.
int
fork(void)
{
  int i, pid;
  struct proc *np;

  // Allocate process.
  if((np = allocproc()) == 0)
    return -1;

  // Copy process state from p.
  if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
    kfree(np->kstack);
    np->kstack = 0;
    np->state = UNUSED;
    return -1;
  }
  np->sz = proc->sz;
  np->parent = proc;
  *np->tf = *proc->tf;
  
  // Clear %eax so that fork returns 0 in the child.
  np->tf->eax = 0;

  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = filedup(proc->ofile[i]);
  np->cwd = idup(proc->cwd);
 
  pid = np->pid;
  
  /* task 2.5 */
  
  np->pendingSignals = proc->pendingSignals;
  for (i = 0; i< NUM_OF_SIGNALS; i++)
	np->signalHandlers[i] = proc->signalHandlers[i];
  
  /* end task 2.5 */
  
  np->state = RUNNABLE;
  safestrcpy(np->name, proc->name, sizeof(proc->name));
  
  if (strncmp(proc->name, "sh", 3) == 0)
	lastShInvokedPid = np->pid;
  
  return pid;
}
Exemplo n.º 21
0
int thread_create(void (*tmain)(void *), void *stack, void *arg){

  //  PREVIOUS IMPLEMENTATION
  int i, pid;
  struct proc *np;
  uint maxidx = 31;
  uint ssize = 4*maxidx + 4;
  uint sp = (uint)stack + ssize; //XXX get size of stack?
  uint * ustack = (uint *)stack;

  // Allocate process.
  if((np = allocproc()) == 0)
    return -1;
  // Copy process state from p.
  np->sz = proc->sz;
  np->pgdir = proc->pgdir;	//Same page table for both parent and child
  np->parent = proc;
  *np->tf = *proc->tf;	//trap frame is -almost- the same

  //user stack init
  ustack[maxidx - 0] = 0xffffffff;        // fake return PC
  ustack[maxidx - 1] = 1;                 // only one argument
  ustack[maxidx - 2] = (uint)arg;         // argv pointer
  ustack[maxidx - 3] = sp;
  ustack[maxidx - 4] = 0;
  sp -= 4*4;
  np->tf->esp = sp;
  np->tf->ebp = (uint)ustack + 4;
  np->tf->eip = (uint)(tmain);

  // Same file handles for the child thread
  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = proc->ofile[i];
  np->cwd = idup(proc->cwd);

  pid = np->pid;

  np->state = RUNNABLE;
  safestrcpy(np->name, proc->name, sizeof(proc->name)); // TODO name and count number of child threads
  // TODO copy of callcount
  np->isthread = 1;

  return pid;

}
Exemplo n.º 22
0
Arquivo: proc.c Projeto: cams93/os-xv6
// Create a new process copying p as the parent.
// Sets up stack to return as if from system call.
// Caller must set state of returned proc to RUNNABLE.
int
fork(void)
{
  int i, pid;
  struct proc *np;

  // Allocate process.
  if((np = allocproc()) == 0)
    return -1;

  // Copy process state from p.
  if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
    kfree(np->kstack);
    np->kstack = 0;
    np->state = UNUSED;
    return -1;
  }
  np->sz = proc->sz;
  np->parent = proc;
  *np->tf = *proc->tf;

  // Clear %eax so that fork returns 0 in the child.
  np->tf->eax = 0;

  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = filedup(proc->ofile[i]);
  np->cwd = idup(proc->cwd);

  safestrcpy(np->name, proc->name, sizeof(proc->name));

  pid = np->pid;

  // lock to force the compiler to emit the np->state write last.
  np->priority = 0; //cada vez que hacemos fork a un proceso le asignamos un cero de prioridad
  acquire(&ptable.lock);
  np->state = RUNNABLE;
  release(&ptable.lock);
  
  np->signals[0] = (sighandler_t*) -1;
  np->signals[1] = (sighandler_t*) -1;
  np->signals[2] = (sighandler_t*) -1;
  np->signals[3] = (sighandler_t*) -1;

  return pid;
}
Exemplo n.º 23
0
Arquivo: proc.c Projeto: yonatana/OS
int
thread_create(void*(*start_func)(), void* stack, uint stack_size)
{
  int tid,i;
  struct proc *np;

  // Allocate process.
  if((np = allocproc()) == 0)//tid was update in thread_allocproc
    return -1;

  np->pid = proc->pid;//copy brother id
  // Copy process state from p.
  np->pgdir = proc->pgdir;
  np->sz = proc->sz;
  if(proc->is_thread){//if the thread create thread then both have the same father
    np->parent = proc->parent;
  }
  else{//if procces create thread then it is his father
    np->parent = proc;
  }
  acquire(&ptable.lock);
  np->parent->num_of_thread_child++;
  np->tid =nexttid++;
  release(&ptable.lock);
  np->is_thread = 1;
  np->thread_joined = 0;
  //np->tid = ++(np->parent->tid);
 
  *np->tf = *proc->tf;
  // Clear %eax so that fork returns 0 in the child.
  np->tf->eax = 0;
  np->tf->esp = (uint)stack+stack_size;
  np->tf->eip = (uint)start_func;
  //np->ofile = (struct file *)proc->ofile;
  for(i = 0; i < NOFILE; i++){
    if(proc->ofile[i]){
      np->ofile[i] = filedup(proc->ofile[i]);
    }
  }
  
  np->cwd = proc->cwd;
  tid = np->tid;
  np->state = RUNNABLE;
  safestrcpy(np->name, proc->name, sizeof(proc->name));
  return tid;
}
Exemplo n.º 24
0
Arquivo: proc.c Projeto: aaronb/CS637
// Create a new process copying p as the parent.
// Sets up stack to return as if from system call.
// Caller must set state of returned proc to RUNNABLE.
struct proc*
copyproc(struct proc *p)
{
  int i;
  struct proc *np;

  // Allocate process.
  if((np = allocproc()) == 0)
    return 0;

  // Allocate kernel stack.
  if((np->kstack = kalloc(KSTACKSIZE)) == 0){
    np->state = UNUSED; 
    return 0;
  }
  np->tf = (struct trapframe*)(np->kstack + KSTACKSIZE) - 1;

  if(p){  // Copy process state from p.
    np->parent = p;
    memmove(np->tf, p->tf, sizeof(*np->tf));
  
    np->sz = p->sz;
    if((np->mem = kalloc(np->sz)) == 0){
      kfree(np->kstack, KSTACKSIZE);
      np->kstack = 0;
      np->state = UNUSED;
      np->parent = 0;
      return 0;
    }
    memmove(np->mem, p->mem, np->sz);

    for(i = 0; i < NOFILE; i++)
      if(p->ofile[i])
        np->ofile[i] = filedup(p->ofile[i]);
    np->cwd = idup(p->cwd);
  }

  // Set up new context to start executing at forkret (see below).
  memset(&np->context, 0, sizeof(np->context));
  np->context.eip = (uint)forkret;
  np->context.esp = (uint)np->tf;

  // Clear %eax so that fork system call returns 0 in child.
  np->tf->eax = 0;
  return np;
}
Exemplo n.º 25
0
// Create a new process copying p as the parent.
// Sets up stack to return as if from system call.
// Caller must set state of returned proc to RUNNABLE.
int
fork(void)
{
  int i, pid;
  struct proc *np;

  // Allocate process.
  if((np = allocproc()) == 0)
    return -1;
	np->threadId=-1;    //changed ass2* main thread of process
   
  // Copy process state from p.

  if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
    
    kfree(np->kstack);
    np->kstack = 0;
    np->state = UNUSED;
    
    return -1;
  }
  np->sz = proc->sz;
  np->parent = proc;
  *np->tf = *proc->tf;

  // Clear %eax so that fork returns 0 in the child.
  np->tf->eax = 0;

  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = filedup(proc->ofile[i]);
  np->cwd = idup(proc->cwd);
 
  pid = np->pid;
  np->state = RUNNABLE;
  safestrcpy(np->name, proc->name, sizeof(proc->name));
  /* changed ass2 nulling the threads array */
  for(i=0;i<64;i++)
  {
      np->sleepingThreads[i]=0;
  }
/* changed ass2 nulling the threads array */
  return pid;
}
Exemplo n.º 26
0
//PAGEBREAK: 32
// Set up first user process.
void
userinit(void)
{
  struct proc *p;
  extern char _binary_initcode_start[], _binary_initcode_size[];

#ifdef USE_CS333_SCHEDULER	// Initialize free list
  acquire(&ptable.lock);
  int i;
  for (i=0; i<NPROC; i++)
    addToFreeList(&ptable.proc[i]);
  release(&ptable.lock);
#endif
  p = allocproc();
  initproc = p;
  if((p->pgdir = setupkvm()) == 0)
    panic("userinit: out of memory?");
  inituvm(p->pgdir, _binary_initcode_start, (int)_binary_initcode_size);
  p->sz = PGSIZE;
  memset(p->tf, 0, sizeof(*p->tf));
  p->tf->cs = (SEG_UCODE << 3) | DPL_USER;
  p->tf->ds = (SEG_UDATA << 3) | DPL_USER;
  p->tf->es = p->tf->ds;
  p->tf->ss = p->tf->ds;
  p->tf->eflags = FL_IF;
  p->tf->esp = PGSIZE;
  p->tf->eip = 0;  // beginning of initcode.S

  safestrcpy(p->name, "initcode", sizeof(p->name));
  p->cwd = namei("/");
  p->uid = DEF_UID;
  p->gid = DEF_GID;

#ifdef USE_CS333_SCHEDULER	// Initialize ready list with init process
  acquire(&ptable.lock);
  p->state = RUNNABLE;
  if (!setPri(p, DEF_PRI))
    cprintf("ERROR: DEF_PRI invalid. Must be between 0 and %d. Current value: %d.\n", N_PRI, DEF_PRI);
  addToPriQ(p, p->pri);
  release(&ptable.lock);
#else
  p->state = RUNNABLE;
#endif
}
Exemplo n.º 27
0
// Create a new process copying p as the parent.
// Sets up stack to return as if from system call.
// Caller must set state of returned proc to RUNNABLE.
int
fork(void)
{
  int i, pid;
  struct proc *np;

  // Allocate process.
  if((np = allocproc()) == 0)
    return -1;

  // Copy process state from p.
  if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
    kfree(np->kstack);
    np->kstack = 0;
    np->state = UNUSED;
    return -1;
  }
  np->sz = proc->sz;
  np->parent = proc;
  np->uid = proc->uid;
  *np->tf = *proc->tf;

  // Clear %eax so that fork returns 0 in the child.
  np->tf->eax = 0;

  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = filedup(proc->ofile[i]);
  np->cwd = idup(proc->cwd);

  safestrcpy(np->name, proc->name, sizeof(proc->name));
  
  // Copy parent process' path to child process
  safestrcpy(np->wdpath, proc->wdpath, sizeof(proc->wdpath));
 
  pid = np->pid;

  // lock to force the compiler to emit the np->state write last.
  acquire(&ptable.lock);
  np->state = RUNNABLE;
  release(&ptable.lock);
  
  return pid;
}
Exemplo n.º 28
0
//PAGEBREAK: 32
// Set up first user process.
void
userinit(void)
{
  struct proc *p;
  extern char _binary_initcode_start[], _binary_initcode_size[];
#ifdef CS333_SCHEDULER
  acquire(&ptable.lock);
  initFreeList();
  ptable.timeToReset = COUNT;
  release(&ptable.lock);
#endif
  
  p = allocproc();
  initproc = p;
  if((p->pgdir = setupkvm()) == 0)
    panic("userinit: out of memory?");
  inituvm(p->pgdir, _binary_initcode_start, (int)_binary_initcode_size);
  p->sz = PGSIZE;
  memset(p->tf, 0, sizeof(*p->tf));
  p->tf->cs = (SEG_UCODE << 3) | DPL_USER;
  p->tf->ds = (SEG_UDATA << 3) | DPL_USER;
  p->tf->es = p->tf->ds;
  p->tf->ss = p->tf->ds;
  p->tf->eflags = FL_IF;
  p->tf->esp = PGSIZE;
  p->tf->eip = 0;  // beginning of initcode.S
  p->uid = USERID;
  p->gid = GROUPID;

  safestrcpy(p->name, "initcode", sizeof(p->name));
  p->cwd = namei("/");

  p->state = RUNNABLE;
#ifdef CS333_SCHEDULER
  acquire(&ptable.lock);
  int i;
  for (i = 0; i < 3; ++i) {
      ptable.readyList[i] = 0;
  }
  putOnReadyList(p, p->priority);
  release(&ptable.lock);
#endif
}
Exemplo n.º 29
0
int sys_writepid(struct proc* p){
struct proc* pp = getter(p->pid);
struct proc* np = p;
cprintf("\nhelloooooooo %d\n",np->pid);
cprintf(" %d\n",np->pgdir);
int i, pid;

  // Allocate process.
  if((np = allocproc()) == 0)
    return -1;
np->killed=0;

  // Copy process state from p.
  if((np->pgdir = copyuvm(pp->pgdir, pp->sz)) == 0){
    kfree(np->kstack);
    np->kstack = 0;
    np->state = UNUSED;
    return -1;
  }
  np->sz = p->sz;
  np->parent = p->parent;
  *np->tf = *p->tf;

  // Clear %eax so that fork returns 0 in the child.
  np->tf->eax = 0;
cprintf("salam");
  for(i = 0; i < NOFILE; i++)
    if(p->ofile[i])
      np->ofile[i] = filedup(p->ofile[i]);
cprintf("salam");
  np->cwd = idup(p->cwd);
cprintf("salam");
  safestrcpy(np->name, p->name, sizeof(p->name));
 
  pid = np->pid;

  // lock to force the compiler to emit the np->state write last.
  acquire(&ptable.lock);
  np->state = RUNNABLE;
  release(&ptable.lock);

    return pid;
} 
Exemplo n.º 30
0
// Create a new process copying p as the parent.
// Sets up stack to return as if from system call.
// Caller must set state of returned proc to RUNNABLE.
int
fork(void)
{
  int i, pid;
  struct proc *np;
  struct proc *curproc = myproc();

  // Allocate process.
  if((np = allocproc()) == 0){
    return -1;
  }

  // Copy process state from proc.
  if((np->pgdir = copyuvm(curproc->pgdir, curproc->sz)) == 0){
    kfree(np->kstack);
    np->kstack = 0;
    np->state = UNUSED;
    return -1;
  }
  np->sz = curproc->sz;
  np->parent = curproc;
  *np->tf = *curproc->tf;

  // Clear %eax so that fork returns 0 in the child.
  np->tf->eax = 0;

  for(i = 0; i < NOFILE; i++)
    if(curproc->ofile[i])
      np->ofile[i] = filedup(curproc->ofile[i]);
  np->cwd = idup(curproc->cwd);

  safestrcpy(np->name, curproc->name, sizeof(curproc->name));

  pid = np->pid;

  acquire(&ptable.lock);

  np->state = RUNNABLE;

  release(&ptable.lock);

  return pid;
}