Commit 6e4cd994 authored by Zhaozhong Liu's avatar Zhaozhong Liu
Browse files

Lab 1, problem 1 completed

parent bbe15365
(1.1)There are 2 num instances, one in the memory space of the parent and one in the memory space of the child. That is why the child increment by 1 while the parent increment by 3 at each time.
"The child process is created with a single thread—the one that called fork(). The entire vir‐
tual address space of the parent is replicated in the child, including the states of mutexes,
condition variables, and other pthreads objects; the use of pthread_atfork(3) may be helpful for
dealing with problems that this can cause." from man 2 fork.
(1.2)
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
* CSCI 315 Operating Systems Design
* Date: 2014-09-02
* Copyright (c) 2014 Bucknell University
*
* Permission is hereby granted, free of charge, to any individual or
* institution obtaining a copy of this software and associated
* documentation files (the "Software"), to use, copy, modify, and
* distribute without restriction, provided that this copyright and
* permission notice is maintained, intact, in all copies and supporting
* documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL BUCKNELL UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
int main(int argc, char *argv[]) {
int pid; int num;
if ((pid = fork()) == -1) {
perror("something went wrong in fork");
exit(-1);
} else if (pid == 0) {
for (num=0; num < 20; num++) {
printf("child: %d\n", num); fflush(stdout);
printf("Get pid: %d\n", getpid()); fflush(stdout);
printf("Get ppid: %d\n:", getppid()); fflush(stdout);
sleep(1);
}
} else {
wait(NULL);
for (num=0; num < 20; num+=3) {
printf("parent: %d\n", num); fflush(stdout);
printf("Parent Pid: %d\n", pid);
sleep(1);
}
}
exit(0);
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
/*
* CSCI 315 Operating Systems Design
* Date: 2014-09-02
* Copyright (c) 2014 Bucknell University
*
* Permission is hereby granted, free of charge, to any individual or
* institution obtaining a copy of this software and associated
* documentation files (the "Software"), to use, copy, modify, and
* distribute without restriction, provided that this copyright and
* permission notice is maintained, intact, in all copies and supporting
* documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL BUCKNELL UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
int main(int argc, char *argv[]) {
int pid; int num;
if ((pid = fork()) == -1) {
perror("something went wrong in fork");
exit(-1);
} else if (pid == 0) {
for (num=0; num < 20; num++) {
printf("child: %d\n", num); fflush(stdout);
printf("Child Pid: %d\n", pid);
sleep(1);
}
} else {
for (num=0; num < 20; num+=3) {
printf("parent: %d\n", num); fflush(stdout);
printf("Parent Pid: %d\n", pid);
sleep(1);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment