. In the code below what will be the first lines of code executed by the parent
ID: 3763987 • Letter: #
Question
. In the code below what will be the first lines of code executed by the parent process immediately after the fork? Child process? b. getpid() is a system call which allows the process to retrieve its own process id. Suppose that in the code below the process id of the parent is 1234 and the process id of the child is 5678. What will the following code output? (Please note: the %i in the printf statement specifies that the value returned by getpid() should be printed as an integer).
int main()
{ pid_it pid = fork();
printf("Hello world!");
if(pid == 0)
{ printf("CAPS LOCK - Preventing Login Since 1980 said: %i ", getpid());
}
else
{
printf("Keyboard not found. Please press ENTER to continue said: %i ", getpid());
}
return 0;
}
Explanation / Answer
firstly the fork() which returns the value i.e's 0 or 1 if it is child and parent process respectively.
so ths value of pid is zero then it is a child process then it specifies it is child process which enters in to the if loop
it prints the value i.e's CAPS LOCK - Preventing Login Since 1980 said: 5678
getpid() return the process id of the calling process.the id is granteed to be unique and is useful for temporary file names. so the value returned would be 5678
whereas if the pid is one then it is a parent process. which leads to else loop and the output would be
Keyboard not found. Please press ENTER to continue said:1234
so the output would be (for loop) is
CAPS LOCK - Preventing Login Since 1980 said: 5678
Keyboard not found. Please press ENTER to continue said:1234
we know the child proccess should execut first and then followed by parent so
OUTPUT for the program would be
Hello world!Keyboard not found. Please press ENTER to continue said: 5678
Hello world!CAPS LOCK - Preventing Login Since 1980 said: 1234
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.