Question

In: Computer Science

#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 program is 0, this program creates no process. How many processes does this program create if the command-line argument is 1?

Question #2 [3 pts]

How many processes does this program create if the command-line argument is 2?

Question #3 [12 pts]

Let T(n) be the number of processes this program creates when its input is n (the answer to Question #1 above is thus T(1)).

Write an recursive formula (i.e., a recurrence relation) that gives T(i) as a function of one or more values of T for smaller input (i.e., smaller i). Explain your reasoning.

Feel free to double-check your formula by actually running the program and possibly augmenting it so that it allows you to count processes in whichever way you want.

Question #4 [3 pts]

Say the maximum number of processes that can be created on your machine is 100,000 (you can find out the actual number of your machine with ulimit -u).

What is the smallest value of the command-line argument for which the above program would experience failed fork() calls?

Solutions

Expert Solution

Solution :

ANSWER 1:

The number of processes created using fork system call is given by the following formula -

so, if the value of ommand-line argument is 1, the number of processes created will be equal to .

ANSWER 2:

If the value of ommand-line argument is 2, the number of processes created will be equal to .

ANSWER 3:-

So, it T(n) is a function of one or more value of T, the following recurrence formula will be formed

where n is the number of process.

The following is the more simpler code-

#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);
}
int pid_t,pid1,pid2;
while(count>0){
   pid1 = fork();
   printf("\n Hello I am the process.My pid is %d \n",getpid());
   fprintf( stderr, "1. parent process ID: %d\n", getppid() );
   if(pid1>0){
       pid2 = fork();
       printf("\n Hello I am the process.My pid is %d\n ",getpid());
       fprintf( stderr, "1. parent process ID: %d\n", getppid() );
       count = count-2;
   }
   else if(pid1==0){
       printf("\n Hello I am the process.My pid is %d \n",getpid());
       fprintf( stderr, "1. parent process ID: %d\n", getppid() );
       count = count-1;
   }
}
exit(0);
return 0;

ANSWER 4:

Given that the maximum number of processes that can be created on your machine is 100,000.

This means

So, 17 is the smallest value of command line argument for which the above program would experience failed fork() calls.


Related Solutions

#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 =...
#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...
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 -...
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 +...
#include<stdio.h> #include<stdlib.h> int main() {     //Q1) Note: You can use only pointer based operations while...
#include<stdio.h> #include<stdlib.h> int main() {     //Q1) Note: You can use only pointer based operations while implementing each of the functionalities below    // zero points will be given if pointer operations are not used in implementing    // each of the operations below.    // Also, use C code only in visual studio or GCC in general.asu.edu server    /* i.) Create a 2 - D array of integers using pointers (use dynamic memory allocation).            Assume that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT