コード例 #1
0
ファイル: proc.c プロジェクト: suoyita/labcodes
/* do_fork -     parent process for a new child process
 * @clone_flags: used to guide how to clone the child process
 * @stack:       the parent's user stack pointer. if stack==0, It means to fork a kernel thread.
 * @tf:          the trapframe info, which will be copied to child process's proc->tf
 */
int
do_fork(uint32_t clone_flags, uintptr_t stack, struct trapframe *tf) {
    int ret = -E_NO_FREE_PROC;
    struct proc_struct *proc;
    if (nr_process >= MAX_PROCESS) {
        goto fork_out;
    }
    ret = -E_NO_MEM;
    //LAB4:EXERCISE2 2013011303
    /*
     * Some Useful MACROs, Functions and DEFINEs, you can use them in below implementation.
     * MACROs or Functions:
     *   alloc_proc:   create a proc struct and init fields (lab4:exercise1)
     *   setup_kstack: alloc pages with size KSTACKPAGE as process kernel stack
     *   copy_mm:      process "proc" duplicate OR share process "current"'s mm according clone_flags
     *                 if clone_flags & CLONE_VM, then "share" ; else "duplicate"
     *   copy_thread:  setup the trapframe on the  process's kernel stack top and
     *                 setup the kernel entry point and stack of process
     *   hash_proc:    add proc into proc hash_list
     *   get_pid:      alloc a unique pid for process
     *   wakup_proc:   set proc->state = PROC_RUNNABLE
     * VARIABLES:
     *   proc_list:    the process set's list
     *   nr_process:   the number of process set
     */

    //    1. call alloc_proc to allocate a proc_struct
    //    2. call setup_kstack to allocate a kernel stack for child process
    //    3. call copy_mm to dup OR share mm according clone_flag
    //    4. call copy_thread to setup tf & context in proc_struct
    //    5. insert proc_struct into hash_list && proc_list
    //    6. call wakup_proc to make the new child process RUNNABLE
    //    7. set ret vaule using child proc's pid
    if ((proc = alloc_proc()) == NULL) {
    	goto fork_out;
    }
    proc->pid = get_pid();
    proc->parent = current;
    nr_process++;
    if (setup_kstack(proc)) {
    	goto bad_fork_cleanup_proc;
    }
    if (copy_mm(clone_flags, proc)) {
    	goto bad_fork_cleanup_kstack;
    }
    copy_thread(proc, stack, tf);
    hash_proc(proc);
    list_add(&proc_list, &(proc->list_link));
    wakeup_proc(proc);
    ret = proc->pid;

fork_out:
    return ret;

bad_fork_cleanup_kstack:
    put_kstack(proc);
bad_fork_cleanup_proc:
    kfree(proc);
    goto fork_out;
}
コード例 #2
0
ファイル: proc.c プロジェクト: chyyuu/ucore-arch-arm
void
proc_init_ap(void)
{
	int lcpu_idx = pls_read(lcpu_idx);
	int lapic_id = pls_read(lapic_id);

	pls_write(idleproc, alloc_proc());
	if (idleproc == NULL) {
        panic("cannot alloc idleproc.\n");
    }

    idleproc->pid = lcpu_idx;
    idleproc->state = PROC_RUNNABLE;
	// XXX
    // idleproc->kstack = (uintptr_t)bootstack;
    idleproc->need_resched = 1;
	idleproc->tf = NULL;
    if ((idleproc->fs_struct = fs_create()) == NULL) {
        panic("create fs_struct (idleproc) failed.\n");
    }
    fs_count_inc(idleproc->fs_struct);

	char namebuf[32];
	snprintf(namebuf, 32, "idle/%d", lapic_id);
	
    set_proc_name(idleproc, namebuf);
    nr_process ++;

	pls_write(current, idleproc);

	assert(idleproc != NULL && idleproc->pid == lcpu_idx);
}
コード例 #3
0
ファイル: proc.c プロジェクト: xieyf13/ucore_lab
// proc_init - set up the first kernel thread idleproc "idle" by itself and 
//           - create the second kernel thread init_main
void
proc_init(void) {
    int i;

    list_init(&proc_list);
    for (i = 0; i < HASH_LIST_SIZE; i ++) {
        list_init(hash_list + i);
    }

    if ((idleproc = alloc_proc()) == NULL) {
        panic("cannot alloc idleproc.\n");
    }

    idleproc->pid = 0;
    idleproc->state = PROC_RUNNABLE;
    idleproc->kstack = (uintptr_t)bootstack;
    idleproc->need_resched = 1;
    set_proc_name(idleproc, "idle");
    nr_process ++;

    current = idleproc;

    int pid = kernel_thread(init_main, "Hello world!!", 0);
    if (pid <= 0) {
        panic("create init_main failed.\n");
    }

    initproc = find_proc(pid);
    set_proc_name(initproc, "init");

    assert(idleproc != NULL && idleproc->pid == 0);
    assert(initproc != NULL && initproc->pid == 1);
}
コード例 #4
0
ファイル: proc.c プロジェクト: xuchiheng/ucore_plus
void proc_init_ap(void)
{
	int cpuid = myid();
	struct proc_struct *idle;

	idle = alloc_proc();
	if (idle == NULL) {
		panic("cannot alloc idleproc.\n");
	}

	idle->pid = cpuid;
	idle->state = PROC_RUNNABLE;
	// XXX
	// idle->kstack = (uintptr_t)bootstack;
	idle->need_resched = 1;
	idle->tf = NULL;
	if ((idle->fs_struct = fs_create()) == NULL) {
		panic("create fs_struct (idleproc) failed.\n");
	}
	fs_count_inc(idle->fs_struct);

	char namebuf[32];
	snprintf(namebuf, 32, "idle/%d", cpuid);

	set_proc_name(idle, namebuf);
	nr_process++;

	idleproc = idle;
	current = idle;

	assert(idleproc != NULL && idleproc->pid == cpuid);
}
コード例 #5
0
ファイル: proc.c プロジェクト: Shuriken13/spoc_exercises
/* do_fork -     parent process for a new child process
 * @clone_flags: used to guide how to clone the child process
 * @stack:       the parent's user stack pointer. if stack==0, It means to fork a kernel thread.
 * @tf:          the trapframe info, which will be copied to child process's proc->tf
 */
int
do_fork(uint32_t clone_flags, uintptr_t stack, struct trapframe *tf) {
    int ret = -E_NO_FREE_PROC;
    struct proc_struct *proc;
    if (nr_process >= MAX_PROCESS) {
        goto fork_out;
    }
    ret = -E_NO_MEM;
    //LAB4:EXERCISE2 2012011346
    /*
     * Some Useful MACROs, Functions and DEFINEs, you can use them in below implementation.
     * MACROs or Functions:
     *   alloc_proc:   create a proc struct and init fields (lab4:exercise1)
     *   setup_kstack: alloc pages with size KSTACKPAGE as process kernel stack
     *   copy_thread:  setup the trapframe on the  process's kernel stack top and
     *                 setup the kernel entry point and stack of process
     *   hash_proc:    add proc into proc hash_list
     *   get_pid:      alloc a unique pid for process
     *   wakeup_proc:  set proc->state = PROC_RUNNABLE
     * VARIABLES:
     *   proc_list:    the process set's list
     *   nr_process:   the number of process set
     */



    //    1. call alloc_proc to allocate a proc_struct
    proc = alloc_proc();
    proc->pid = get_pid();

    cprintf("fork pid = %d\n", proc->pid);


    //    2. call setup_kstack to allocate a kernel stack for child process
    setup_kstack(proc);
    //    3. call copy_thread to setup tf & context in proc_struct
    copy_thread(proc, stack, tf);
    //    4. insert proc_struct into  proc_list
    list_add_before(&proc_list, &proc->list_link);
    //    5. call wakeup_proc to make the new child process RUNNABLE
    wakeup_proc(proc);
    //    7. set ret vaule using child proc's pid
    nr_process++;
    ret = proc->pid;
    //  8. set parent
    proc->parent = current;
fork_out:
    return ret;

bad_fork_cleanup_kstack:
    put_kstack(proc);
bad_fork_cleanup_proc:
    kfree(proc);
    goto fork_out;
}
コード例 #6
0
ファイル: proc.c プロジェクト: liangchao1992/ucore
// proc_init - set up the first kernel thread idleproc "idle" by itself and 
//           - create the second kernel thread init_main
void proc_init(void)
{
	int i;
	int cpuid = myid();
	struct proc_struct *idle;

	spinlock_init(&proc_lock);
	list_init(&proc_list);
	list_init(&proc_mm_list);
	for (i = 0; i < HASH_LIST_SIZE; i++) {
		list_init(hash_list + i);
	}

	idle = alloc_proc();
	if (idle == NULL) {
		panic("cannot alloc idleproc.\n");
	}

	idle->pid = cpuid;
	idle->state = PROC_RUNNABLE;
	// No need to be set for kthread (no privilege switch)
	// idleproc->kstack = (uintptr_t)bootstack;
	idle->need_resched = 1;
	idle->tf = NULL;
	if ((idle->fs_struct = fs_create()) == NULL) {
		panic("create fs_struct (idleproc) failed.\n");
	}
	fs_count_inc(idle->fs_struct);

	char namebuf[32];
	snprintf(namebuf, 32, "idle/%d", cpuid);

	set_proc_name(idle, namebuf);
	nr_process++;

	idleproc = idle;
	current = idle;

	int pid = ucore_kernel_thread(init_main, NULL, 0);
	if (pid <= 0) {
		panic("create init_main failed.\n");
	}

	initproc = find_proc(pid);
	set_proc_name(initproc, "kinit");
	char *proc_init="Proc init OK";
	assert(idleproc != NULL && idleproc->pid == cpuid);
	assert(initproc != NULL && initproc->pid == sysconf.lcpu_count);
}
コード例 #7
0
ファイル: proc.c プロジェクト: chyyuu/ucore-arch-arm
// proc_init - set up the first kernel thread idleproc "idle" by itself and 
//           - create the second kernel thread init_main
void
proc_init(void) {
    int i;
	int lcpu_idx = pls_read(lcpu_idx);
	int lapic_id = pls_read(lapic_id);
	int lcpu_count = pls_read(lcpu_count);

    list_init(&proc_list);
    list_init(&proc_mm_list);
    for (i = 0; i < HASH_LIST_SIZE; i ++) {
        list_init(hash_list + i);
    }

	pls_write(idleproc, alloc_proc());
    if (idleproc == NULL) {
        panic("cannot alloc idleproc.\n");
    }

    idleproc->pid = lcpu_idx;
    idleproc->state = PROC_RUNNABLE;
	// XXX
    // idleproc->kstack = (uintptr_t)bootstack;
    idleproc->need_resched = 1;
	idleproc->tf = NULL;
    if ((idleproc->fs_struct = fs_create()) == NULL) {
        panic("create fs_struct (idleproc) failed.\n");
    }
    fs_count_inc(idleproc->fs_struct);

	char namebuf[32];
	snprintf(namebuf, 32, "idle/%d", lapic_id);
	
    set_proc_name(idleproc, namebuf);
    nr_process ++;

    pls_write(current, idleproc);

    int pid = kernel_thread(init_main, NULL, 0);
    if (pid <= 0) {
        panic("create init_main failed.\n");
    }

    initproc = find_proc(pid);
    set_proc_name(initproc, "init");

    assert(idleproc != NULL && idleproc->pid == lcpu_idx);
    assert(initproc != NULL && initproc->pid == lcpu_count);
}
コード例 #8
0
ファイル: matrizcpar.c プロジェクト: carlosstenzel/TCC
main()
{
int nprocs = 4;

//tempo

struct timeval iniciotmp, finaltmp;

int tempogasto;


int pidf,id_wait;
void dt_shareG();

   mc_var_inic ();

   def_task (0, "inicializa_matriz", sizeof (__inicializa_matriz__));
   def_task (1, "multiplica_matriz", sizeof (__multiplica_matriz__));
   id_wait=sem_wait_inic(2);


   alloc_proc(nprocs);
      
   pidf = exec_task (0, nprocs);  /* --- rs30() - create --- */
   if (pidf == 0)   {
      inicializa_matriz();
      end_task();            /* --- rs31() - create --- */
   }

    wait_all();
    gettimeofday(&iniciotmp, NULL);
      
   pidf = exec_task (1, nprocs);  /* --- rs30() - create --- */
   if (pidf == 0)   {
      multiplica_matriz();
      end_task();            /* --- rs31() - create --- */
   }

    wait_all();
    gettimeofday(&finaltmp, NULL);
    tempogasto = (int) (1000 * (finaltmp.tv_sec - iniciotmp.tv_sec) + (finaltmp.tv_usec - iniciotmp.tv_usec) / 1000);
    printf("Tempo decorrido: %d\n", tempogasto);
       wait_all();      /* --- rs307()  */
   end_program();
   remove_semaforo(id_wait);
return 0;
}
コード例 #9
0
ファイル: proc.c プロジェクト: thinxer/ucore-multi
// do_fork - parent process for a new child process
//    1. call alloc_proc to allocate a proc_struct
//    2. call setup_kstack to allocate a kernel stack for child process
//    3. call copy_mm to dup OR share mm according clone_flag
//    4. call wakup_proc to make the new child process RUNNABLE 
int
do_fork(uint32_t clone_flags, uintptr_t stack, struct trapframe *tf) {
    int ret = -E_NO_FREE_PROC;
    struct proc_struct *proc;
    if (nr_process >= MAX_PROCESS) {
        goto fork_out;
    }

    ret = -E_NO_MEM;

    if ((proc = alloc_proc()) == NULL) {
        goto fork_out;
    }

    proc->parent = current;

    if (setup_kstack(proc) != 0) {
        goto bad_fork_cleanup_proc;
    }
    if (copy_mm(clone_flags, proc) != 0) {
        goto bad_fork_cleanup_kstack;
    }
    copy_thread(proc, stack, tf);

    unsigned long intr_flag;
    local_irq_save(intr_flag);
    {
        proc->pid = get_pid();
        hash_proc(proc);
        list_add(&proc_list, &(proc->list_link));
        nr_process ++;
    }
    local_irq_restore(intr_flag);

    wakeup_proc(proc);

    ret = proc->pid;
fork_out:
    return ret;

bad_fork_cleanup_kstack:
    put_kstack(proc);
bad_fork_cleanup_proc:
    kfree(proc);
    goto fork_out;
}
コード例 #10
0
ReturnOop BufferedFile::allocate(JvmAllocProc alloc_proc JVM_TRAPS) {
  if (alloc_proc != NULL) {
    OopDesc* oop_desc =
      (OopDesc*)alloc_proc(sizeof(BufferedFileDesc));
    if (oop_desc == NULL) {
      Throw::out_of_memory_error(JVM_SINGLE_ARG_THROW_0);
      return NULL;
    }

    // Must zero-inititialize the allocated block to match 
    // ObjectHeap::allocate behavior.
    jvm_memset((char*)oop_desc, 0, sizeof(BufferedFileDesc));
      
    return oop_desc;
  } else {
    return BufferedFile::allocate(JVM_SINGLE_ARG_CHECK_0);
  }
}
コード例 #11
0
ファイル: Buffer.cpp プロジェクト: hbao/phonemefeaturedevices
ReturnOop Buffer::allocate(unsigned int length, 
                           JvmAllocProc alloc_proc JVM_TRAPS) {
  if (alloc_proc != NULL) {
    OopDesc* oop_desc = (OopDesc*)alloc_proc(sizeof(ArrayDesc) +
                                             length * sizeof(jubyte));
    if (oop_desc == NULL) {
      Throw::out_of_memory_error(JVM_SINGLE_ARG_THROW_0);
    }
    // Must zero-inititialize the allocated block to match 
    // ObjectHeap::allocate behavior.
    jvm_memset((char*)oop_desc + sizeof(ArrayDesc), 0, 
               length * sizeof(jubyte));
    *oop_desc->int_field_addr(Array::length_offset()) = length;
    return oop_desc;
  } else {
    return Universe::new_byte_array(length JVM_CHECK_0);
  }
}
コード例 #12
0
ファイル: proc.c プロジェクト: liangchao1992/ucore
void proc_init_ap(void)
{
	int cpuid = myid();
	struct proc_struct *idle;

	idle = alloc_proc();
	if (idle == NULL) {
		panic("cannot alloc idleproc.\n");
	}

	idle->pid = cpuid;
	idle->state = PROC_RUNNABLE;
	// No need to be set for kthread (no privilege switch)
	// idle->kstack = (uintptr_t)bootstack;
	idle->need_resched = 1;
	idle->tf = NULL;
	if ((idle->fs_struct = fs_create()) == NULL) {
		panic("create fs_struct (idleproc) failed.\n");
	}
	fs_count_inc(idle->fs_struct);

	char namebuf[32];
	snprintf(namebuf, 32, "idle/%d", cpuid);

	set_proc_name(idle, namebuf);
	nr_process++;

	idleproc = idle;
	current = idle;
#if 1
	int pid;
	char proc_name[32];
	if((pid = ucore_kernel_thread(krefcache_cleaner, NULL, 0)) <= 0){
		panic("krefcache_cleaner init failed.\n");
	}
	struct proc_struct* cleaner = find_proc(pid);
	snprintf(proc_name, 32, "krefcache/%d", myid());
	set_proc_name(cleaner, proc_name);
	set_proc_cpu_affinity(cleaner, myid());
	nr_process++;
#endif

	assert(idleproc != NULL && idleproc->pid == cpuid);
}
コード例 #13
0
ファイル: proc.c プロジェクト: Shuriken13/spoc_exercises
// proc_init - set up the first kernel thread idleproc "idle" by itself and
//           - create the second kernel thread init_main
void
proc_init(void) {
    int i;

    list_init(&proc_list);

    if ((idleproc = alloc_proc()) == NULL) {
        panic("cannot alloc idleproc.\n");
    }

    idleproc->pid = 0;
    idleproc->state = PROC_RUNNABLE;
    idleproc->kstack = (uintptr_t)bootstack;
    idleproc->need_resched = 1;
    set_proc_name(idleproc, "idle");
    nr_process ++;

    current = idleproc;

    int pid1 = kernel_thread(init_main, "init main1: Hello world!!", 0);
    int pid2 = kernel_thread(init_main, "init main2: Hello world!!", 0);
    int pid3 = kernel_thread(init_main, "init main3: Lab4 spoc discussion!!", 0);
    if (pid1 <= 0 || pid2 <= 0 || pid3 <= 0) {
        panic("create kernel thread init_main1 or 2 or 3 failed.\n");
    }

    initproc1 = find_proc(pid1);
    initproc2 = find_proc(pid2);
    initproc3 = find_proc(pid3);

    set_proc_name(initproc1, "init1");
    set_proc_name(initproc2, "init2");
    set_proc_name(initproc3, "init3");
    cprintf("proc_init:: Created kernel thread init_main--> pid: %d, name: %s\n", initproc1->pid, initproc1->name);
    cprintf("proc_init:: Created kernel thread init_main--> pid: %d, name: %s\n", initproc2->pid, initproc2->name);
    cprintf("proc_init:: Created kernel thread init_main--> pid: %d, name: %s\n", initproc3->pid, initproc3->name);
    assert(idleproc != NULL && idleproc->pid == 0);
}
コード例 #14
0
ファイル: problema1.c プロジェクト: carlosstenzel/TCC
int main()
{
int i, j;


int pidf,id_wait;
void dt_shareG();

   mc_var_inic ();

   def_task (0, "multiplicarAB", sizeof (__multiplicarAB__));
   def_task (1, "multiplicarCD", sizeof (__multiplicarCD__));
   def_task (2, "sumar", sizeof (__sumar__));
   def_task (3, "print", sizeof (__print__));
   id_wait=sem_wait_inic(4);


   for (i=0; i<100; i++){
        for (j = 0; j < 100; j++)
        {
            /*A[i][j] = i + j;
            B[i][j] = i + 2*j;
            C[i][j] = i*2 + j*3;
            D[i][j] = i*2 + j;*/
            sharedG->A[i][j] = 1;
            sharedG->B[i][j] = 1;
            sharedG->C[i][j] = 1;
            sharedG->D[i][j] = 1;
        }
    }

    alloc_proc(4);

      
   pidf = exec_task (0, 1);  /* --- rs30() - create --- */
   if (pidf == 0)   {
      multiplicarAB();
      end_task();            /* --- rs31() - create --- */
   }

      
   pidf = exec_task (1, 1);  /* --- rs30() - create --- */
   if (pidf == 0)   {
      multiplicarCD();
      end_task();            /* --- rs31() - create --- */
   }


    wait_all();

      
   pidf = exec_task (2, 1);  /* --- rs30() - create --- */
   if (pidf == 0)   {
      sumar();
      end_task();            /* --- rs31() - create --- */
   }

    
    
   wait_proc (2);    /* --- wait_proc() --- */


      
   pidf = exec_task (3, 1);  /* --- rs30() - create --- */
   if (pidf == 0)   {
      print();
      end_task();            /* --- rs31() - create --- */
   }


       wait_all();      /* --- rs307()  */
   end_program();
   remove_semaforo(id_wait);
return 0;
}
コード例 #15
0
ファイル: proc.c プロジェクト: chyyuu/ucore-arch-arm
// do_fork - parent process for a new child process
//    1. call alloc_proc to allocate a proc_struct
//    2. call setup_kstack to allocate a kernel stack for child process
//    3. call copy_mm to dup OR share mm according clone_flag
//    4. call wakup_proc to make the new child process RUNNABLE 
int
do_fork(uint32_t clone_flags, uintptr_t stack, struct trapframe *tf) {
    int ret = -E_NO_FREE_PROC;
    struct proc_struct *proc;
    if (nr_process >= MAX_PROCESS) {
        goto fork_out;
    }

    ret = -E_NO_MEM;

    if ((proc = alloc_proc()) == NULL) {
        goto fork_out;
    }

    proc->parent = current;
    list_init(&(proc->thread_group));
    assert(current->wait_state == 0);

    assert(current->time_slice >= 0);
    proc->time_slice = current->time_slice / 2;
    current->time_slice -= proc->time_slice;

    if (setup_kstack(proc) != 0) {
        goto bad_fork_cleanup_proc;
    }
    if (copy_sem(clone_flags, proc) != 0) {
        goto bad_fork_cleanup_kstack;
    }
    if (copy_fs(clone_flags, proc) != 0) {
        goto bad_fork_cleanup_sem;
    }
    if ( copy_signal(clone_flags, proc) != 0 ) {
      goto bad_fork_cleanup_fs;
    }
    if ( copy_sighand(clone_flags, proc) != 0 ) {
      goto bad_fork_cleanup_signal;
    }
    if (copy_mm(clone_flags, proc) != 0) {
        goto bad_fork_cleanup_sighand;
    }
    if (copy_thread(clone_flags, proc, stack, tf) != 0) {
      goto bad_fork_cleanup_sighand;
    }

    bool intr_flag;
    local_intr_save(intr_flag);
    {
        proc->pid = get_pid();
		    proc->tid = proc->pid;
        hash_proc(proc);
        set_links(proc);
        if (clone_flags & CLONE_THREAD) {
          list_add_before(&(current->thread_group), &(proc->thread_group));
          proc->gid = current->gid;
        }else{
          proc->gid = proc->pid;
        }
    }
    local_intr_restore(intr_flag);

    wakeup_proc(proc);

    ret = proc->pid;
fork_out:
    return ret;
bad_fork_cleanup_sighand:
    put_sighand(proc);
bad_fork_cleanup_signal:
    put_signal(proc);
bad_fork_cleanup_fs:
    put_fs(proc);
bad_fork_cleanup_sem:
    put_sem_queue(proc);
bad_fork_cleanup_kstack:
    put_kstack(proc);
bad_fork_cleanup_proc:
    kfree(proc);
    goto fork_out;
}
コード例 #16
0
static void read_procs(void) {
    DIR *proc_dir, *task_dir;
    struct dirent *pid_dir, *tid_dir;
    char filename[64];
    FILE *file;
    int proc_num;
    struct proc_info *proc;
    pid_t pid, tid;

    int i;

    proc_dir = opendir("/proc");
    if (!proc_dir) die("Could not open /proc.\n");

    new_procs = calloc(INIT_PROCS * (threads ? THREAD_MULT : 1), sizeof(struct proc_info *));
    num_new_procs = INIT_PROCS * (threads ? THREAD_MULT : 1);

    file = fopen("/proc/stat", "r");
    if (!file) die("Could not open /proc/stat.\n");
    fscanf(file, "cpu  %lu %lu %lu %lu %lu %lu %lu", &new_cpu.utime, &new_cpu.ntime, &new_cpu.stime,
            &new_cpu.itime, &new_cpu.iowtime, &new_cpu.irqtime, &new_cpu.sirqtime);
    fclose(file);

    proc_num = 0;
    while ((pid_dir = readdir(proc_dir))) {
        if (!isdigit(pid_dir->d_name[0]))
            continue;

        pid = atoi(pid_dir->d_name);
        
        struct proc_info cur_proc;
        
        if (!threads) {
            proc = alloc_proc();

            proc->pid = proc->tid = pid;

            sprintf(filename, "/proc/%d/stat", pid);
            read_stat(filename, proc);

            sprintf(filename, "/proc/%d/cmdline", pid);
            read_cmdline(filename, proc);

            sprintf(filename, "/proc/%d/status", pid);
            read_status(filename, proc);

            read_policy(pid, proc);

            proc->num_threads = 0;
        } else {
            sprintf(filename, "/proc/%d/cmdline", pid);
            read_cmdline(filename, &cur_proc);

            sprintf(filename, "/proc/%d/status", pid);
            read_status(filename, &cur_proc);
            
            proc = NULL;
        }

        sprintf(filename, "/proc/%d/task", pid);
        task_dir = opendir(filename);
        if (!task_dir) continue;

        while ((tid_dir = readdir(task_dir))) {
            if (!isdigit(tid_dir->d_name[0]))
                continue;

            if (threads) {
                tid = atoi(tid_dir->d_name);

                proc = alloc_proc();

                proc->pid = pid; proc->tid = tid;

                sprintf(filename, "/proc/%d/task/%d/stat", pid, tid);
                read_stat(filename, proc);

                read_policy(tid, proc);

                strcpy(proc->name, cur_proc.name);
                proc->uid = cur_proc.uid;
                proc->gid = cur_proc.gid;

                add_proc(proc_num++, proc);
            } else {
                proc->num_threads++;
            }
        }

        closedir(task_dir);
        
        if (!threads)
            add_proc(proc_num++, proc);
    }

    for (i = proc_num; i < num_new_procs; i++)
        new_procs[i] = NULL;

    closedir(proc_dir);
}
コード例 #17
0
ファイル: process.c プロジェクト: karthikbox/os
/* creates the first process */
void userinit(){
	struct proc *p;
	/* printf("entered userinit\n"); */
	/* allocate memory for stdin wait queue */
	_stdin = (struct read_proc*)kmalloc(sizeof(struct read_proc));
	memset1((char *)_stdin,0,sizeof(struct read_proc));
	memset1((char *)ptable.proc,0,sizeof(ptable));
	p=alloc_proc();
	initproc=p;
	fgproc=initproc;
	if(!(p->pml4_t=load_kern_vm())){
		/* panic code goes here*/
		printf("Kernel Panic: Unable To Create Initproc\n");
	}
	//inituvm(p->pml4_t,binary_initcode_start,binary_initcode_size);
	p->size=FRAME_SIZE;
	/* clear the trapframe. this is only done for fist process */
	memset1((char *)p->tf,0,sizeof(struct trapframe));
	/* p->tf->cs=0x8/\* SEG_KCS() *\/;			/\* kernel code segment *\/ */
	/* p->tf->ss=0x10/\* SEG_KDS() *\/;			/\* kernel data segment *\/ */
	p->tf->cs=0x1B;			/* user code segment */
	p->tf->ss=0x23;			/* user data segment */
	p->tf->rflags=FL_IF;			/* enable interrupts */
	
	/* rsp will be sey in exec */
	/* p->tf->rsp=FRAME_SIZE; /\* where does the user stack star from in the proc vm *\/ */

	/* rip will be set in exec */
	/* p->tf->rip=0;		   /\* begining of initcode.S *\/ */

	strcpy(p->name,"initcode");	/* p->name has a size of 32 bytes */
	p->state=RUNNABLE;
	
	
	/* printf("calling exec\n"); */
	
	/* call exec */
	proc=p;
	/* set cwd of initproc to root '/' */
	strcpy(proc->cwd,"/");

	char *a="bin/hello";
	char *argv[3];
	argv[0]=a;argv[1]="os/sbunix";argv[2]=NULL;
	char *envp[7];
	envp[0]="PATH=/bin";
	envp[1]="envp1";
	envp[2]="envp2";
	envp[3]="envp3";
	envp[4]="envp4";
	envp[5]="envp5";
	envp[6]=NULL;
	cli();

	exec("bin/hello",argv,envp);
	//p->tf->cs=(SEG_);
	/* ltr(0x2Bu); */
	/* ltr 0x2B   with RPL of 3 for user??? */
	ltr(0x2Bu);

	/* set WP bit in cr0 */
	set_wp_bit();


	/* clear ftable */
	memset1((char *)ftable.file,0,sizeof(struct file)*NFILE);
	/* clear file descriptor table of proc,i.e initproc */
	int fd=0;
	for(fd=0;fd<NOFILE;fd++){
		proc->ofile[fd]=NULL;
	}
	struct file *fp=NULL;
	/* give initproc STDIN, STDOUT,STDERR*/
	proc->ofile[STDIN]=filealloc();
	if(proc->ofile[STDIN]==NULL){
		printf("Kernel Panic, STDIN fiel struct not allocd for initproc\n");
		/* panic. kill proc */
	}
	fp=proc->ofile[STDIN];
	fp->type=FD_STDIN;
	fp->readable=1;	/* mark readable */
	fp->writable=0;	/* mark not writable */

	proc->ofile[STDOUT]=filealloc();
	if(proc->ofile[STDOUT]==NULL){
		printf("Kernel Panic: STDOUT file struct not allocd for initproc\n");
		/* panic. kill proc */
	}
	fp=proc->ofile[STDOUT];
	fp->type=FD_STDOUT;
	fp->readable=0;	/* mark NOT readable */
	fp->writable=1;	/* mark writable */


	proc->ofile[STDERR]=filealloc();
	if(proc->ofile[STDERR]==NULL){
		printf("Kernel Panic: STDERR fiel struct not allocd for initproc\n");
		/* panic. kill proc */
	}
	fp=proc->ofile[STDERR];
	fp->type=FD_STDERR;
	fp->readable=0;	/* mark NOT readable */
	fp->writable=1;	/* mark  writable */


	/* initialize sleep_head and sleep_tail to NULL */
	init_sleep_queue();
	init_waitpid_queue();
	init_stdin_queue();

	/* initialize inodes */
	/* init_inodes(); */
	/* printf writes to <1MB mem region. Now user page tables are loaded. We cannot access <1MB since we did not map that region into user process < 1MB VM aread */
	/* printf("calling scheduler\n"); */
	scheduler();
}