Use p->lock to read p->killed

This commit is contained in:
Frans Kaashoek 2022-08-11 14:22:00 -04:00
parent 429c7b717e
commit 4f716c8550
3 changed files with 17 additions and 3 deletions

View file

@ -91,6 +91,7 @@ pagetable_t proc_pagetable(struct proc *);
void proc_freepagetable(pagetable_t, uint64); void proc_freepagetable(pagetable_t, uint64);
int kill(int); int kill(int);
int killed(struct proc*); int killed(struct proc*);
void setkilled(struct proc*);
struct cpu* mycpu(void); struct cpu* mycpu(void);
struct cpu* getmycpu(void); struct cpu* getmycpu(void);
struct proc* myproc(); struct proc* myproc();

View file

@ -588,7 +588,7 @@ kill(int pid)
for(p = proc; p < &proc[NPROC]; p++){ for(p = proc; p < &proc[NPROC]; p++){
acquire(&p->lock); acquire(&p->lock);
if(p->pid == pid){ if(p->pid == pid){
__atomic_store_n(&p->killed, 1, __ATOMIC_SEQ_CST); p->killed = 1;
if(p->state == SLEEPING){ if(p->state == SLEEPING){
// Wake process from sleep(). // Wake process from sleep().
p->state = RUNNABLE; p->state = RUNNABLE;
@ -601,10 +601,23 @@ kill(int pid)
return -1; return -1;
} }
void
setkilled(struct proc *p)
{
acquire(&p->lock);
p->killed = 1;
release(&p->lock);
}
int int
killed(struct proc *p) killed(struct proc *p)
{ {
return __atomic_load_n(&p->killed, __ATOMIC_SEQ_CST); int k;
acquire(&p->lock);
k = p->killed;
release(&p->lock);
return k;
} }
// Copy to either a user address, or kernel address, // Copy to either a user address, or kernel address,

View file

@ -70,7 +70,7 @@ usertrap(void)
} else { } else {
printf("usertrap(): unexpected scause %p pid=%d\n", r_scause(), p->pid); printf("usertrap(): unexpected scause %p pid=%d\n", r_scause(), p->pid);
printf(" sepc=%p stval=%p\n", r_sepc(), r_stval()); printf(" sepc=%p stval=%p\n", r_sepc(), r_stval());
__atomic_store_n(&p->killed, 1, __ATOMIC_SEQ_CST); setkilled(p);
} }
if(killed(p)) if(killed(p))