#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int value = 10;
int main() {
pid_t pid;
pid = fork();
if (pid == 0) {
value = value + 100;
}
else if (pid > 0) {
value = value -100;
printf("PARENT: value= %d \n", value); //Line A
wait (NULL); }
}
(i) What will be the output in Line A? Justify your answer.
(ii) Do you think there is synchronization problem in updating the variable value?
Justify your answer.We need at least 9 more requests to produce the answer.
1 / 10 have requested this problem solution
The more requests, the faster the answer.
Problem la Points (20) Draw the process tree for the program shown below. #include<sys/types.h> #include<stdio.h> #include<unistd.h> int value = 5; int main() pid_t pid; pid = forko; if(pid ==0) { */ Child Process */ forkO; value += 15; return 0; else if (pid > 0) {/* Parent process */ forkO; forkO; wait(NULL); printf("Parent: value = %d", value); /* LINE A */ return 0;
Systems Programming Purpose: Understand the fork() regarding its returning values, global/local variables declared in the parent's process, and processing Instruction: Attached is a lab.c file containing a fork() statement. Compile and run it. Explain the output of this program. Thing to submit: Submit a word file to explain the following questions: When fork() is executed, what is happening to the returning values in parent and in child? After fork() is executed, how many more process is created? What is the...
Source code to modify:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t pid;
/*fork a child process*/
pid = fork();
if (pid<0){ /*error occured*/
fprintf(stderr,"Fork failed\n");
return 1;
}
else if (pid == 0) { /*child process */
printf("I am the child %d\n",pid);
execlp("/bin/ls","ls",NULL);
}
else { /*parent process */
/*parent will wait for the child to complete*/
printf("I am the parent %d\n",pid);
wait(NULL);
printf("Child Complete\n");
}
return 0;
}
1. Write program codes for 3.21 a and...
OPERATING SYSTEMS QUESTION: Given the code below: int main() { pid_t pid; int value = 10; pid = fork(); if (pid == 0) { /* child */ value += 8; printf(“CHILD: value = %d\n”, value); /* Line A */ exit(0); } else { /* parent */ value += 5; wait(NULL); \ printf(“PARENT: value = %d\n”, value); /* Line B */ exit(0); } } What is printed at Line B? Explain why in a few sentences.
What is the output? #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { int x = 5; int y = 2; int z = 30; x = fork(); y = fork(); if (x != 0) printf("Type 1\n"); if (y != 0) printf("Type 2\n"); z = fork(); if (x > 0 || y > 0 || z > 0) printf("Type 3\n"); if (x == 0 && y == 0 && z != 0) printf("Type 4\n"); if (x...
Suppose that you have three programs that are suppose to print a house diagram in a collaborative manner. (i) prog1.c #include <stdio.h> /* Input/Output */ #include <stdlib.h> /* General Utilities */ int main() { printf(“ ^^^^^^^^^^^^ \n”); printf(“ ^^^^^^^^^^^ ^^^^^^^^^^^ \n”); printf(“^^^^^^^^^^^ ^^^^^^^^^^^ \n”); printf(“ | | \n”); printf(“ | | \n”); exit(1); } (i) prog2.c #include <stdio.h> /* Input/Output */ #include <stdlib.h> /* General Utilities */ int main() { printf(“ | | ...
With explanation Please.
#include #include <sys/types.h> <unistd.h> int main void ) fork fork fork execip ( "/bin/ls", return 0 /* Line A /* Line B/ /*Line C/ /. Line D ./ "ls", NULL); Answer the following: (a) Including the initial parent process, how many processes are created by the program? (b) Answer (a) assuming that lines C and D are interchanged in the program (c) Answer (a) assuming that lines B and D are interchanged (instead of C and D)...
Rewrite the program as a set of two programs that utilize a named pipe “ages” in the current folder for IPC and implement the same function as discussed above. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> int main(void) { int fd[2], nbytes; pid_t childpid; int users = 95; char mystring[80]; char readbuffer[80]; char digit1,digit2; digit1 = (char) users%10; digit2 = (char ) users/10; mystring[0] = digit1; mystring[1] = digit2;...
Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...