Reputation: 12197
I'm reading Operating Systems: Three Easy Pieces and I'm finding a problem is not mentioned in the book.
This is the C script:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
/*#include "common.h"*/
#include <unistd.h>
#include <assert.h>
int
main(int argc, char *argv[])
{
int *p = malloc(sizeof(int));
assert(p != NULL);
printf("(%d address pointed to by p: %p\n",
getpid(), p);
*p = 0;
while (1) {
sleep(1);
*p = *p +1;
printf("(%d) p: %d\n", getpid(), *p);
}
return 0;
}
It allocates some memory, prints out the address memory, puts the number 0 into it and finally loops to increment the value.
I compile it through gcc -o mem mem.c -Wall
and I have no problem running it with ./mem
, if I press CRTL+C it will stop:
But then problems come when I run the script twice in parallel with the command ./mem & ./mem
, look at the GIF:
No matter how many times I try to kill the process the scripts keeps hammering.
How to kill my C which project?
Upvotes: 1
Views: 859
Reputation: 361527
Use fg
to bring the backgrounded process to the foreground, then it will respond to Ctrl-C.
You can also use jobs
to see a numbered list of backgrounded jobs, and kill %<number>
to kill a specific job, e.g. kill %1
.
Upvotes: 1