| 424 | } // namespace internals |
| 425 | |
| 426 | pid_t fork_process() { |
| 427 | using namespace internals; |
| 428 | lock_metapage(); |
| 429 | for (int p = 0; p < MAX_PROCESS; p++) { |
| 430 | if (vmem.metapage->process_info[p].pid == 0) { |
| 431 | pid_t pid = fork(); |
| 432 | if (pid < 0) { |
| 433 | // error |
| 434 | return -1; |
| 435 | } else if (pid == 0) { |
| 436 | // child process |
| 437 | int parent = vmem.current_process; |
| 438 | vmem.current_process = p; |
| 439 | lock_metapage(); |
| 440 | vmem.metapage->process_info[p].pid = getpid(); |
| 441 | unlock_metapage(); |
| 442 | send_signal(parent); |
| 443 | } else { |
| 444 | // parent process |
| 445 | unlock_metapage(); |
| 446 | wait_signal(); |
| 447 | // child has unlocked metapage, so we don't need to. |
| 448 | } |
| 449 | return pid; |
| 450 | } |
| 451 | } |
| 452 | unlock_metapage(); |
| 453 | return -1; |
| 454 | } |
| 455 | |
| 456 | void Semaphore::post() { |
| 457 | int wakeup = -1; |
no test coverage detected