Course Hero Logo

CS2301 Unit 2 Assignment.doc - Running head: UNIT 2...

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 1 - 4 out of 6 pages.

Running head: UNIT 2 ASSIGNMENT1CS2301 UNIT 2 ASSIGNMENTUniversity of the People
UNIT 2 ASSIGNMENT2AssignmentAnswer the following:In Unit 2, we discussed API:Write a program that calls fork(). Before calling fork(), have the main process access a variable(e.g., x) and set its value to some-thing (e.g., 100). What value is the variable in the childprocess? What happens to the variable when both the child and parent change the value of x?1.Write out the code to your program.Code adapted fromArpaci-Dusseau, R. & Arpaci-Dusseau, A. (2012):#include <stdio.h>#include <stdlib.h>#include <unistd.h>int main(int argc, char *argv[]) {int x = 3; // Set variable x to value of 3printf("Initial value of x = %d. \n\n", x); // Print initial value of xint rc = fork();if (rc < 0) {// fork failedfprintf(stderr, "fork failed\n");exit(1);} else if (rc == 0) {// creating child processprintf("-------------------------------\n");printf("Child process\n");
UNIT 2 ASSIGNMENT3printf("-------------------------------\n");printf("Value of x = %d. \n", x);x = 7;printf("Hello, I am the child process (pid:%d) where (x = %d).\n\n", (int) getpid(),x);} else {// parent goes down this path (main)int rc_wait = wait(NULL); // Waits for child finish its executionprintf("-------------------------------\n");printf("Parent process\n");printf("-------------------------------\n");printf("Value of x = %d. \n", x);

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 6 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Fall
Professor
N/A
Tags
Exit, Parent process, Child process

Newly uploaded documents

Show More

Newly uploaded documents

Show More

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture