CS350 COS
COS
Loading...
Searching...
No Matches
process.c File Reference
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <sys/syscall.h>
#include <sys/kassert.h>
#include <sys/kconfig.h>
#include <sys/kdebug.h>
#include <sys/kmem.h>
#include <sys/ktime.h>
#include <sys/mp.h>
#include <sys/spinlock.h>
#include <sys/thread.h>
#include <machine/trap.h>
#include <machine/pmap.h>
Include dependency graph for process.c:

Go to the source code of this file.

Functions

ProcessProcess_Create (Process *parent, const char *title)
 
static void Process_Destroy (Process *proc)
 
ProcessProcess_Lookup (uint64_t pid)
 
void Process_Retain (Process *proc)
 
void Process_Release (Process *proc)
 
uint64_t Process_Wait (Process *proc, uint64_t pid)
 
void Process_Dump (Process *proc)
 
static void Debug_Processes (int argc, const char *argv[])
 
 REGISTER_DBGCMD (processes, "Display list of processes", Debug_Processes)
 
static void Debug_ProcInfo (int argc, const char *argv[])
 
 REGISTER_DBGCMD (procinfo, "Display current process state", Debug_ProcInfo)
 

Variables

ThreadcurProc [MAX_CPUS]
 
Spinlock procLock
 
uint64_t nextProcessID
 
ProcessQueue processList
 
Slab processSlab
 

Function Documentation

◆ Debug_Processes()

static void Debug_Processes ( int  argc,
const char *  argv[] 
)
static

Definition at line 269 of file process.c.

270{
271 Process *proc;
272
273 /*
274 * We don't hold locks in case you the kernel debugger is entered while
275 * holding this lock.
276 */
277 //Spinlock_Lock(&threadLock);
278
280 {
281 kprintf("Process: %d(%016llx)\n", proc->pid, proc);
282 Process_Dump(proc);
283 }
284
285 //Spinlock_Unlock(&threadLock);
286}
int kprintf(const char *fmt,...)
Definition: printf.c:210
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:557
Definition: thread.h:65
uint64_t pid
Definition: thread.h:66
ProcessQueue processList
Definition: process.c:30
void Process_Dump(Process *proc)
Definition: process.c:251
Here is the call graph for this function:

◆ Debug_ProcInfo()

static void Debug_ProcInfo ( int  argc,
const char *  argv[] 
)
static

Definition at line 291 of file process.c.

292{
293 Thread *thr = curProc[CPU()];
294
295 kprintf("Current Process State:\n");
296 Process_Dump(thr->proc);
297}
#define CPU
Definition: mp.h:7
Definition: thread.h:31
struct Process * proc
Definition: thread.h:39
Thread * curProc[MAX_CPUS]
Definition: sched.c:41
Here is the call graph for this function:

◆ Process_Create()

Process * Process_Create ( Process parent,
const char *  title 
)

Process_Create –

Create a process.

Parameters
[in]parentParent process.
[in]titleProcess title.
Returns
The newly created process.

Definition at line 50 of file process.c.

51{
53
54 if (!proc)
55 return NULL;
56
57 memset(proc, 0, sizeof(*proc));
58
59 proc->pid = nextProcessID++;
60 proc->threads = 0;
61 proc->refCount = 1;
63 TAILQ_INIT(&proc->threadList);
64
65 if (title) {
66 strncpy((char *)&proc->title, title, PROCESS_TITLE_LENGTH);
67 } else {
68 proc->title[0] = '\0';
69 }
70
71 proc->space = PMap_NewAS();
72 if (proc->space == NULL) {
73 Slab_Free(&processSlab, proc);
74 return NULL;
75 }
77
78 Spinlock_Init(&proc->lock, "Process Lock", SPINLOCK_TYPE_NORMAL);
79
80 Semaphore_Init(&proc->zombieSemaphore, 0, "Zombie Semaphore");
81 TAILQ_INIT(&proc->zombieQueue);
82
83 Handle_Init(proc);
84
85 proc->parent = parent;
86 if (parent) {
87 Spinlock_Lock(&parent->lock);
88 TAILQ_INSERT_TAIL(&parent->childrenList, proc, siblingList);
89 Spinlock_Unlock(&parent->lock);
90 }
92 TAILQ_INIT(&proc->zombieProc);
93 Mutex_Init(&proc->zombieProcLock, "Zombie Process Lock");
94 CV_Init(&proc->zombieProcCV, "Zombie Process CV");
95 CV_Init(&proc->zombieProcPCV, "Zombie Process PCV");
96
100
101 return proc;
102}
void CV_Init(CV *cv, const char *name)
Definition: cv.c:25
void Handle_Init(Process *proc)
Definition: handle.c:23
#define PROC_STATE_NULL
Definition: thread.h:61
#define PROCESS_TITLE_LENGTH
Definition: thread.h:59
void Slab_Free(Slab *slab, void *obj)
Definition: slab.c:135
void * Slab_Alloc(Slab *slab) __attribute__((malloc))
Definition: slab.c:105
AS * PMap_NewAS()
Definition: pmap.c:80
#define MEM_USERSPACE_STKBASE
Definition: pmap.h:36
#define TAILQ_INIT(head)
Definition: queue.h:597
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:641
void Semaphore_Init(Semaphore *sema, int count, const char *name)
void Spinlock_Unlock(Spinlock *lock) __UNLOCK_EX(*lock)
Definition: spinlock.c:109
#define SPINLOCK_TYPE_NORMAL
Definition: spinlock.h:12
void Spinlock_Lock(Spinlock *lock) __LOCK_EX(*lock)
Definition: spinlock.c:75
void Spinlock_Init(Spinlock *lock, const char *name, uint64_t type)
Definition: spinlock.c:43
#define NULL
Definition: stddef.h:6
char * strncpy(char *to, const char *from, size_t len)
Definition: string.c:34
void * memset(void *dst, int c, size_t len)
Definition: string.c:164
Spinlock lock
Definition: thread.h:68
uint64_t refCount
Definition: thread.h:73
ProcessQueue zombieProc
Definition: thread.h:81
ThreadQueue threadList
Definition: thread.h:87
uintptr_t ustackNext
Definition: thread.h:71
char title[PROCESS_TITLE_LENGTH]
Definition: thread.h:74
ThreadQueue zombieQueue
Definition: thread.h:89
uint64_t threads
Definition: thread.h:86
Process * parent
Definition: thread.h:78
Semaphore zombieSemaphore
Definition: thread.h:88
CV zombieProcPCV
Definition: thread.h:84
Mutex zombieProcLock
Definition: thread.h:82
int procState
Definition: thread.h:75
CV zombieProcCV
Definition: thread.h:83
ProcessQueue childrenList
Definition: thread.h:80
AS * space
Definition: thread.h:67
void Mutex_Init(Mutex *mtx, const char *name)
Definition: mutex.c:30
uint64_t nextProcessID
Definition: process.c:29
Spinlock procLock
Definition: process.c:28
Slab processSlab
Definition: process.c:33
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Process_Destroy()

static void Process_Destroy ( Process proc)
static

Process_Destroy –

Destroy a process. This should not be called direct, but rather by Process_Release.

Parameters
[in]procProcess to destroy.

Definition at line 113 of file process.c.

114{
115 Handle_Destroy(proc);
116
117 Spinlock_Destroy(&proc->lock);
120 CV_Destroy(&proc->zombieProcCV);
122 PMap_DestroyAS(proc->space);
123
124 // XXX: We need to promote zombie processes to our parent
125 // XXX: Release the semaphore as well
126
130
131 Slab_Free(&processSlab, proc);
132}
void CV_Destroy(CV *cv)
Definition: cv.c:33
void Handle_Destroy(Process *proc)
Definition: handle.c:33
void PMap_DestroyAS(AS *space)
Definition: pmap.c:116
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:659
void Semaphore_Destroy(Semaphore *sema)
Definition: semaphore.c:39
void Spinlock_Destroy(Spinlock *lock)
Definition: spinlock.c:61
void Mutex_Destroy(Mutex *mtx)
Definition: mutex.c:39
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Process_Dump()

void Process_Dump ( Process proc)

Definition at line 251 of file process.c.

252{
253 const char *stateStrings[] = {
254 "NULL",
255 "READY",
256 "ZOMBIE"
257 };
258
259 kprintf("title %s\n", proc->title);
260 kprintf("pid %llu\n", proc->pid);
261 kprintf("state %s\n", stateStrings[proc->procState]);
262 kprintf("space %016llx\n", proc->space);
263 kprintf("threads %llu\n", proc->threads);
264 kprintf("refCount %d\n", proc->refCount);
265 kprintf("nextFD %llu\n", proc->nextFD);
266}
uint64_t nextFD
Definition: thread.h:91
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Process_Lookup()

