Chunk Start Size in Decimal Size in Hex Allocated/Free ----------- --------------- ----------- -------------- 0x147d58 40 0x28 Free 0x147d80 56 0x38 Allocated 0x147db8 32 0x20 Free 0x147dd8 64 0x40 Allocated 0x147e18 32 0x20 Free 0x147e38 32 0x20 Allocated 0x147e58 48 0x30 Free 0x147e88 208 0xd0 AllocatedHow did I figure those values out? Here's how I did it:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { if (fork() != 0) return 0; // The parent exits instantly. sleep(1); printf("I am an orphan.\n"); // The child waits for a second to print its line. return 0; } |
#include <stdio.h> #include <stdlib.h> #include <unistd.h> /* Have the child create the pipe. */ int main() { int status; int p[2]; if (fork() != 0) { // The parent simply creates the child and waits. wait(&status); return 0; } else { pipe(p); close(p[0]); // Close the read end. while(1) write(p[1], "Fred", 4); // Repeatedly write until you generate SIGPIPE return 0; } } |
#include <stdio.h> #include <stdlib.h> #include <unistd.h> /* Have the parent create the pipe. */ int main() { int status; int p[2]; pipe(p); if (fork() != 0) { // Since the parent creates the pipe, it needs to close(p[0]); // close the read end. wait(&status); return 0; } else { close(p[0]); // The child needs to close the read end, too. while(1) write(p[1], "Fred", 4); // Repeatedly write until you generate SIGPIPE return 0; } } |
The best thing is for Y to call alarm() and to set up a signal handler to catch SIGLARM. The signal handler will kill E and D, and then when it returns, the wait() calls will return.
Either answer is ok, BTW.
/* Missing Thread Code. */ pthread_mutex_lock(s->lock); while (1) { pthread_cond_wait(s->cvs[p->id], s->lock); if (scanf("%d", &num) != 1) exit(0); // This kills the process when stdin is done. printf("%3ld: Thread %d\n", time(0) - s->base_time, p->id); fflush(stdout); sleep(num); pthread_cond_signal(s->main_cv); } /* Missing Main Code. It would be OK for you to lock the mutex at the beginning and forget about it. */ i = 0; while (1) { printf("%3ld: Main\n", time(0) - s->base_time); sleep(1); pthread_mutex_lock(s->lock); pthread_cond_signal(s->cvs[i]); pthread_cond_wait(s->main_cv, s->lock); pthread_mutex_unlock(s->lock); i = (i + 1) % n; } |
/* Missing Thread Code */ pthread_mutex_lock(s->lock); printf("I'm Ready\n"); s->n++; pthread_cond_signal(s->main_cv); pthread_mutex_unlock(s->lock); while(1); /* Missing Main Code */ pthread_mutex_lock(s->lock); while (s->n < n) pthread_cond_wait(s->main_cv, s->lock); printf("I'm Ready Too\n"); pthread_mutex_unlock(s->lock); // This is unnecessary, but good form. while(1); |