Question
Clone.c /* In this program we use the clone() function to spawn a child process in two different ways: A child spawned as a HEAVYWEIGHT
Clone.c
/* In this program we use the clone() function to spawn a child process in two different ways:
A child spawned as a HEAVYWEIGHT process gets a copy of parents memory, file descriptors, etc, but any changes in child would be only for the child.
A child spawned as a LIGHTWEIGHT process (thread) shares memory and files with its parent by using flags CLONE_VM and CLONE_FILES. The difference is only in a single line, seen below. Comment out the appropriate line depending on which type of child you wish to spawn */
#define _GNU_SOURCE //for Ubuntu #include
// Globals in scope for both main() and do_something() int var = 0; FILE *fd;
int do_something() { printf(" Child in do_something(): "); printf(" My PID is: %d ", getpid()); printf(" My parent's PID is %d ", getppid()); var = 42; // Change global variable fclose(fd); // Close file exit(0); // Exit (terminate child) }
int main(int argc, char *argv[]) { char *stack, *stackTop; // Pointers for child stack const int STACK_SIZE = 65536; // Note stack grows down stack = malloc(STACK_SIZE); // Allocate stack for child if (stack == NULL) { perror("malloc"); exit(1); } stackTop = stack + STACK_SIZE; // Note stack grows down
var = 9; fd = fopen("test.txt", "r");
// Ensure file has been opened successfully if (fd == NULL) { fprintf(stderr, "Missing test.txt file in this directory! "); exit(2); }
printf("VAR in parent before clone: %d ", var);
// Calling clone() this way spawns a HEAVYWEIGHT process (stays in the same VAR) int cret = clone(do_something, stackTop, 0, NULL);
// Calling clone() this way spawns a LIGHTWEIGHT process (changes VAR after clone/sleep to 42) //int cret = clone(do_something, stackTop, CLONE_VM|CLONE_FILES, NULL);
if (cret == -1) { perror("clone() failed"); exit(0); } // Parent keeps going here, child continues execution in do_something() printf("Parent PID after clone() %d ", getpid()); sleep(1);
printf("VAR in parent after clone/sleep: %d ", var); // Attempt to read character from file test.txt char tempch; if (fscanf(fd, "%c", &tempch)
forkAdd.c (Code)
/* In this program we use the clone() function to spawn a child process in two different ways:
A child spawned as a HEAVYWEIGHT process gets a copy of parents memory, file descriptors, etc, but any changes in child would be only for the child.
A child spawned as a LIGHTWEIGHT process (thread) shares memory and files with its parent by using flags CLONE_VM and CLONE_FILES. The difference is only in a single line, seen below. Comment out the appropriate line depending on which type of child you wish to spawn */
#define _GNU_SOURCE //for Ubuntu #include
// Globals in scope for both main() and do_something() int var = 0; FILE *fd;
int do_something() { printf(" Child in do_something(): "); printf(" My PID is: %d ", getpid()); printf(" My parent's PID is %d ", getppid()); var = 42; // Change global variable fclose(fd); // Close file exit(0); // Exit (terminate child) }
int main(int argc, char *argv[]) { char *stack, *stackTop; // Pointers for child stack const int STACK_SIZE = 65536; // Note stack grows down stack = malloc(STACK_SIZE); // Allocate stack for child if (stack == NULL) { perror("malloc"); exit(1); } stackTop = stack + STACK_SIZE; // Note stack grows down
var = 9; fd = fopen("test.txt", "r");
// Ensure file has been opened successfully if (fd == NULL) { fprintf(stderr, "Missing test.txt file in this directory! "); exit(2); }
printf("VAR in parent before clone: %d ", var);
// Calling clone() this way spawns a HEAVYWEIGHT process (stays in the same VAR) int cret = clone(do_something, stackTop, 0, NULL);
// Calling clone() this way spawns a LIGHTWEIGHT process (changes VAR after clone/sleep to 42) //int cret = clone(do_something, stackTop, CLONE_VM|CLONE_FILES, NULL);
if (cret == -1) { perror("clone() failed"); exit(0); } // Parent keeps going here, child continues execution in do_something() printf("Parent PID after clone() %d ", getpid()); sleep(1);
printf("VAR in parent after clone/sleep: %d ", var); // Attempt to read character from file test.txt char tempch; if (fscanf(fd, "%c", &tempch)
test.txt file is a text with a letter a
ab Description Start this lab by opening the accompanying C program clone.c. Read through this file and its comments, understand what it does. Notice particularly that the only difference between spawning a lightweight VS heavyweight process is the arguments passed to the clone() function. Also note that this program attempts to open and read from a file called "test.txt". This is just a plain text file containing a single character. You must create this file yourself. 1) Compile and run clone.c with the following line uncommented (and the other one commented out) Notice the output, notice the character read from the file. int cret = clone(do_something, stackTop,0, NULL); Think about why the file closure and the variable modification made by the child not recognized by the parent. Why, when printed by the parent, does the variable have the original value, despite the modification made by the child? Why is the file still open? 2) Adjust clone.c so that the other clone() call is uncommented instead. Compile and run the program, observe the output. Think about why the file closure and the variable modification by the child ARE recognized in the parent (var has new value, file closed). 3) The second file given with this lab is forkAdd.c. It should not be too hard to discern what it does if you did last week's lab. Check it out, compile it, run it. Next, create a new file named cloneAdd.c that accomplishes the same thing as forkAdd.c, but uses clone() instead of fork(). Start by using clone() to create a HEAVYWEIGHT process. This will achieve behavior closest to fork(). Modify cloneAdd.c slightly to spawn a LIGHTWEIGHT process. Observe the difference in the variable values that get printed. Much like clone.c, keep both clone() lines in cloneAdd.c and simply comment in/out the appropriate one. You cannot simply substitute fork() for clone(). Notice the structure of clone.c and how a clone() call differs from a simple call to fork(). The best way to get started is to use clone.c as a guide and modify it to do the same thing as fork.c. The parent should set y to 1 , and the child should set x to 7Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started