Process * Process_Lookup ( uint64_t  pid)

Process_Lookup –

Lookup a process by PID and increment its reference count.

Parameters
[in]pidProcess ID to search for.
Return values
NULLNo such process.
Returns
Process that corresponds to the pid.

Definition at line 145 of file process.c.

146{
147 Process *p;
148 Process *proc = NULL;
149
152 if (p->pid == pid) {
154 proc = p;
155 break;
156 }
157 }
159
160 return proc;
161}
void Process_Retain(Process *proc)
Definition: process.c:171
Here is the call graph for this function:

◆ Process_Release()

void Process_Release ( Process proc)

Process_Release –

Decrement the reference count for a given process.

Parameters
procProcess to release a reference of.

Definition at line 185 of file process.c.

186{
187 ASSERT(proc->refCount != 0);
188 if (__sync_fetch_and_sub(&proc->refCount, 1) == 1) {
189 Process_Destroy(proc);
190 }
191}
#define ASSERT(_x)
Definition: kassert.h:8
static void Process_Destroy(Process *proc)
Definition: process.c:113
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Process_Retain()

void Process_Retain ( Process proc)

Process_Retain –

Increment the reference count for a given process.

Parameters
procProcess to retain a reference to.

Definition at line 171 of file process.c.

172{
173 ASSERT(proc->refCount != 0);
174 __sync_fetch_and_add(&proc->refCount, 1);
175}
Here is the caller graph for this function:

◆ Process_Wait()

uint64_t Process_Wait ( Process proc,
uint64_t  pid 
)

Process_Wait –

Wait for a process to exit and then cleanup it's references. If the pid == 0, we wait for any process, otherwise we wait for a specific process.

Parameters
[in]procParent process.
[in]pidOptionally specify the pid of the process to wait on.
Return values
ENOENTProcess ID doesn't exist.
Returns
Exit status of the process that exited or crashed.

Definition at line 206 of file process.c.

207{
208 Thread *thr;
209 Process *p = NULL;
210 uint64_t status;
211
212 // XXXFILLMEIN
213 /*
214 * Dummy waitpid implementation that pretends the
215 * process has already exited. Remove and replace
216 * with the actual implementation from the assignment
217 * description.
218 */
219 /* XXXREMOVE START */
220 ASSERT(pid != 0);
221 return (pid << 16);
222 /* XXXREMOVE END */
223
224 status = (p->pid << 16) | (p->exitCode & 0xff);
225
226 // Release threads
227 Spinlock_Lock(&proc->lock);
228 while (!TAILQ_EMPTY(&p->zombieQueue)) {
229 thr = TAILQ_FIRST(&p->zombieQueue);
230 TAILQ_REMOVE(&p->zombieQueue, thr, schedQueue);
231 Spinlock_Unlock(&proc->lock);
232
233 ASSERT(thr->proc->pid != 1);
234 Thread_Release(thr);
235
236 Spinlock_Lock(&proc->lock);
237 }
238 Spinlock_Unlock(&proc->lock);
239
240 // Release process
242
243 return SYSCALL_PACK(0, status);
244}
void Thread_Release(Thread *thr)
Definition: thread.c:265
#define TAILQ_FIRST(head)
Definition: queue.h:555
#define TAILQ_EMPTY(head)
Definition: queue.h:553
uint64_t exitCode
Definition: thread.h:76
#define SYSCALL_PACK(_errcode, _val)
Definition: syscall.h:55
void Process_Release(Process *proc)
Definition: process.c:185
unsigned long uint64_t
Definition: types.h:13
Here is the call graph for this function:
Here is the caller graph for this function:

◆ REGISTER_DBGCMD() [1/2]

REGISTER_DBGCMD ( processes  ,
"Display list of processes"  ,
Debug_Processes   
)

◆ REGISTER_DBGCMD() [2/2]

REGISTER_DBGCMD ( procinfo  ,
"Display current process state"  ,
Debug_ProcInfo   
)

Variable Documentation

◆ curProc

Thread* curProc[MAX_CPUS]
extern

Current thread executing on a given CPU.

Definition at line 41 of file sched.c.

◆ nextProcessID

uint64_t nextProcessID

Definition at line 29 of file process.c.

◆ processList

ProcessQueue processList

Definition at line 30 of file process.c.

◆ processSlab

Slab processSlab

Definition at line 33 of file process.c.

◆ procLock

Spinlock procLock

Definition at line 28 of file process.c.