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
Write a program that removes a target item (a string) from a list of strings, and...
Write a program that removes a target item (a string) from a list of strings, and that will print out the list without the item. Do not use built-in functions other than input, print, and append. Instead, you may generate a new list that does not include the target item and you can use the append() function. Your program should read two inputs: the target and the list. For example, if the input is “apple” and the list is [‘berry’,...
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,...
SOLVE IN C: 6.31 LAB: Print string in reverse Write a program that takes in a...
SOLVE IN C: 6.31 LAB: Print string in reverse Write a program that takes in a line of text as input, and outputs that line of text in reverse. You may assume that each line of text will not exceed 50 characters.The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH Hint: Use the fgets function to...
3. Write a Java program that generates a set of random strings from a given string...
3. Write a Java program that generates a set of random strings from a given string of same length where no character is ever repeated and characters belong to the original string. Examples Input: “Hello World” Output: “World oHlel”
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 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 ...
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...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT