Question

In: Computer Science

Write a program to print 2 strings. One string must be written by parent process and...

Write a program to print 2 strings. One string must be written by parent process and another string must be written by a child process

In language C

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
//variable declaration
int pid;
  
//array declaration and initialization
char childStr[] = "Child Process Id: ";
char parentStr[] = "Parent Process Id: ";
  
pid = fork();
  
//child process
if(pid==0)
{
//display child process id
printf( "%s%d\n",childStr, getpid() );
}
//parent process
else
{
//display main porecess or parent process id
printf( "%s%d\n", parentStr, getpid());
}
  
return 0;
}

OUTPUT:

Parent Process Id: 9852
Child Process Id: 9853


Explanation:

fork() system call:

The fork() system call is used to create the child process and the child process is a copy of the parent process that will be run concurrently with the parent process. It returns an integer value and this value may be of three types:

  1. Zero
  2. Positive
  3. Negative

1. Zero

The zero value is returned to the newly created process.

2. Positive value

The positive value is the process id and this value is returned to the parent process.

3. Negative value

If the child process is not created successfully then the negative value is returned.


Related Solutions

Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a whole paragraph with punctuations (up to 500 letters) either input from user, initialize or read from file and provide following functionalities within a class: a)   Declare class Paragraph_Analysis b)   Member Function: SearchWord (to search for a particular word) c)   Member Function: SearchLetter (to search for a particular letter) d)   Member Function: WordCount (to count total words) e)   Member Function: LetterCount (ONLY to count all...
Python Write a program that will analyse the string input and print “accept” or “reject” based...
Python Write a program that will analyse the string input and print “accept” or “reject” based on the pattern given Accept if it fulfils the following conditions -String length 9 -3 small alphabet (3 lowercase letter) -3 digits -3 big alphabet (3 uppercase letters) -1st alphabet should be a capital -Last alphabet should be a number -Two consecutive alphabets can't be small Reject if any of the conditions is absent So i want it to accept or reject my input,...
You will write a C program to fork a child process. The child process will print...
You will write a C program to fork a child process. The child process will print the current time/date. The parent will wait for the child to complete. Here is how you can programmatically call the Linux date command from a C program. execlp("/bin/date","date",NULL); Your C file should be named myfork.c The binary will be named myfork.
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Write a program that uses a for loop to print One of the months of the...
Write a program that uses a for loop to print One of the months of the year is January One of the months of the year is February ...
IN C LANGUAGE This program will read in a series of strings and print only the...
IN C LANGUAGE This program will read in a series of strings and print only the consonants (including Y) until the word "stop" appears. No string will be longer than 100 characters. A consonant is any letter that is not a vowel. Don't forget to follow the standard read pattern! Examples Enter a string: Hello Hll Enter a string: World! Wrld Enter a string: 123! Enter a string: stop Enter a string: stop
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java program that will solve the following problem. The program uses one and two-dimensional arrays to accomplish the tasks specified below. The menu is shown below. Please build a control panel as follows: (Note: the first letter is shown as bold for emphasis and you do not have to make them bold in your program.) Help SetParams FillArray DisplayResults Quit Upon program execution, the screen...
Write a “C” program(Linux) that creates a pipe and forks a child process. The parent then...
Write a “C” program(Linux) that creates a pipe and forks a child process. The parent then sends the following message on his side of the pipe “I am your daddy! and my name is <pid-of the-parent-process>\n”, the child receives this message and prints it to its stdout verbatim. The parent then blocks reading on the pipe from the child. The child writes back to its parent through its side ofthe pipe stating “Daddy, my name is <pid-of-the-child>”. The parent then...
C++ Write a program that outputs an amortization schedule for a given loan. Must be written...
C++ Write a program that outputs an amortization schedule for a given loan. Must be written using user-defined functions must take at least 1 argument Output number of months, payment, interest to pay, principle to pay, and ending balance
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT