Question

In: Computer Science

#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid;...

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>


int main(int argc, char **argv)
{
        pid_t pid;

        // Main process's PID=42

        pid = fork(); // creates process with PID=36
        if (pid == 0) {
                pid_t pid2 = fork(); // creates process with PID=99
                sleep(10);
                if (pid2 > 0) {
                        sleep(10);
                        exit(0);
                } else {
                        sleep(30);
                        printf("** ONE **\n");
                        exit(0);
                }
        } else {
                pid_t pid3 = fork(); // creates process with PID=71
                if (pid3 == 0) {
                        sleep(30);
                        exit(0);
                } 
                pid_t pid4 = fork(); // creates process with PID=123
                if (pid4 > 0) {
                        waitpid(pid3, NULL, 0);
                        sleep(20);
                        printf("** TWO **\n");
                        exit(0);
                }  
                exit(0);
        }

        sleep(20);
        exit(0);
}

Assume that the PID of the main process is 42. Each call to fork() in the code is commented to show the PID of the newly created process (The PIDS are thus 42, 36, 99, 71, and 123). All calls to all functions / systems calls succeed.

In this exercise we assume that all operations take zero time but for sleep() (which sleeps a prescribed number of seconds) and waitpid(), which obviously might block for a while. We also assume that the execution starts at time zero. Therefore, in all the questions below, whenever a time is asked, it’s always a perfect multiple of 10 (since all calls to sleep in the above program are for numbers of seconds that are multiples of 10).

Answer the following questions:

  • q1 [3 pts]: At what time is “** ONE **” printed to the terminal?

  • q2 [3 pts]: At what time is “** TWO **” printed to the terminal?

  • q3 [3 pts]: What is the PID of the process that prints “** TWO **”?

  • q4 [3 pts]: What is the initial PPID of the process with PID 99?

  • q5 [3 pts]: At what time does the process with PID 123 terminate?

  • q6 [5 pts]: How many children (NOT counting grand-children, great-grant-children, etc.) does PID 42 create?

  • q7 [5 pts]: What is the PPID of the process that prints ONE (when it prints it)

  • q8 [5 pts]: Which processes are “alive” (i.e., not terminated/zombies) at time 25

Solutions

Expert Solution

The fork() system call creates a new process. The return value is used to test whether it is the parent process or the newly created child process. In the parent process, the return value will be the pid of the child, while the return value will be 0 in the child process. Based on this, the following table gives the information regarding the code blocks that are executed in each process.

Code segment

PIDs in which it will be executed

Time (at beginning of code block)

        // Main process's PID=42

        pid = fork(); // creates process with PID=36

42

0

        if (pid == 0) {

                pid_t pid2 = fork(); // creates process with PID=99

36

0

                sleep(10);

36, 99

0

                if (pid2 > 0) {

                        sleep(10);

                        exit(0);

36

10

                } else {

                        sleep(30);

                        printf("** ONE **\n");

                        exit(0);

                }

99

10

        } else {

                pid_t pid3 = fork(); // creates process with PID=71

42

0

                if (pid3 == 0) {

                        sleep(30);

                        exit(0);

                }

71

0

                pid_t pid4 = fork(); // creates process with PID=123

42

0

                if (pid4 > 0) {

                        waitpid(pid3, NULL, 0);

                        sleep(20);

                        printf("** TWO **\n");

                        exit(0);

                }

42

0

                exit(0);

        }

        sleep(20);

        exit(0);

}

123

0

Based on the above analysis in the table, the answers are as follows:

  • q1 [3 pts]: At what time is “** ONE **” printed to the terminal?

40 seconds

  • q2 [3 pts]: At what time is “** TWO **” printed to the terminal?

The waitpid(pid3, NULL); blocks the process till process 71 is completed, which happens at 30 seconds. It sleeps for a further 20 seconds before printing TWO. So, TWO is printed at 50 seconds

  • q3 [3 pts]: What is the PID of the process that prints “** TWO **”?

The PID is 42

  • q4 [3 pts]: What is the initial PPID of the process with PID 99?

PID 99 is forked by PID 36. So the initial PPID is 36

  • q5 [3 pts]: At what time does the process with PID 123 terminate?

PID 123 terminates immediately, ie at time 0 seconds

  • q6 [5 pts]: How many children (NOT counting grand-children, great-grant-children, etc.) does PID 42 create?

The fork calls executed in PID 42 are the following:

- pid = fork(); // creates process with PID=36

- pid_t pid3 = fork(); // creates process with PID=71

- pid_t pid4 = fork(); // creates process with PID=123

So the total number of children is 3.

  • q7 [5 pts]: What is the PPID of the process that prints ONE (when it prints it)

ONE is printed by PID 99. It initially has a PPID of 36. But PID 36 dies at 20 seconds. Following this, its PPID becomes 42 (the PPID of 36). So at the time of printing ONE, its PPID is 36.

  • q8 [5 pts]: Which processes are “alive” (i.e., not terminated/zombies) at time 25

PID 123 dies at time 0, 36 at time 20 seconds. These are the only two processes that die before 25 seconds. So the processes alive at that time are: 71, 99, 42


Related Solutions

#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
int main(int argc, char *argv[]){ int fd1, fd2; char buffer[100]; long int n1; if(((fd1 = open(argv[1],...
int main(int argc, char *argv[]){ int fd1, fd2; char buffer[100]; long int n1; if(((fd1 = open(argv[1], O_RDONLY)) == -1) || ((fd2 = open(argv[2], O_CREAT|O_WRONLY|O_TRUNC,0700)) == -1)){ perror("file problem "); exit(1); } while((n1=read(fd1, buffer, 512) > 0)) if(write(fd2, buffer, n1) != n1){ perror("writing problem "); exit(3); } // Case of an error exit from the loop if(n1 == -1){ perror("Reading problem "); exit(2); } close(fd2); exit(0); } Could anyone fix the two issues in the while loop, so the ouput can...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;     ...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");      while(1)      {         c = fgetc(fp1);         if(c==EOF)             break;         else             printf("%c", c);      }      fclose(fp1);      return 0; } In the program above which statement is functioning for opening a file Write the syntax for opening a file What mode that being used in the program. Give the example from the program Referring to...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the computer won, 2 if there is a tie, and -1 if the player decides to quit int menu(int *); // Displays choices to user // Receives score array int main() { srand(42); // Seeding Random with 42 int score[3]; // Array keeping Player, Computer, and Tie Scores score [0] = 0; // Player - initialized to Zero score [1] = 0; // Computer -...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I have random array [4,2,7,1,9,8,0]. Sort the array's index but cannot change the position of item in the array, if you copy the array to a new array, you also cannot change the item's position in array. The index now is[0,1,2,3,4,5,6] The output should be[6,3,1,0,2,5,4]
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
Use the given Strings.c and Strings.h module: Strings.c: #include "Strings.h" #include <string.h> #include<stdlib.h> #include<stdio.h> char* substring(char*...
Use the given Strings.c and Strings.h module: Strings.c: #include "Strings.h" #include <string.h> #include<stdlib.h> #include<stdio.h> char* substring(char* str, int iPos){ if(iPos > strlen(str)||iPos < 0)return (char*)NULL; char* substr; substr = &str[iPos]; return substr; } int charPosition(char* str, char c){ char* string = (char*)malloc(strlen(str)+1); int i; for(i = 0; i < strlen(str)+1; i++) { if(str[i] == c) { return i; } } return -1; } Strings.h: #include <string.h> /* substring - return a pointer to the substring beginning at the iPos-th position....
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT