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...
Includes you will need: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <sys/wait.h> Create a...
Includes you will need: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <sys/wait.h> Create a .c file that does the following: 1. Use fork() to create a 2nd process, using the if checks we’ve seen to determine if you’re the child or the parent. Print a message from each process saying who is who. (APUE chapter 1/7) • Note: The parent should save the pid_t of the child (returned from fork()) for later. 2. Have the child register a...
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc,...
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc, char **argv) { int n, k, l, r, t, d, i; char str[65]; /* Make the user enter a non-negative integer */ printf("Please enter a non-negative integer: "); scanf("%d", &n); while (n < 0) { printf("Sorry, your input is incorrect.\n"); printf("Please enter a non-negative integer: "); scanf("%d", &n); } /* Convert the integer to reversed binary: e.g. 6 gets converted to 011 */ if...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
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...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
*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...
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]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT